3

我试图在我的命令处理程序上拦截对 Handle 方法的调用。当我显式注册每个命令处理程序时,此过程工作正常,问题是我对命令处理程序和拦截器的通用注册不正确。

例外:

Castle.Windsor.dll 中出现“Castle.MicroKernel.ComponentActivator.ComponentActivatorException”类型的异常,但未在用户代码中处理

附加信息:ComponentActivator:无法代理 TempSearch.Command.Data.CommandHandlers.AddTempsJobCommandHandler

看起来它找不到拦截器,因为它说某些组件配置错误:

“无法静态解析此组件的某些依赖项。\r\n'TempSearch.Command.Data.CommandHandlers.AddTempsCandidateAvailabilityCommandHandler' 正在等待以下依赖项:\r\n- 组件'TempSearch.Ioc.ExceptionHandlingIntercepter'(通过覆盖) 未找到。您是否忘记注册它或拼错名称?如果组件已注册并且覆盖是通过类型,请确保它没有显式分配非默认名称或通过名称覆盖依赖关系。\r\ n"

界面:

public interface ICommandHandler<TCommand>
{
    void Handle(TCommand command);
}

一个示例命令处理程序:

public class AddTempsCandidateAvailabilityCommandHandler 
    : ICommandHandler<TempsCandidateAvailability>
{
    private readonly IDbConnection connection;

    public AddTempsCandidateAvailabilityCommandHandler(
        IDbConnection connection)
    {
        this.connection = connection;
    }

    public void Handle(TempsCandidateAvailability command)
    {
        // ...
    }
}

注册:

public void Install(IWindsorContainer container, IConfigurationStore store)
{
    container.Register(
        Component.For<IDbConnection>()
            .UsingFactoryMethod(() => ConnectionHelper.GetOpenDbConnection(
                Connection.DatabaseName.ReedOnline))
            .LifestylePerWebRequest());

    container.Register(
        Classes
            .FromAssemblyContaining<EcruiterCommands>()
            .Where(t => t.Name.EndsWith("Commands"))
            .WithService
            .AllInterfaces().LifestylePerWebRequest());

    container.Register(
        Classes
            .FromAssemblyContaining<EcruiterCommands>()
            .Where(t => t.Name.EndsWith("CommandHandler"))
            .WithService.AllInterfaces()
            .LifestylePerWebRequest()
            .Configure(c => c.Interceptors<ExceptionHandlingIntercepter>()
                .LifestyleTransient()));
}

拦截器:

[Transient]
public class ExceptionHandlingIntercepter : IInterceptor
{
    private static readonly MethodInfo Execute = 
        typeof(ICommandHandler<>).GetMethod("Handle");

    private readonly IKernel kernel;

    public ExceptionHandlingIntercepter(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public void Intercept(IInvocation invocation)
    {
        if (invocation.Method != Execute)
        {
            invocation.Proceed();
            return;
        }

        try
        {
            invocation.Proceed();
        }
        finally
        {
            kernel.ReleaseComponent(invocation.Proxy);
        }
    }
}
4

2 回答 2

6

您必须注册拦截器本身,以便在初始化命令处理程序时让 Castle 解决它。将以下内容添加到您的注册中:

container.Register(
    Component.For<ExceptionHandlingIntercepter>(); // should be enough

我喜欢命名我的拦截器以按名称注册它们(不知道为什么,因为你的方式应该可以正常工作)

于 2015-04-15T10:06:04.833 回答
0

我使用以下代码解决了这个问题:

public void Install(IWindsorContainer container, IConfigurationStore store)
        {    
            container.Register(
                Component.For<IInterceptor>()
                    .ImplementedBy<ExceptionHandlingIntercepter>().LifestyleTransient(),
                Classes
                    .FromAssemblyContaining<EcruiterCommands>()
                    .Where(t => t.Name.EndsWith("CommandHandler"))
                    .WithServiceAllInterfaces()
                    .LifestylePerWebRequest().Configure(c => c.Interceptors<ExceptionHandlingIntercepter>())
                );
        }
于 2015-04-15T15:10:05.463 回答