不支持带有Autofac throw IServiceProvider的Ne​​t Core 3.0

时间:2019-09-09 12:32:42

标签: c# dependency-injection .net-core autofac

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

  

例外:       System.NotSupportedException:'不支持返回System.IServiceProvider的ConfigureServices。'

程序类代码:

     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());

        }
    }

1 个答案:

答案 0 :(得分:3)

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

相反,在Startup中执行以下操作

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));
}

ASP.NET Core 3.0及更高版本的参考文献Autofac documentation