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条回答
迷人小祖宗
2楼-- · 2019-01-26 03:58

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

查看更多
狗以群分
3楼-- · 2019-01-26 04:04

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");
    });
});
查看更多
何必那么认真
4楼-- · 2019-01-26 04:04

Where's your bootstrapping for the IConfiguration concrete class?

I.e:

x.For<IConfiguration>().Use<Configuration>();
查看更多
等我变得足够好
5楼-- · 2019-01-26 04:13

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.

查看更多
倾城 Initia
6楼-- · 2019-01-26 04:13

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'

查看更多
SAY GOODBYE
7楼-- · 2019-01-26 04:15

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