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

asp.net-web-api – 如何在StructureMap ServiceActivator中使用

发布时间:2020-12-15 19:15:26 所属栏目:asp.Net 来源:网络整理
导读:在使用StructureMap在WebAPI中实现DI时,我们使用了在中的ServiceActivator Configuring Dependency Injection with ASP.NET WebAPI 2.1 WebAPI + APIController with structureMap public class ServiceActivator : IHttpControllerActivator{ public Servi
在使用StructureMap在WebAPI中实现DI时,我们使用了在中的ServiceActivator

> Configuring Dependency Injection with ASP.NET WebAPI 2.1
> WebAPI + APIController with structureMap

public class ServiceActivator : IHttpControllerActivator
{
    public ServiceActivator(HttpConfiguration configuration) {}    

    public IHttpController Create(HttpRequestMessage request,HttpControllerDescriptor controllerDescriptor,Type controllerType)
    {
        var controller = ObjectFactory.GetInstance(controllerType) as IHttpController;
        return controller;
    }
}

但现在有了新的StructureMap,我的ReSharper建议:

Class ‘StructureMap.ObjectFactory’ is obsolete: ObjectFactory will be removed in a future 4.0 release of StructureMap. Favor the usage of the Container class for future work

集装箱上的智能感觉给我的信息非常有限。

我们应该如何用Container类重写我们的ServiceActivator?

解决方法

该 static stuff is going away.如果不使用某种类型的服务定位器,你将不得不实现自己“的ObjectFactory”为 referenced here:
public static class ObjectFactory
{
    private static readonly Lazy<Container> _containerBuilder =
            new Lazy<Container>(defaultContainer,LazyThreadSafetyMode.ExecutionAndPublication);

    public static IContainer Container
    {
       get { return _containerBuilder.Value; }
    }

     private static Container defaultContainer()
     {
        return new Container(x =>
        {
               // default config
         });
     }
}

更新:我以前的答案是错误的。感谢@JoeMighty的头。

(编辑:李大同)

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

    推荐文章
      热点阅读