我如何判断iregistrationBuilder.enableInterFaceceptors()已经调用了?

发布于 2025-01-26 11:48:06 字数 2086 浏览 3 评论 0原文

我正在使用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 IInterceptors. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提赋 2025-02-02 11:48:06

您无法检查事后的拦截位。但是,ContainerBuilder具有属性您可以使用的字典。

public static class RegistrationBuilderExtensions
{
  public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle>
    WithTelemetryLogging<T, TActivatorData, TRegistrationStyle>(
      this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder, ContainerBuilder cb)
  {
    var key = PropertyKey(builder);
    if(!cb.Properties.ContainsKey(key))
    {
      cb.Properties[key] = true;
      return builder
        .EnableInterfaceInterceptors()
        .InterceptedBy(typeof(TelemetryLoggingInterceptor));
    }

    return builder;
  }

  public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle>
    WithCorrelationRoots<T, TActivatorData, TRegistrationStyle>(
      this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder, ContainerBuilder cb)
  {
    var key = PropertyKey(builder);
    if(!cb.Properties.ContainsKey(key))
    {
      cb.Properties[key] = true;
      return builder
        .EnableInterfaceInterceptors()
        .InterceptedBy(typeof(NewCorrelationInterceptor));
    }

    return builder;
  }

  private static string PropertyKey<T, TActivatorData, TRegistrationStyle>(IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> registration)
  {
    // Uniquely identify the registration however you want.
    return registration.GetType().ToString();
  }
}

不幸的是,当您使用它时,它看起来有些混乱,因为containerBuilder是具有属性的原因。您必须将其传递。

builder.RegisterAssemblyTypes(GetType().Assembly)
    .AsImplementedInterfaces()
    .WithCorrelationRoots(builder)
    .PreserveExistingDefaults();

集装箱构建和注册不是多线程的,因此您不会使用该<​​code> containsKey 调用任何竞赛条件。

propertyKey方法上有一个警告 - 这基本上是唯一的per per 注册类型,但是如果您有一系列相同类型的服务,则可能是一个挑战。现在没有一个很好的方法来唯一地识别注册。 我代表您提出了有关此问题的问题。同时,这是关于如何做的一个想法。

You can't inspect the interception bits post-facto. However, ContainerBuilder has a Properties dictionary that you could use.

public static class RegistrationBuilderExtensions
{
  public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle>
    WithTelemetryLogging<T, TActivatorData, TRegistrationStyle>(
      this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder, ContainerBuilder cb)
  {
    var key = PropertyKey(builder);
    if(!cb.Properties.ContainsKey(key))
    {
      cb.Properties[key] = true;
      return builder
        .EnableInterfaceInterceptors()
        .InterceptedBy(typeof(TelemetryLoggingInterceptor));
    }

    return builder;
  }

  public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle>
    WithCorrelationRoots<T, TActivatorData, TRegistrationStyle>(
      this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder, ContainerBuilder cb)
  {
    var key = PropertyKey(builder);
    if(!cb.Properties.ContainsKey(key))
    {
      cb.Properties[key] = true;
      return builder
        .EnableInterfaceInterceptors()
        .InterceptedBy(typeof(NewCorrelationInterceptor));
    }

    return builder;
  }

  private static string PropertyKey<T, TActivatorData, TRegistrationStyle>(IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> registration)
  {
    // Uniquely identify the registration however you want.
    return registration.GetType().ToString();
  }
}

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.

builder.RegisterAssemblyTypes(GetType().Assembly)
    .AsImplementedInterfaces()
    .WithCorrelationRoots(builder)
    .PreserveExistingDefaults();

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.

蓝天 2025-02-02 11:48:06

受特拉维斯(Travis)的响应的启发,我在iregistrationBuilder中找到了一个字典,我成功使用了。我现在有看起来像这样的代码:

const string MF_ENABLE_INTERFACE_INTERCEPTORS = "MFEnableInterfaceInterceptors";

public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> WithTelemetryLogging<T, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
    if (!_AreInterfaceInterceptorsEnabled(builder))
    {
        builder.EnableInterfaceInterceptors();
        _TrackEnableInterfaceInterceptors(builder);
    }
    return builder.InterceptedBy(typeof(TelemetryLoggingInterceptor));
}

public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> WithCorrelationRoots<T, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
    if (!_AreInterfaceInterceptorsEnabled(builder))
    {
        builder.EnableInterfaceInterceptors();
        _TrackEnableInterfaceInterceptors(builder);
    }
    return builder.InterceptedBy(typeof(CorrelatedInterceptor));
}

private static bool _AreInterfaceInterceptorsEnabled<T, TActivatorData, TRegistrationStyle>(IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
    if (builder.RegistrationData.Metadata.TryGetValue(MF_ENABLE_INTERFACE_INTERCEPTORS, out var metadata))
    {
        return (bool?)metadata ?? false;
    }
    return false;
}

private static void _TrackEnableInterfaceInterceptors<T, TActivatorData, TRegistrationStyle>(IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
    builder.RegistrationData.Metadata[MF_ENABLE_INTERFACE_INTERCEPTORS] = true;
}

谢谢,特拉维斯(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:

const string MF_ENABLE_INTERFACE_INTERCEPTORS = "MFEnableInterfaceInterceptors";

public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> WithTelemetryLogging<T, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
    if (!_AreInterfaceInterceptorsEnabled(builder))
    {
        builder.EnableInterfaceInterceptors();
        _TrackEnableInterfaceInterceptors(builder);
    }
    return builder.InterceptedBy(typeof(TelemetryLoggingInterceptor));
}

public static IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> WithCorrelationRoots<T, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
    if (!_AreInterfaceInterceptorsEnabled(builder))
    {
        builder.EnableInterfaceInterceptors();
        _TrackEnableInterfaceInterceptors(builder);
    }
    return builder.InterceptedBy(typeof(CorrelatedInterceptor));
}

private static bool _AreInterfaceInterceptorsEnabled<T, TActivatorData, TRegistrationStyle>(IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
    if (builder.RegistrationData.Metadata.TryGetValue(MF_ENABLE_INTERFACE_INTERCEPTORS, out var metadata))
    {
        return (bool?)metadata ?? false;
    }
    return false;
}

private static void _TrackEnableInterfaceInterceptors<T, TActivatorData, TRegistrationStyle>(IRegistrationBuilder<T, TActivatorData, TRegistrationStyle> builder)
{
    builder.RegistrationData.Metadata[MF_ENABLE_INTERFACE_INTERCEPTORS] = true;
}

Thanks, Travis, for the inspiration!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文