温莎城堡拦截器

Xer*_*xes 3 c# dependency-injection castle-windsor ioc-container interception

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

例外:

Castle.Windsor.dll中发生类型'Castle.MicroKernel.ComponentActivator.ComponentActivatorException'的异常,但未在用户代码中处理

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

似乎无法找到拦截器,因为它说某些组件配置错误:

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

界面:

public interface ICommandHandler<TCommand>
{
    void Handle(TCommand command);
}
Run Code Online (Sandbox Code Playgroud)

示例命令处理程序:

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

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

    public void Handle(TempsCandidateAvailability command)
    {
        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)

注册:

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()));
}
Run Code Online (Sandbox Code Playgroud)

拦截器:

[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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

sam*_*amy 5

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

container.Register(
    Component.For<ExceptionHandlingIntercepter>(); // should be enough
Run Code Online (Sandbox Code Playgroud)

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