依赖注入小伙伴们比较常了,这里只说core 下autofac依赖注入的使用 ,不多费话,直接代码。
在 Startup.cs里
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; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }
如果想用官方提供的注入方法,请自行了解官方方法。
如果使用autofac的话 需要修改上面的方法。
首先去NUGET上引入以下包
修改方法
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); var builder = new ContainerBuilder(); builder.Populate(services); //把官方方法的注入移植到autofac了 var container = builder.Build(); return new AutofacServiceProvider(container); }