【问题标题】:.NET Unity Interception using Custom Attributes.NET Unity 使用自定义属性拦截
【发布时间】:2019-03-02 18:35:55
【问题描述】:

我想获得答案here 中描述的行为,但通过代码使用配置。代码示例显示了在没有任何统一相关的情况下创建自定义属性并通过配置添加行为。

自定义属性位于同一解决方案中引用的单独程序集中。

问题是它在配置过程中抛出异常:

InvalidOperationException:Microsoft.Practices.Unity.InterceptionExtension.CustomAttributeMatchingRule 类型没有采用参数(LogAttribute、Boolean)的构造函数。

container
    .AddNewExtension<Interception>()
    .Configure<Interception>()
        .AddPolicy("MyLoggingPolicy")
        .AddMatchingRule<CustomAttributeMatchingRule>(
        new InjectionConstructor(typeof(Abstractions.Attributes.LogAttribute), true))
        .AddCallHandler<LoggingHandler>(new ContainerControlledLifetimeManager())
            .Interception
            .Container
        .RegisterType<IFirstInterface>(new InjectionFactory((context) => FirstClassFactoryMethod()))
        .RegisterType<ISecondInterface>(new InjectionFactory((context) => SecondClassFactoryMethod()));

[AttributeUsage(AttributeTargets.Method)]
public class LogAttribute : Attribute { }

public class LoggingHandler : ICallHandler
{
    public int Order { get; set; }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")} Started: {input.MethodBase.Name}");
        var result = getNext()(input, getNext);
        Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")} Completed: {input.MethodBase.Name}");
        return result;
    }
}

将抛出的行更新为:

.AddMatchingRule(
    new CustomAttributeMatchingRule(typeof(Abstractions.Attributes.LogAttribute), true))

防止抛出异常,但 LoggingHandler 不接收来自具有 [Log] 属性的方法的任何调用。

注意:标有 [Log] 的方法是公共方法,在不同的程序集中,在使用 .Resolve() 实例化的类中。

【问题讨论】:

  • 您拦截的方法是否可能不是虚拟的或不是接口定义的一部分,或者您将它们解析为具体类型而不是接口?因为这样会阻止拦截
  • @vasiloreshenski 它是接口定义的一部分。我像上面显示的那样解决它:.RegisterType(new InjectionFactory((context) => FirstClassFactoryMethod()))。它还需要是虚拟的吗?

标签: c# unity-container aop unity-interception


【解决方案1】:

对于遇到相同问题的任何人 - 我必须在注册类型上定义拦截行为:

.RegisterType<IFirstInterface>(
    new InjectionFactory((context) => FirstClassFactoryMethod())
    new Interceptor<TransparentProxyInterceptor>()
    new InterceptionBehavior<PolicyInjectionBehavior>())
.RegisterType<ISecondInterface>(
    new InjectionFactory((context) => SecondClassFactoryMethod())
    new Interceptor<TransparentProxyInterceptor>()
    new InterceptionBehavior<PolicyInjectionBehavior>()));

【讨论】:

    【解决方案2】:

    我遇到了同样的错误并尝试了 Filip 解决方案,但没有奏效。对我来说,为 InjectionFactory(Unity 4.0.1)更改了 InjectionConstructor:

    container
    .AddNewExtension<Interception>()
    .Configure<Interception>()
        .AddPolicy("MyLoggingPolicy")
        .AddMatchingRule<CustomAttributeMatchingRule>(
        new InjectionFactory((context) => new CustomAttributeMatchingRule(typeof(Abstractions.Attributes.LogAttribute), true))
        .AddCallHandler<LoggingHandler>(new ContainerControlledLifetimeManager());
    

    希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-06
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      相关资源
      最近更新 更多