Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3.7.0 and v3.7.1 prevents from registering multiple services for a given type? #206

Open
bounav opened this issue Jul 19, 2023 · 1 comment

Comments

@bounav
Copy link

bounav commented Jul 19, 2023

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?

@bounav
Copy link
Author

bounav commented Dec 12, 2023

I totally forgot about this, updated our dependency to LightInject.Microsoft.DepedencyInjection and unleashed hell again. 🔥

Has anyone else had problems with that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant