views:

398

answers:

2

Hi.

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

A: 

Where's your bootstrapping for the IConfiguration concrete class?

I.e:

x.For<IConfiguration>().Use<Configuration>();
RPM1984
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.
Code Sherpa
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.
Joshua Flanagan
A: 

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

   ObjectFactory.Configure(x =>
        {   x.Scan(scan =>
            {
                scan.LookForRegistries();
                scan.Assembly("MyAssembly");
                scan.Assembly("MyAssembly");
            });
        });

I hope this helps somebody...

Code Sherpa