首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Unity2.0侦听WCF服务不可拦截

使用Unity2.0侦听WCF服务不可拦截
EN

Stack Overflow用户
提问于 2012-01-06 08:36:23
回答 1查看 2.6K关注 0票数 1

在我的WCF web应用程序中,我配置了用于拦截的统一容器。以下是我的统一配置。

代码语言:javascript
复制
<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>

当我试图使用wcftestclient工具在服务上调用一个方法时,会引发以下异常。

ArgumentException -类型WCFServiceLib1.Service1不可截取.

参数名称: interceptedType

我使用svctraceviewer工具获取上述异常细节。

下面是类LogPerformanceDataBehavior的实现

代码语言:javascript
复制
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; }
    }
}

有什么可能是错的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-09 08:25:27

问题是WCF实例提供程序没有解决接口问题。它解析服务类型。您使用的是无法直接应用于类的接口拦截器。见Comparison of Interception Techniques

解决办法是:

  1. 改为VirtualMethodInterceptor。将被拦截的任何服务方法标记为virtual.

示例注册:

代码语言:javascript
复制
<register type="Service1" >         
    <interceptor type="VirtualMethodInterceptor"/> 
    <interceptionBehavior type="LogPerformanceDataBehavior"/>       
</register>
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8755263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档