ASP.Net Core 3.1 With Autofac ConfigureServices returning an System.IServiceProvider isn't supported.

前言

Autofac在ASP.Net Core3.0以后,集成方式有所调整。在ASP.Net Core2中我们一般是把StartupConfigureServices方法返回值类型改为IServiceProvider。我们可以先看一下部分代码:

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {

            services.AddMvc();
			//xxxxxx你的其他代码 省略...........
            //用Autofac来替换IOC容器 返回值由 void 修改为 IServiceProvider
            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterModule<CustomAutofacModule>();
            containerBuilder.Populate(services);
            var container = containerBuilder.Build();
            //将当前的容器保存起来,用于后续使用
            AutofacHelper.Container = container;
            return new AutofacServiceProvider(container);
        }

我们把这段代码Copy到ASP.Net Core3.1的项目中启动:

就是说,我们不能再这样修改返回值类型,

那我们按照官方文档的方法修改一下。

建立ASP.Net Core3.1 Web项目

效果图

ASP.Net Core 3.1 With Autofac ConfigureServices returning an System.IServiceProvider isn&#39;t supported.-LMLPHP

Services

创建完成后我们先创建文件夹Services,用来测试使用。

Services文件夹下创建Iservice

namespace DotNetCore3AutoFac.Services
{
    public interface IService
    {
        string SayHi();
    }
}

然后创建两个Iservice的实现类OneServiceTwoService

namespace DotNetCore3AutoFac.Services
{
    public class OneService:IService
    {
        public string SayHi()
        {
            return "One Say Hi";
        }
    }
}
namespace DotNetCore3AutoFac.Services
{
    public class TwoService:IService
    {
        public string SayHi()
        {
            return "Two Say Hi";
        }
    }
}

Autofac

创建Autofac文件夹,在该文件夹下创建CustomAutofacModule

using Autofac;
using DotNetCore3AutoFac.Services;

namespace DotNetCore3AutoFac.Autofac
{
    public class CustomAutofacModule : Module
    {
        /// <summary>
        /// AutoFac注册类
        /// </summary>
        /// <param name="builder"></param>
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<OneService>().As<IService>();
        }
    }
}

我们先用OneService去实现,当然,正常这里会有很多很多的代码~

修改StartupProgram

前序工作都完成了,我们来改最重要的,首先修改我们的Program,增加UseServiceProviderFactory(new AutofacServiceProviderFactory())

namespace DotNetCore3AutoFac
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            	//改用Autofac来实现依赖注入
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

然后我们修改Startup

using Autofac;
using Autofac.Extensions.DependencyInjection;
using DotNetCore3AutoFac.Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

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

        public IConfiguration Configuration { get; }
		//autofac 新增
        public ILifetimeScope AutofacContainer { get; private set; }


        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
        }

		//autofac 新增
        public void ConfigureContainer(ContainerBuilder builder)
        {
            // 直接用Autofac注册我们自定义的
            builder.RegisterModule(new CustomAutofacModule());
        }


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
			//autofac 新增 可选
            this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();


            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

验证

我们修改下HomeController

ASP.Net Core 3.1 With Autofac ConfigureServices returning an System.IServiceProvider isn&#39;t supported.-LMLPHP

运行,断点看下结果是否正确

ASP.Net Core 3.1 With Autofac ConfigureServices returning an System.IServiceProvider isn&#39;t supported.-LMLPHP

修改下实现类,也是正确的~

ASP.Net Core 3.1 With Autofac ConfigureServices returning an System.IServiceProvider isn&#39;t supported.-LMLPHP

总结

其实很多官方文档都讲述了,因为自己近期打算把原来的项目LJD.APP:https://github.com/jellydong/LJDAPP升级到.Net Core 3.1上,当然是一个简化版本https://github.com/jellydong/Jelly.Simple。

所以不要怕英文,翻译插件那么好用,基本都能看懂的

当然Autofac的应用远不止这些,还有一个比原来好的就是可以实现AOP~

ASP.Net Core 3.1 With Autofac ConfigureServices returning an System.IServiceProvider isn&#39;t supported.-LMLPHP

07-18 03:32