StructureMap Exception Code: 202 No Default Instance defined for PluginFamily
Asked Answered
F

9

24

I am new to StructureMap. I have downloaded and am using version 2.6.1.0. I keep getting the below error:

StructureMap Exception Code: 202 No Default Instance defined for PluginFamily Company.ProjectCore.Core.IConfiguration, Company.ProjectCore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

My Global.asax.cs looks like:

protected void Application_Start(object sender, EventArgs e)
{

    var container = new Container(x =>
                    {
                        x.For<ICache>().Use<Cache>();
                        x.For<IEmailService>().Use<EmailService>();
                        x.For<IUserSession>().Use<UserSession>();
                        x.For<IRedirector>().Use<Redirector>();
                        x.For<INavigation>().Use<Navigation>();
                    });

                container.AssertConfigurationIsValid();

}

I changed from ObjectFactory.Initialize to "new Container" to debug. When stepping through the AssertConfigurationIsValid() method, Cache works but EmailService fails at the GetInstance method in the following line:

[Pluggable("Default")]
public class EmailService : IEmailService

private readonly IConfiguration _configuration;

public EmailService()
{
    _configuration = ObjectFactory.GetInstance<IConfiguration>();
}

If I remove IEmailService, the same 202 error is thrown at IUserSession.

Should I be adding something else in Application_Start or in my class files?

Thanks in advance...

Fascicule answered 26/5, 2010 at 6:19 Comment(1)
What application layer does StructureMap belong to? UI, Business, or Data layer?Ponton
F
23

This problem was fixed by replacing ObjectFactory.Initialize with ObjectFactory.Configure and adding the assemblies in my project:

ObjectFactory.Configure(x =>
{
    x.Scan(scan =>
    {
        scan.LookForRegistries();
        scan.Assembly("MyAssembly");
        scan.Assembly("MyAssembly");
    });
});
Fascicule answered 26/5, 2010 at 15:32 Comment(2)
Thanks for that. It work good. Can u mark it as answer please. Because its easy for the reader.Drone
I had the same error message. The reason for my issue was that StructureMap couldn't find the implementation of the interface because of the Default ISomething/Something Convention (refer to structuremap.github.io/registration/…). Once I change the implmentation name to be matched with interface name, it worked.Luzon
W
15

I was getting the same error message, but for a different reason. I had a class Foo that defined two constructors like so:

public class Foo : IFoo
{
    private Bar _bar;

    public Foo()
    {
       _bar = new Bar();
    }

    public Foo(Bar bar)
    {
        _bar = bar;
    }
}

and my StructureMap configuration was like so:

For<IFoo>.Use<Foo>();

I kept getting an error message like

202 No Default Instance defined for Bar

The problem was that StructureMap was trying to construct a Foo using the constructor that takes a parameter, instead of using the parameterless default constructor. I solved it using the answer in How to define a default constructor by code using StructureMap? like so:

For<IFoo>.Use(() => new Foo());
Wilek answered 25/7, 2012 at 20:53 Comment(0)
L
6

Where's your bootstrapping for the IConfiguration concrete class?

I.e:

x.For<IConfiguration>().Use<Configuration>();
Latinize answered 26/5, 2010 at 6:40 Comment(2)
Yeah.. I tried adding that and it doesn't seem to matter. When it is there, it is never hit. When I comment out all other classes, the compiler acts as if it is not there and just goes to the next block of code. Also, as I pointed out before, if I take out IEmailService (where IConfiguration is called) the next interface - IUserSession - gets called and I get the 202 error again. So, I don't think the answer to my problem is how I am bootstrapping IConfiguration.Fascicule
The answer to your problem is certainly how you are bootstrapping. StructureMap error 202 means you haven't told StructureMap how to resolve a dependency (in your bootstrapping). In this specific case, you never told it how to resolve an IConfiguration.Container
P
3

I was seeing the same error. In my case, I had a typo in the implementation name, so the interface and implementation names did not match.

public class FooTypo : IFoo

Where I should have had:

public class Foo : IFoo
Pedigo answered 4/8, 2014 at 23:42 Comment(0)
L
3

Another thing to look for is to make sure that the dependency (class) that you are injecting is public. If the class is internal, it can cause this issue.

Lucas answered 22/11, 2016 at 22:41 Comment(1)
My class constructor was 'protected', changing to public did the trick.Feudality
S
1

Mine was because I had accidentally referenced the concrete type in the constructor of another class rather than the interface

eg in structuremap i had

x.For<IFoo>().Use<Foo>();
x.For<IBar>().Use<Bar>();

and constructor for Foo looked like this

public class Foo : IFoo {
    public Foo(Bar bar) {...}
}

when it should have looked like this as SM couldn't just create an instance of Bar, it only knows how to make an IBar:

public class Foo : IFoo {
    public Foo(IBar bar) {...}
}

It also meant that my exception referenced the concrete type instead of the interface but I missed that initially

Sorcerer answered 1/1, 2020 at 18:38 Comment(0)
G
0

When I got this error it was because I forgot to mark my class public. That simple.

Geognosy answered 26/2, 2015 at 0:28 Comment(0)
M
0

I also had this issue when I was using Visual Studio 2015 with NCrunch. All you have to do is toggle an option to true in the configuration menu item under NCrunch. Switching initialize to configure didn't work for me.

The option is under Build Settings, it is named 'Copy referenced assemblies to workspace'

Medwin answered 5/1, 2016 at 9:16 Comment(0)
E
0

I was seeing the same error. In my case, I had a typo in the implementation with the same name of another class in another project.

Endocrinotherapy answered 7/2, 2022 at 19:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.