本文介绍了不支持带有Autofac throw IServiceProvider的Ne​​t Core 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试解决问题时遇到了一些麻烦,我将Autofac与.net core 3.0-6preview一起使用。
我将 new AutofacServiceProviderFactory()添加到 CreateHostBuilder中,在此.net核心版本框架中这是必需的。
代码在2.1版及更低版本中正常运行,但现在应用程序崩溃了。

I have some trouble i try to resolve problem i use Autofac with .net core 3.0-6preview.I add new AutofacServiceProviderFactory() to CreateHostBuilder which is require in this .net core version framework.The code was working correctly in version 2.1 and lower but now application was crashed

程序类代码:

     public class Program
    {
        public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

启动类:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public IContainer ApplicationContainer { get; private set; }


        // This method gets called by the runtime. Use this method to add services to the container.
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var builder = new ContainerBuilder();
            builder.Populate(services);
            builder.RegisterModule(new ContainerModule(Configuration));
            ApplicationContainer = builder.Build();
            return new AutofacServiceProvider(ApplicationContainer);
        }



        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IApplicationLifetime appLifetime)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });


            var jwtSettings = app.ApplicationServices.GetService<JwtSettings>();
            var generalSettings = app.ApplicationServices.GetService<GeneralSettings>();
            if (generalSettings.SeedData)
            {
                var dataInitializer = app.ApplicationServices.GetService<IDataInitializer>();
                dataInitializer.SeedAsync();
            }

            //           app.UseMvc();
            appLifetime.ApplicationStopped.Register(() => ApplicationContainer.Dispose());

        }
    }


推荐答案

用于配置Autofac的启动语法已更改。

Startup syntax has changed for configuring Autofac.

相反,在启动中执行以下操作

Instead, in Startup do the following

public void ConfigureServices(IServiceCollection services) {
    //... normal registration here

    // Add services to the collection. Don't build or return
    // any IServiceProvider or the ConfigureContainer method
    // won't get called.
}

public void ConfigureContainer(ContainerBuilder builder) {
    //configure auto fac here
    builder.RegisterModule(new ContainerModule(Configuration));
}

参考

这篇关于不支持带有Autofac throw IServiceProvider的Ne​​t Core 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 21:35