microsoft-dependency-injection icon indicating copy to clipboard operation
microsoft-dependency-injection copied to clipboard

Install Unity.Microsoft.DependencyInjection 2.1.1 but got "No service for type" exception

Open vulcanlee opened this issue 8 years ago • 6 comments

My environment is VS2017 15.8.3

I create a new ASP.NET Core MVC project and install NuGet Package of Unity.Microsoft.DependencyInjection 2.1.1, then register Unity as default service provider and change the Startup class by Unity.Microsoft.DependencyInjection Documentation.

Whne I run this project and got "No service for type" exception, you can do following procedure for get this error

  • I create ASP.NET Core MVC project
  • Create Message.cs file and add following code
    public interface IMessage
    {
        void Send(string message);
    }

    public class ConsoleMessage : IMessage
    {
        public void Send(string message)
        {
            Console.WriteLine($"ConsoleMessage :{message}");
        }
    }
    public class FileMessage : IMessage
    {
        public void Send(string message)
        {
            Console.WriteLine($"FileMessage :{message}");
        }
    }
  • Install package for "Unity.Microsoft.DependencyInjection" 2.1.1
  • Add UseUnityServiceProvider() in CreateWebHostBuilder method of Program.cs file
        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseUnityServiceProvider()   // Add Unity as default Service Provider
                .UseStartup<Startup>();
  • Add ConfigureContainer method to Startup.cs
        public void ConfigureContainer(IUnityContainer container)
        {
            // Could be used to register more types
            container.RegisterType<IMessage, ConsoleMessage>();
        }
  • Build and run this project, then get following exception
System.InvalidOperationException: 'No service for type 'System.Collections.Generic.IEnumerable`1[Microsoft.AspNetCore.Hosting.Internal.IStartupConfigureContainerFilter`1[Unity.IUnityContainer]]' has been registered.'

You can get this demo project from this repository

More Information

  • when I remove ConfigureContainer method from Startup.cs, above error will disappear.
  • If I downgrade to Unity.Microsoft.DependencyInjection 2.0.12 but ConfigureContainer method still in Startup.cs file, the project work fine.

vulcanlee avatar Sep 28 '18 04:09 vulcanlee

When I reference the Unity.Microsoft.DependencyInjection 2.1.1 project source and have change the Unity.Microsoft.DependencyInjection 2.1.1 source code by modify the class HostingExtension.UseUnityServiceProvider method that remove this line : services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IServiceCollection>>(_factory));

And build and run this project

            return hostBuilder.ConfigureServices((context, services) =>
            {
                services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IUnityContainer>>(_factory));
                // When I remove this line,the test program can work fine.
                //services.Replace(ServiceDescriptor.Singleton<IServiceProviderFactory<IServiceCollection>>(_factory));
            });

vulcanlee avatar Sep 28 '18 09:09 vulcanlee

Following update made it to work end-to-end

Should documentation be updated (Readme.md) ?

Update Startup.cs void ConfigureServices (IServiceCollection services) to return IServiceProvider e.g.

public IServiceProvider ConfigureServices(IServiceCollection services) {

// *** services.AddMvc() etc. code ***

Unity.Microsoft.DependencyInjection.ServiceProvider serviceProvider = 
    Unity.Microsoft.DependencyInjection.ServiceProvider.ConfigureServices(services)
    as Unity.Microsoft.DependencyInjection.ServiceProvider;

    ConfigureContainer((UnityContainer)serviceProvider);

    return serviceProvider;

}

AlexKaramushko avatar Oct 15 '18 17:10 AlexKaramushko

Do you want to send a PR?

ENikS avatar Oct 15 '18 20:10 ENikS

Yes, I'll do it.

AlexKaramushko avatar Oct 16 '18 07:10 AlexKaramushko

Thanks @AlexKaramushko . This solved it for me. You should definitely make that PR.

johnxy84 avatar Dec 05 '18 12:12 johnxy84

@ENikS I have created the pull request. https://github.com/unitycontainer/microsoft-dependency-injection/pull/31 However, build failed. I'm not sure why. Changes are in README.md only. Please let me know - should I do anything else. Thank you, Alex

AlexKaramushko avatar Dec 09 '18 07:12 AlexKaramushko