gpt4 book ai didi

asp.net-mvc - .NET Core MVC项目中的Autofac属性注入(inject)

转载 作者:行者123 更新时间:2023-12-02 03:37:35 24 4
gpt4 key购买 nike

我有一个具有 ILol 类型属性的 Controller 和实现 ILolLol 类。

public class UniversityController : Controller
{
public ILol Lol { get; set; }

public IActionResult Index()
{
ViewData["Header"] = "Hello, world!";
ViewData["NullCheck"] = Lol == null ? "It's null" : Lol.GetLol();

return View();
}
}

我尝试以这种方式将属性注入(inject)Autofac结合使用(我的Startup类的一部分):

public IContainer ApplicationContainer { get; private set; }

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the 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;
});

// Dependency resolving.
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

ContainerBuilder builder = new ContainerBuilder();

builder.RegisterType<Lol>().As<ILol>().PropertiesAutowired().InstancePerLifetimeScope();
builder.Populate(services);

IContainer container = builder.Build();

ApplicationContainer = container;

return new AutofacServiceProvider(container);
}

它不起作用。该属性在运行时为空。尽管构造函数注入(inject)工作正常。

最佳答案

您必须指定哪些 Controller 需要使用属性注入(inject):

 var controllersTypesInAssembly = typeof(Startup).Assembly.GetExportedTypes()
.Where(type => typeof(ControllerBase).IsAssignableFrom(type)).ToArray();
builder.RegisterTypes(controllersTypesInAssembly).PropertiesAutowired();

并调用AddControllersAsServices():

services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddControllersAsServices();

如果您需要注册属性注入(inject),只需在构建器中注册更多类型 PropertiesAutowired() 即可。

ConfigureServices 将如下所示:

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

// Dependency resolving.
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddControllersAsServices();

ContainerBuilder builder = new ContainerBuilder();
builder.Populate(services);//Autofac.Extensions.DependencyInjection

builder.RegisterType<Lol>().As<ILol>().PropertiesAutowired().InstancePerLifetimeScope();
builder.Populate(services);

var controllersTypesInAssembly = typeof(Startup).Assembly.GetExportedTypes()
.Where(type => typeof(ControllerBase).IsAssignableFrom(type)).ToArray();
builder.RegisterTypes(controllersTypesInAssembly).PropertiesAutowired();

IContainer container = builder.Build();

ApplicationContainer = container;

return new AutofacServiceProvider(container);
}

关于asp.net-mvc - .NET Core MVC项目中的Autofac属性注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53981672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com