加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Autofac懒惰属性注入

发布时间:2020-12-16 00:08:54 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试将业务逻辑实现注入Web API基础控制器.基本控制器中的某些属性始终为null. 另外我怎么做懒人注射? Startups.cs public IServiceProvider ConfigureServices(IServiceCollection services){ // Add framework services. services.AddMvc(); var co
我正在尝试将业务逻辑实现注入Web API基础控制器.基本控制器中的某些属性始终为null.

另外我怎么做懒人注射?

Startups.cs

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    var containerBuilder = new ContainerBuilder();
    containerBuilder.RegisterType<ViewBusinessLogic>().As<IViewBusinessLogic>().
        PropertiesAutowired();
    containerBuilder.Populate(services);

    var container = containerBuilder.Build();

    return container.Resolve<IServiceProvider>();
}

接口,实现和基本控制器:

public interface IViewBusinessLogic
{
    IEnumerable<dynamic> GetView(Guid viewId);
}

public class ViewBusinessLogic : BusinessLogic,IViewBusinessLogic
{
    public IEnumerable<dynamic> GetView(Guid viewId)
    {
        return new List<dynamic>
        {
            new { Test = "Test1" },new { Test = "Test2" }
        };
    }
}

public abstract class BaseController : Controller
{
    public IViewBusinessLogic ViewBusinessLogic { get; }
}

解决方法

默认情况下,DI框架不会解析控制器.您需要添加AddControllerAsServices才能通过您选择的DI解析它们.

从this GitHub issue开始:

Hi,

Maybe I’m wrong but as I tested deeply (and checked Mvc source code),Controllers are not resolved from IServiceProvider,but only constructor arguments of them are resolved from IServiceProvider.

Is that by design? I’m very suprised. Because,I’m using a different DI framework which supports property injection. And I can not use property injection since Controller instances are not requested from IServiceProvider.


您是否在Startup中添加了AddControllersAsServices(https://github.com/aspnet/Mvc/blob/ab76f743f4ee537939b69bdb9f79bfca35398545/test/WebSites/ControllersFromServicesWebSite/Startup.cs#L37)

以上示例引用以供将来参考.

public void ConfigureServices(IServiceCollection services)
{
    var builder = services
        .AddMvc()
        .ConfigureApplicationPartManager(manager => manager.ApplicationParts.Clear())
        .AddApplicationPart(typeof(TimeScheduleController).GetTypeInfo().Assembly)
        .ConfigureApplicationPartManager(manager =>
        {
            manager.ApplicationParts.Add(new TypesPart(
              typeof(AnotherController),typeof(ComponentFromServicesViewComponent),typeof(InServicesTagHelper)));

            manager.FeatureProviders.Add(new AssemblyMetadataReferenceFeatureProvider());
        })

        // This here is important
        .AddControllersAsServices()
        .AddViewComponentsAsServices()
        .AddTagHelpersAsServices();

    services.AddTransient<QueryValueService>();
    services.AddTransient<ValueService>();
    services.AddSingleton<IHttpContextAccessor,HttpContextAccessor>();
}

至于你问题的第二部分:我认为根本不可能通过IoC容器进行延迟实例化.最适合您的是创建工厂类并注入工厂而不是具体服务.

但通常你不需要懒惰的实例化,服务的实例化应该很快.如果不是,你可能在构造函数中做一些时髦的东西(连接某个地方,或做其他长时间运行的操作),这是一种反模式.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读