Install Unity.Microsoft.DependencyInjection 2.1.1 but got "No service for type" exception
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.
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));
});
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;
}
Do you want to send a PR?
Yes, I'll do it.
Thanks @AlexKaramushko . This solved it for me. You should definitely make that PR.
@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