This morning, festive update VS2019, the official version 3.0 finally ah ~
A small partners say one thing Autofac how access because Startup.ConfigureServices can no longer return value into IServiceProvider, the original dependency injection container replacement is not feasible, I casually said, a bit above the Host. UseServiceProviderFactory
It's that simple thought, he asked me to be an example, toss a bit and found things are not so simple
. UseServiceProviderFactory <TContainer> to match Startup written inside a ConfigureContainer (TContainer builder)
Internal IServiceCollection.AddAutofac Autofac official actually implemented a single embodiment of the container into a IServiceProviderFactory <ContainerBuilder>
But not UseServiceProviderFactory <TContainer> configuration inside, then it can only
services.AddAutofac(container=>
{
});
And can not have ConfigureContainer (ContainerBuilder builder), or else you will be prompted
For example, we have a MyClass
public class MyClass { public void Hello() { Console.WriteLine("Hello"); } }
We configure the look
Host
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseServiceProviderFactory(new AutofacServiceProviderFactory()) //增加 .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
then
Startup.ConfigureContainer
public void ConfigureContainer(ContainerBuilder builder) { builder.RegisterType<MyClass>().SingleInstance(); }
or
services.AddAutofac(container=> { container.RegisterType<MyClass>().SingleInstance(); });
It's done ~ ~ ~