1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
/* Web: var builder = WebApplication.CreateBuilder(args); builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); */ var builder = Host.CreateDefaultBuilder() .UseServiceProviderFactory(new AutofacServiceProviderFactory()) .ConfigureAppConfiguration((hostContext, config) => { config .SetBasePath(System.IO.Directory.GetCurrentDirectory()) .AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true) .AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true) .AddEnvironmentVariables(); }) .ConfigureServices((hostContext, services) => { IConfiguration config = hostContext.Configuration; Console.WriteLine(config.GetDebugView()); services.AddLogging(b => { b.AddDebug(); b.AddConsole(); }); // Register over IServiceCollection }) .ConfigureContainer<ContainerBuilder>(builder => { builder.RegisterInstance(new LoggerFactory()) .As<ILoggerFactory>(); builder.RegisterGeneric(typeof(Logger<>)) .As(typeof(ILogger<>)) .SingleInstance(); // Register over Autofac ContainerBuilder }); App = builder.Build(); var service = (AutofacServiceProvider)App.Services; this.Container = service.LifetimeScope; var myService = this.Container.Resolve<MyService>(); |
Login