You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the WebHostBuilder add UseUnityServiceProvider(...) method
publicstaticIHostBuilderCreateHostBuilder(string[]args)=>Host.CreateDefaultBuilder(args).UseUnityServiceProvider()<----Addthis line
.ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseStartup<Startup>();});
In case Unity container configured via application configuration or by convention this container could be used to initialize service provider.
publicstaticIHostBuilderCreateHostBuilder(string[]args)=>Host.CreateDefaultBuilder(args).UseUnityServiceProvider(_container)//<---- Add this line.ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseStartup<Startup>();});
Add optional method to your Startup class
publicvoidConfigureContainer(IUnityContainercontainer){// Could be used to register more typescontainer.RegisterType<IMyService,MyService>();}
Resolving Controllers from Unity
By default ASP resolves controllers using built in activator. To enable resolution of controllers from Unity you need to add following line to MVC configuration:
publicvoidConfigureServices(IServiceCollectionservices){
...services.AddMvc().AddControllersAsServices()//<-- Add this line.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
...}
Examples
For example of using Unity with Core 3.1 Web application follow this link