Karol Pisarzewski
Karol Pisarzewski

Reputation: 700

Net core 3.0 with Autofac throw IServiceProvider isn't supported

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

The exception: System.NotSupportedException: 'ConfigureServices returning an System.IServiceProvider isn't supported.'

The Program class code:

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

And the Startup class:

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

        }
    }

Upvotes: 3

Views: 4715

Answers (2)

Sadegh Tohidi
Sadegh Tohidi

Reputation: 21

you must change program.c

    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
       WebHost.CreateDefaultBuilder(args)
           .ConfigureLogging(options => options.ClearProviders())
           .UseStartup<Startup>();

Upvotes: 0

Nkosi
Nkosi

Reputation: 247591

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

Reference Autofac documentation for ASP.NET Core 3.0+

Upvotes: 4

Related Questions