3

我正在使用 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());
            }
        }

引导程序.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");
            });
        }
    }
}
4

1 回答 1

5

您必须添加自己的 实现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
        });
    }
}

有关更多详细信息,请参见此处

或者返回使用旧版本的 StructureMap。

于 2016-05-12T20:51:07.627 回答