我正在使用 StructureMap 来解决依赖关系,这在旧版本上运行良好。但是在更新 StructureMap 版本 4.2.0.40 之后,我遇到了这个错误。
ObjectFactory 现在在新版本中已经过时。 那么如何修改以下逻辑以适应更新版本。
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
try
{
if ((requestContext == null) || (controllerType == null))
return null;
return (Controller)ObjectFactory.GetInstance(controllerType);
}
catch (StructureMapException)
{
System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
throw new Exception(ObjectFactory.WhatDoIHave());
}
}
Bootstrapper.cs
public static class Bootstrapper
{
public static void Run()
{
ControllerBuilder.Current
.SetControllerFactory(new StructureMapControllerFactory());
ObjectFactory.Initialize(x =>
{
x.AddConfigurationFromXmlFile(@"D:\Samples\Web_API\OneCode\StructureMap.Web\StructureMap.Web\StructureMap.xml");
});
}
}
}
最佳答案
您必须添加自己的 ObjectFactory
实现,例如:
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
});
}
}
参见 here更多细节。
或者返回使用旧版本的 StructureMap。
关于asp.net-mvc - StructureMap:名称 'ObjectFactory' 在当前上下文中不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37196452/