使用Unity 2.0拦截不能拦截WCF服务

sur*_*aik 1 wcf aop unity-container

在我的WCF Web应用程序中,我已经为拦截配置了Unity容器.以下是我的统一配置.

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> 
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>

    <assembly name="Infrastructure" />
    <assembly name="WCFServiceLib1"/>

    <namespace name="Infrastructure"/>
    <namespace name="WCFServiceLib1" />

    <container>
      <extension type="Interception" />
      <register type="IService1" mapTo="Service1">
        <interceptor type="InterfaceInterceptor"/>
        <interceptionBehavior type="LogPerformanceDataBehavior"/>
      </register>
    </container>
</unity>
Run Code Online (Sandbox Code Playgroud)

当我尝试使用wcftestclient工具调用服务上的方法时,抛出以下异常.

ArgumentException - 类型WCFServiceLib1.Service1不可拦截.
参数名称:interceptedType

我使用svctraceviewer工具获取上述异常详细信息.

以下是LogPerformanceDataBehavior类的实现

public class LogPerformanceDataBehavior : IInterceptionBehavior
{
    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Type.EmptyTypes;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        var watch = new Stopwatch();

        watch.Start();
        IMethodReturn methodReturn = getNext()(input, getNext);
        watch.Stop();

        string sb = string.Format("Method {0}.{1} executed in: ({2} ms, {3} ticks){4}",
                                  input.MethodBase.DeclaringType.Name, input.MethodBase.Name,
                                  watch.ElapsedMilliseconds, watch.ElapsedTicks, Environment.NewLine);

        using (StreamWriter outfile = new StreamWriter(@"c:\logs\Performance.txt"))
        {
            outfile.Write(sb);
        }

        return methodReturn;
    }

    public bool WillExecute
    {
        get { return true; }
    }
}
Run Code Online (Sandbox Code Playgroud)


什么可能是错的?

Ern*_*ieL 6

问题是WCF实例提供程序没有解析接口.它解决了服务类型.您正在使用接口拦截器,它无法直接应用于类.见拦截技术的比较.

修复是:

  1. 改为a VirtualMethodInterceptor.
  2. 将任何要截取的服务方法标记为virtual.

注册示例:

<register type="Service1" >         
    <interceptor type="VirtualMethodInterceptor"/> 
    <interceptionBehavior type="LogPerformanceDataBehavior"/>       
</register>
Run Code Online (Sandbox Code Playgroud)