3

I am getting "Cannot Resolve Symbol Error" for StructureMap ObjectFactory.TryGetInstance

But ObjectFactory.GetInstance is okay.

StructureMap Version 3. Assembly include is "Using StructureMap;" I am using this in an MVC 5 project.

Missing any other includes?'

Blue Clouds
  • 7,295
  • 4
  • 71
  • 112

4 Answers4

5

ObjectFactory.Container.TryGetInstance is even better

Narcis
  • 76
  • 4
4

ObjectFactory.Container.Try/GetInstance() is now replaced by creating a Container instance and using the methods from it. ObjectFactory was an encapsulation of Container anyway, from what I read.

    public object GetService(Type serviceType)
    {
        // Previous way
        return serviceType.IsAbstract || serviceType.IsInterface ?
            ObjectFactory.Container.TryGetInstance(serviceType) :
            ObjectFactory.Container.GetInstance(serviceType);

        // New way
        Container container = new Container();
        return serviceType.IsAbstract || serviceType.IsInterface ?
            container.TryGetInstance(serviceType) :
            container.GetInstance(serviceType);
    }

Source: https://groups.google.com/forum/#!topic/structuremap-users/S7nBib95zh0

VictorySaber
  • 3,084
  • 1
  • 27
  • 45
1

ObjectFactory.Container.GetInstance resolved this.

Blue Clouds
  • 7,295
  • 4
  • 71
  • 112
0

As Vahid N.'s answer may be useful in discussion :

My version, produces a lazy loaded thread safe singleton, but your Initialize method is not thread safe. Try:

public static class StructureMapObjectFactory
{
    private static readonly Lazy<Container> _containerBuilder = new Lazy<Container>(() => new Container(), LazyThreadSafetyMode.ExecutionAndPublication);

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

    public static void Initialize<T>() where T : Registry, new()
    {
        Container.Configure(x =>
        {
            x.AddRegistry<T>();
        });
    }
}

And how to use :

DbContext dataContext = StructureMapObjectFactory.Container.TryGetInstance<DbContext>();
Serkan Yılmaz
  • 309
  • 5
  • 12