请求的插件类型没有默认实例或命名实例“默认" [英] No default instance or named instance 'Default' for requested plugin type

查看:31
本文介绍了请求的插件类型没有默认实例或命名实例“默认"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图避免在我的主项目中引用具体类型库,但我收到此错误:

I'm trying to avoid referencing the concrete type library in my main project, but I'm getting this error:

No default instance or named instance 'Default' for requested plugin type StackExchangeChatInterfaces.IClient
1.) Container.GetInstance(StackExchangeChatInterfaces.IClient ,{username=; password=; defaultRoomUrl=; System.Action`2[System.Object,System.Object]=System.Action`2[System.Object,System.Object]})

我已将容器设置为扫描程序集,如下所示:

I've setup my container to scan for assemblies, like so:

        var container = new Container(x =>
        {
            x.Scan(scan =>
            {
                scan.AssembliesFromApplicationBaseDirectory();
                scan.ExcludeNamespace("StructureMap");
                scan.WithDefaultConventions();
                scan.AddAllTypesOf<IMessageHandlers>();
            });
            //x.For<IClient>().Use<Client>(); //GetInstance will work if this line is not commented out.
        });

当我尝试获取实例时,出现错误,获取实例的代码在这里:

When I try to get an instance, I get the error, my code for getting an instance is here:

chatInterface = container
    .With("username").EqualTo(username)
    .With("password").EqualTo(password)
    .With("defaultRoomUrl").EqualTo(roomUrl)
    .With<Action<object, object>>(delegate(object sender, object messageWrapper)
        {
            string message = ((dynamic)messageWrapper).Message;
            Console.WriteLine("");
            Console.WriteLine(message);
            foreach (var item in messageHandlers)
            {
                item.MessageHandler.Invoke(message, chatInterface);
            }                        
        }).GetInstance<IClient>();

如果我将具体类显式映射到接口,则一切正常,但这意味着我需要引用 Client 所在的项目,而我不想这样做.

If I explicitly map the concrete class to the interface, everything works hunky dory, but that means I need to reference the project that Client is in, which I don't want to do.

推荐答案

这真的很有趣.看起来默认约定无法使用此类构造函数注册类型(在 2.6.3 和 3+ 版本上都尝试过).我只在指定无参数构造函数时才注册.查看这两个版本的来源,它真的很可疑,因为它应该被注册.需要更深入地研究代码...

This is really interesting. Looks like default conventions are not able to register types with such constructor (tried on both versions 2.6.3 and 3+). I was only registered when only parameterless constructor was specified. Looking at sources of both versions it is really suspicious as it should be registered. Deeper dive into the code would be needed...

无论如何尝试使用自定义注册约定:

Anyway try using custom registration convention:

public class ClientConvention : IRegistrationConvention
{
    public void Process(Type type, Registry registry)
    {
        if (type.IsClass && !type.IsAbstract && !type.IsGenericType &&
            type.GetInterfaces().Contains(typeof(IClient)))
        {
            registry.For(typeof(IClient)).Use(type);
        }
    }
}

这样配置:

    var container = new Container(
            c => c.Scan(
                s =>
                    {
                         s.ExcludeNamespace("StructureMap");
                         s.WithDefaultConventions();
                         s.Convention<ClientConvention>();
                         s.AddAllTypesOf<IMessageHandlers>();
                    }));

这应该可以正常工作.

这篇关于请求的插件类型没有默认实例或命名实例“默认"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆