3

I am using StructureMap to resolve dependency, which works fine with older version.But after updating StructureMap version 4.2.0.40, i am facing this error.

ObjectFactory is now obsoleted in new version. So how to modify below logic to fit this with updated version.

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");
            });
        }
    }
}
Dev
  • 295
  • 1
  • 4
  • 22

1 Answers1

5

You will have to add your own implementation of ObjectFactory, something like:

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
        });
    }
}

See here for more details.

Or alternatively go back to using an older version of StructureMap.

James P
  • 2,201
  • 2
  • 20
  • 30