StructureMap Exception Code: 202 No Default Instance defined for PluginFamily

我怕爱的太早我们不能终老 提交于 2019-11-29 09:08:12

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");
    });
});

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());

Where's your bootstrapping for the IConfiguration concrete class?

I.e:

x.For<IConfiguration>().Use<Configuration>();

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

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.

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

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'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!