之前看过一个帖子,他用的.net core2.2给出两种方法,我整理到了下边,自己写了一下,发现两种写法在.net core 3.0中都不支持,然后查了一些资料在最下边整理出来3.0的写法 欢迎学习交流
1.环境: .net core 2.2
nuget 安装:
Autofac
Autofac.Extensions.DependencyInjection
第一种方法:
新建类:AutofacModule
using Autofac; using IService2; using Service2; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace WebApplication2 { public class AutofacModule: Module { protected override void Load(ContainerBuilder builder) { builder.RegisterType<UserInfoService>().As<IUserInfoService>(); builder.RegisterType<TestService>().As<ITestService>(); } } }
Startup类中: 返回 IServiceProvider
public IServiceProvider ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); // Add Autofac 第一种方式,使用AutofacModule配置文件 var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterModule<AutofacModule>(); containerBuilder.Populate(services); var container = containerBuilder.Build(); return new AutofacServiceProvider(container); }
第二种方法:
public IServiceProvider ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);// Add Autofac 第二种方式 自动注册该程序集下的所有接口 var containerBuilder = new ContainerBuilder(); //自动注册该程序集下的所有接口 //Service2 为程序集命名空间 服务层非接口层 //InstancePerLifetimeScope:同一个Lifetime生成的对象是同一个实例 //SingleInstance:单例模式,每次调用,都会使用同一个实例化的对象;每次都用同一个对象; //InstancePerDependency:默认模式,每次调用,都会重新实例化对象;每次请求都创建一个新的对象; containerBuilder.RegisterAssemblyTypes(Assembly.Load("Service2")).AsImplementedInterfaces().InstancePerLifetimeScope(); containerBuilder.Populate(services); var container = containerBuilder.Build(); return new AutofacServiceProvider(container); }
2.环境: .net core 3.0 preview5
nuget 安装:
Autofac
Autofac.Extensions.DependencyInjection
Program类中
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) //Wire up autofac .UseServiceProviderFactory(new AutofacServiceProviderFactory()); }
Startup类中
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; }); services.AddControllersWithViews() .AddNewtonsoftJson(); services.AddRazorPages(); } public void ConfigureContainer(ContainerBuilder containerBuilder) { //wire up using autofac specific APIs here containerBuilder.RegisterType<UserInfoService>().As<IUserInfoService>().InstancePerLifetimeScope(); containerBuilder.RegisterType<TestService>().As<ITestService>().InstancePerLifetimeScope(); }