unitycontainer / microsoft-dependency-injection

Unity.Microsoft.DependencyInjection package
Apache License 2.0
63 stars 36 forks source link

Cannot register IServiceProviderFactory #21

Closed tranceporter closed 5 years ago

tranceporter commented 6 years ago

Hi,

I do realise that your notes mention that it is currently not possible to bootstrap the Startup class, but I am not sure if it's the same as the issue I am currently experiencing.

Program.cs has

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseUnityServiceProvider()
            .UseStartup<Startup>()
            .Build();
}

Startup.cs has

    public void ConfigureContainer(IUnityContainer container)
    {
        var logger = Logger.For(this).ForAction(nameof(ConfigureContainer));
        logger.Info("Registering Messaging with UnityContainer...");
        container.RegisterMessaging(Configuration);

        logger.Info("Registering other Services with UnityContainer...");
        container.RegisterServices(Configuration);
    }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(
            options =>
                {
                    options.TokenValidationParameters =
                        new TokenValidationParameters().WithConfiguredParameters(this.Configuration);
                });

        EnableCors(services);
        services.AddMvc();
    }

When I run this and try to call an API, I get an exception

InvalidOperationException: No service for type 'Microsoft.Extensions.DependencyInjection.IServiceProviderFactory`1[Microsoft.Practices.Unity.IUnityContainer]' has been registered.

Is there any way around this? I am using version 2.0.10.

EDIT: Re-reading the exception, I just realised that it is trying to bootstrap IServiceProviderFactory from Microsoft.Practices.Unity.IUnityContainer which I am assuming is the wrong IUnityContainer? We currently have some in-house Nuget packages which are class libraries and reference Microsoft.Practices.Unity, and resolve themselves using a UnityBoostrapperModule and MEF. Is there anyway for Microsoft.Practices.Unity.IUnityContainer to co-exist with Unity.IUnityContainer?

Thanks, Shreyas

tranceporter commented 6 years ago

For now I am getting around this by copying registration from Microsoft.Practices.Unity.IUnityContainer to Unity.IUnityContainer

foreach (var registration in currentContainer.Registrations) { container.RegisterType(registration.RegisteredType, registration.MappedToType); }

ENikS commented 6 years ago

Microsoft.Practices.Unity.IUnityContainer is being used up to v4.0.1. Unity 5+ uses Unity.IUnityContainer