1

I'm using StructureMap on WCF service and MVC 4 application, I have configured it on both of them, but once I run the application I receive the following exception:

StructureMap Exception Code: 202 No Default Instance defined for PluginFamily LookupsRepositories.Lookups.ILookupRepository`1[[JE.Domain.Lookups.Status, JE.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], LookupsRepositories, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

The LookupRepository is an abstract class, a generic class, this is how I register it:

For(typeof(ILookupRepository<>)).Use(typeof(LookupRepository<>));
For<ILookupUnitOfWork>().Use<LookupUnitOfWork>();
Scan(s =>
        {
            s.AssemblyContainingType(typeof(LookupRepository<>));
            s.ConnectImplementationsToTypesClosing(typeof(ILookupRepository<>));
        });

Calling the registry in Global.asax:

protected void Application_Start(object sender, EventArgs e)
{
    ObjectFactory.Initialize(x => x.AddRegistry(new JedcoRegistry()));
}

Still I got the exception. Any idea why?

Note: StructureMap version 2.6.4.0

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
SVI
  • 921
  • 4
  • 11
  • 23

1 Answers1

0

The answer here relates to the information:

The LookupRepository is an abstract class

So, to make it running we have to be sure, there is:

// abstract base
public class LookupRepository<T> : ILookupRepository<T>
// implementation
public class StatusRepository : LookupRepository<Status> { ... }

...
// mapping
// do not use this
// r.For(typeof(ILookupRepository<>)).Use(typeof(LookupRepository<>));
// just this
r.Scan(s =>
{
    s.AssemblyContainingType(typeof(LookupRepository<>));
    s.ConnectImplementationsToTypesClosing(typeof(ILookupRepository<>));
});


...
// init - will return new StatusRepository();
ObjectFactory.GetInstance<ILookupRepository<Status>>(); 

If the base class won't be abstract, this will work as well:

// non abstract
public class LookupRepository<T> : ILookupRepository<T>

...
// mapping
r.For(typeof(ILookupRepository<>)).Use(typeof(LookupRepository<>));

...
// init - will return new LookupRepository<Status>();
ObjectFactory.GetInstance<ILookupRepository<Status>>(); 
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335