Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StructureMap exception: No Default Instance defined for PluginFamily

Tags:

structuremap

I have a SysMsgManager class defined in CoreService project as following:

public class SysMsgManager 
{
    private ISysMsgRepository _SysMsgRepository;

    public SysMsgManager()
    {
        _SysMsgRepository = ObjectFactory.GetInstance<ISysMsgRepository>();
    }

    ....
}

In my DataAccess project I have 'ISysMsgRepository' interface and two concrete implementations defined as following:

namespace DataAccess.Repository
{
   [Pluggable("Default")]
   public class SysMsgRepository : ISysMsgRepository
   {
      ...
   }
}

namespace DataAccess.Repository
{
    [Pluggable("Stub")]
    public class SysMsgRepository_Test : ISysMsgRepository
    {
        ...
    }
}

and this is what I have in my StructureMap.config file

<StructureMap>

<Assembly Name="CoreService" /> 
<Assembly Name="DataAccess" />

<PluginFamily
    Assembly="DataAccess"
    Type="DataAccess.Repository.ISysMsgRepository"
    DefaultKey="Default" />

</StructureMap>

When I try to run my app, I got the following error:

StructureMap Exception Code: 202\nNo Default Instance defined for PluginFamily DataAccess.Repository.ISysMsgRepository, DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Can anyone help me to solve this problem? Thanks!

like image 339
sean717 Avatar asked Feb 18 '10 01:02

sean717


1 Answers

Unfortunately I have little familiarity with configuring StructureMap via Xml. Let me show you how it is done using C#.

var container = new Container(config=>
{
  config.For<ISysMsgRepository>().Use<SysMsgRepository>();
});

It seems you are using the standard naming convention for your interfaces and classes (just tacking an I onto the front of the class name). If you do that for all your types you can just configure your container like this:

var container = new Container(config=>
{
    config.Scan(scan =>
    {
        scan.TheCallingAssembly();
        scan.WithDefaultConventions();
    });
});

I hope this helps. It is much easier to configure your container using code rather than Xml. Give it a try. You'll be a convert.

like image 74
KevM Avatar answered Sep 21 '22 05:09

KevM