我如何判断iregistrationBuilder.enableInterFaceceptors()已经调用了?
我正在使用autofac.extras.dynamicproxy
写几个iinterceptor
s。它们可以单独使用,也可以一起使用。我希望这些拦截器的消费者能够轻松地将它们连接到AutoFac注册,因此我为每个拦截器编写了iregistrationBuilder
扩展方法:
public static class RegistrationBuilderExtensions
{
public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> WithTelemetryLogging<T, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
return builder.EnableInterfaceInterceptors().InterceptedBy(typeof(TelemetryLoggingInterceptor));
}
public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> WithCorrelationRoots<T, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
return builder.EnableInterfaceInterceptors().InterceptedBy(typeof(NewCorrelationInterceptor));
}
}
当使用一个或另一个时,扩展方法可以很好地工作。但是,如果消费者使用 扩展方法,则如下:
builder.RegisterAssemblyTypes(GetType().Assembly)
.AsImplementedInterfaces()
.WithCorrelationRoots()
.WithTelemetryLogging()
.PreserveExistingDefaults();
我有一个例外,要创建代理代理:
castle.dynamicproxy.proxygeneration exception:这是DynamiCproxy2错误:代理实现Castle.dynamicproxy.iproxytargetAccessor,它是一个动态的基础架构接口,您永远不应该自己实现它。您是否试图代理现有代理?
我相信这是由于enableInterfaceinterceptors()
被称为两次,每种扩展方法一次。
我宁愿从扩展方法中删除enableInterfaceInterceptors()
的调用,并迫使消费者记住自己调用它。取而代之的是,我希望能够根据是否已经调用它有条件地调用它。
我如何检查iregistrationBuilder
对象确定是否已经进行了调用和/或使用其条件注册机制之一?类似:
if (!builder.EIIHasBeenCalled)
{
builder.EnableInterfaceInterceptors();
}
return builder.InterceptedBy(...
或
builder.EnableInterfaceInterceptors.OnlyIf(b => !b.EIIHasBeenCalled).InterceptedBy(...
I'm using Autofac.Extras.DynamicProxy
to write a couple of IInterceptor
s. They can be used individually, or both together. I want consumers of these interceptors to be able to attach them to Autofac registrations easily, so I wrote an IRegistrationBuilder
extension method for each of them:
public static class RegistrationBuilderExtensions
{
public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> WithTelemetryLogging<T, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
return builder.EnableInterfaceInterceptors().InterceptedBy(typeof(TelemetryLoggingInterceptor));
}
public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> WithCorrelationRoots<T, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
return builder.EnableInterfaceInterceptors().InterceptedBy(typeof(NewCorrelationInterceptor));
}
}
The extension methods work well when one or the other is used. However, if a consumer uses both extension methods, as in:
builder.RegisterAssemblyTypes(GetType().Assembly)
.AsImplementedInterfaces()
.WithCorrelationRoots()
.WithTelemetryLogging()
.PreserveExistingDefaults();
I get an exception about creating a proxy of a proxy:
Castle.DynamicProxy.ProxyGenerationException: This is a DynamicProxy2 error: Target type for the proxy implements Castle.DynamicProxy.IProxyTargetAccessor which is a DynamicProxy infrastructure interface and you should never implement it yourself. Are you trying to proxy an existing proxy?
I believe it's due to EnableInterfaceInterceptors()
being called twice, once in each extension method.
I'd rather not remove the calls to EnableInterfaceInterceptors()
from the extension methods and force the consumers to remember to call it themselves. Instead, I'd like to be able to conditionally call it based on whether or not it had already been called.
How I can inspect the IRegistrationBuilder
object to determine if this call has already been made, and/or use one of its conditional registration mechanisms? Something like:
if (!builder.EIIHasBeenCalled)
{
builder.EnableInterfaceInterceptors();
}
return builder.InterceptedBy(...
or
builder.EnableInterfaceInterceptors.OnlyIf(b => !b.EIIHasBeenCalled).InterceptedBy(...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法检查事后的拦截位。但是,
ContainerBuilder
具有属性
您可以使用的字典。不幸的是,当您使用它时,它看起来有些混乱,因为
containerBuilder
是具有属性的原因。您必须将其传递。集装箱构建和注册不是多线程的,因此您不会使用该<code> containsKey 调用任何竞赛条件。
propertyKey
方法上有一个警告 - 这基本上是唯一的per per 注册类型,但是如果您有一系列相同类型的服务,则可能是一个挑战。现在没有一个很好的方法来唯一地识别注册。 我代表您提出了有关此问题的问题。同时,这是关于如何做的一个想法。You can't inspect the interception bits post-facto. However,
ContainerBuilder
has aProperties
dictionary that you could use.Unfortunately, it looks a little messy when you use it because the
ContainerBuilder
is what has the properties. You'd have to pass that in.Container building and registration isn't multi-threaded so you won't hit any race conditions with that
ContainsKey
call.There is a caveat on the
PropertyKey
method - this will basically be unique per registration type but if you have a bunch of services that are identical types, that could be a challenge. There is not a good way right now to uniquely identify a registration. I've filed an issue about this on your behalf. In the meantime, this is one idea on how to do it.受特拉维斯(Travis)的响应的启发,我在
iregistrationBuilder
中找到了一个字典,我成功使用了。我现在有看起来像这样的代码:谢谢,特拉维斯(Travis)的灵感!
Inspired by Travis' response, I found a dictionary within
IRegistrationBuilder
that I'm using with success. I now have code that looks like this:Thanks, Travis, for the inspiration!