我试图在我的命令处理程序上拦截对 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);
}
}
}