gpt4 book ai didi

asp.net-web-api - 如何在 StructureMap ServiceActivator 中使用 Container 而不是 ObjectFactory?

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

当使用 StructureMap 在 WebAPI 中实现 DI 时,我们使用了

中的 ServiceActivator <小时/>
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 上的智能感知只给了我非常有限的信息。

我们应该如何用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 的提醒。

关于asp.net-web-api - 如何在 StructureMap ServiceActivator 中使用 Container 而不是 ObjectFactory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25550914/

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