2

I'm getting this error:

StructureMap Exception Code:  202
No Default Instance defined for PluginFamily 

My setup looks like:

Console.WriteLine("Structure Map");
SetupSM sm = new SetupSM();
sm.Setup();

ISomeThing someThing = ObjectFactory.GetInstance<ISomeThing>();
Console.WriteLine("something.HowManyTHings: " + someThing.HowManyThings("asdf"));



 public class SetupSM
    {
        public void Setup()
        {
            var c1 = new Container(config =>
            {
                config.Scan(scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                });
            });

            var c2 = new Container(x =>
                                              {
                                                  x.For<ISomeThing>().Use<SomeThingOne>();
                                              });


        }
    }

This is my first try at using structure map, what am I missing? It seems the guide on their main website is very old using the old syntax etc.

2
  • "It seems the guide on their main website is very old using the old syntax etc." StructureMap is kind of notorious for this. Commented Jul 21, 2011 at 14:34
  • You're using ObjectFactory to get the instance but you're configuring 2 entirely seperate containers. Try ObjectFactory.Configure Commented Jul 21, 2011 at 14:38

3 Answers 3

5

Try applying your configuration to the static ObjectFactory instead of seperate containers which you appear to throw away immediately..

public class SetupSM
    {
        public void Setup()
        {
            ObjectFactory.Configure(config =>
            { 
                config.Scan(scan =>
                {
                    scan.TheCallingAssembly();
                    scan.WithDefaultConventions();
                });

                config.For<ISomething>().Use<SomeThingOne>();
            });
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that did it! So in a web application, I would just make this call to Setup() in my application begin request in global.asax file?
5

The previous answers are for StructureMap 2 or 3, but in StructureMap 4 the syntax has changed. You can see a complete solution here:

http://ardalis.com/using-structuremap-4-in-a-console-app

Essentially, ObjectFactory has been replaced with a Container instance. You can also optionally perform the configuration in a separate Registry instance, which is recommended. Thus your initialization code in Main() becomes:

static void Main(string[] args)
{
    var container = Container.For<ConsoleRegistry>();

    var app = container.GetInstance<Application>();
    app.Run();
    Console.ReadLine();
}

This keeps Main very clean, and allows Application to request any dependencies it needs through dependency injection and to follow the Explicit Dependencies Principle. Combined, these allow your application to be very composable and testable.

1 Comment

What about the Application class? We get it from the container, but never explicitly register it. Does its registration get handled by StructureMap's "Default Conventions"?
1
public static class StructureMapBootStrapper
{
    public static void BootStrap()
    {
        StructureMap.ObjectFactory.Initialize(
            bootStrapper =>
            {
                bootStrapper.For<ISomeThing>().Use<SomeThingOne>();
            });
    }
}

Your console app:

Console.WriteLine("Structure Map");
StructureMapBootStrapper.BootStrap();

ISomeThing someThing = ObjectFactory.GetInstance<ISomeThing>();
Console.WriteLine("something.HowManyTHings: " + someThing.HowManyThings("asdf"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.