Autofac 无法解析我注册的通用服务 [英] Autofac cannot resolve my Registered generic service

查看:34
本文介绍了Autofac 无法解析我注册的通用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Autofac 中注册了我的通用接口,但是在我解决它时抛出了异常.

I've registered my generic interface in Autofac, but as I resolve it exception is thrown.

Autofac.Core.Registration.ComponentNotRegisteredException: 请求的服务 'MyCLI.Command.ICommandHandler`1[[MyCLI.Command.ICommand, MyCLI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' 没有已注册.

Autofac.Core.Registration.ComponentNotRegisteredException: The requested service 'MyCLI.Command.ICommandHandler`1[[MyCLI.Command.ICommand, MyCLI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' has not been registered.

为避免此异常,请注册一个组件以提供服务,使用 IsRegistered() 检查服务注册,或使用 ResolveOptional() 方法来解析可选依赖项.

To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.

程序.cs

    static void Main(string[] args)
    {
        ContainerBuilder builder = new ContainerBuilder();

        var container = builder.RegisterTypes();
        var invoker = new Invoker(container);
        var command = TypeHelper.GetCommandByDescriptor("LS");
        invoker.Dispatch(command);

        Console.Read();
    }

服务注册

    public static IContainer RegisterTypes(this ContainerBuilder builder)
    {
        builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
            .AsClosedTypesOf(typeof (ICommandHandler<>)).AsImplementedInterfaces();
        return builder.Build();
    }

解决服务

public class Invoker : IInvoker
{
    private readonly IContainer container;
    public Invoker(IContainer container)
    {
        this.container = container;
    }

    public void Dispatch<T>(T command) where T : ICommand
    {
        //if (!container.IsRegistered(typeof(ICommandHandler<>))) return;
        var candidate = container.Resolve<ICommandHandler<T>>();
        candidate.Execute(command);
    }
}

GetCommandByDescriptor

    public static ICommand GetCommandByDescriptor(string descriptor)
    {
        var classes = GetAllCommands();
        var command = classes.First(x => x.GetType()
                        .GetCustomAttributes<CommandDescriptorAttribute>().First().CommandName.Equals(descriptor, StringComparison.OrdinalIgnoreCase));
        return command;
    }

推荐答案

我得到了解决方案,感谢 @Nkosi.当我从 GetCommandByDescriptor(string descriptor) 返回 ICommand 时,Dispatch Method 中的类型 T 将来自 ICommand 类型实际上没有注册,我应该返回一个已经实现了 ICommand 例如的类型ListOfDirectoryCommand.

I got the solution, thank to @Nkosi. As I return ICommand from GetCommandByDescriptor(string descriptor) the type T in Dispatch Method would be from ICommand type which is actually not registered, I should return a type which have implemented ICommand e.g. ListOfDirectoryCommand.

我也是这样:

invoker.Dispatch((dynamic)command);

所以command的类型将在运行时指定.

So the type of command would be specified in run time.

这篇关于Autofac 无法解析我注册的通用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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