v3.7.0 and v3.7.1 prevents from registering multiple services for a given type?
Opened this issue · 2 comments
Hello,
We have just noticed something odd after updating to LightInject.Microsoft.DependencyInjection
from v3.6.3
to v3.7.1
(v3.7.0 is also affected):
We can no longer have multiple service registrations for a given type.
Steps to reproduce
Create a new CLI project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net48</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LightInject" Version="6.6.4" />
<PackageReference Include="LightInject.Microsoft.DependencyInjection" Version="3.7.1" />
</ItemGroup>
</Project>
Program
using System;
using LightInject;
using LightInject.Microsoft.DependencyInjection;
namespace LightInjectRepro
{
public interface IFoo
{
}
namespace One
{
public class Foo : IFoo
{
}
}
namespace Two
{
public class Foo : IFoo
{
}
}
internal class Program
{
static void Main(string[] args)
{
if (args.Length != 1)
{
throw new ArgumentException("Only only arg is permitted: e.g. \"LightInjectRepro.exe One\"");
}
var options = new ContainerOptions().WithMicrosoftSettings();
var container = new ServiceContainer(options);
container.Register<IFoo, One.Foo>("One.Foo");
container.Register<IFoo, Two.Foo>("Two.Foo");
container.Register<IFoo>(f =>
{
switch (args[0])
{
case "one":
case "One":
return f.GetInstance<IFoo>("One.Foo");
case "two":
case "Two":
return f.GetInstance<IFoo>("Two.Foo");
default:
throw new ArgumentException(
$"Invalid cli parameter: {args[0]} (only One or Two are valid values)");
}
});
var foo = container.GetInstance<IFoo>();
return;
}
}
}
The code above runs but container.GetInstance<IFoo>();
always returns an instance of Two.Foo
. The f => {}
third registration doesn't kick in.
The container.AvailableServices
enumerable does contain the three services that have been registered.
Downgrading to <PackageReference Include="LightInject.Microsoft.DependencyInjection" Version="3.6.3" />
changes the behaviour back to how it worked in the past and the callback runs and you get the either and instance of One.Foo
or Two.Foo
depending on your cli parameter.
Could this be a consequence of that change?
I totally forgot about this, updated our dependency to LightInject.Microsoft.DepedencyInjection
and unleashed hell again. 🔥
Has anyone else had problems with that?
@bounav This is possibly fixed with the v4.
See #207 (comment)
(But for me, a new bug was introduced)