gpt4 book ai didi

c# - CaSTLe温莎拦截器

转载 作者:行者123 更新时间:2023-11-30 14:53:22 29 4
gpt4 key购买 nike

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

异常(exception):

An exception of type 'Castle.MicroKernel.ComponentActivator.ComponentActivatorException' occurred in Castle.Windsor.dll but was not handled in user code

Additional information: ComponentActivator: could not proxy TempSearch.Command.Data.CommandHandlers.AddTempsJobCommandHandler

它看起来像它说的那样找不到拦截器一些组件配置错误:

"Some dependencies of this component could not be statically resolved.\r\n'TempSearch.Command.Data.CommandHandlers.AddTempsCandidateAvailabilityCommandHandler' is waiting for the following dependencies:\r\n- Component 'TempSearch.Ioc.ExceptionHandlingIntercepter' (via override) which was not found. Did you forget to register it or misspelled the name? If the component is registered and override is via type make sure it doesn't have non-default name assigned explicitly or override the dependency via name.\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);
}
}
}

最佳答案

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

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

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

关于c# - CaSTLe温莎拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29629645/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com