StructureMap Exception Code: 202 No Default Instan

2019-01-26 03:33发布

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...

7条回答
forever°为你锁心
2楼-- · 2019-01-26 04:23

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());
查看更多
登录 后发表回答