在我的WCF web应用程序中,我配置了用于拦截的统一容器。以下是我的统一配置。
<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的实现
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; }
}
}有什么可能是错的?
发布于 2012-01-09 08:25:27
问题是WCF实例提供程序没有解决接口问题。它解析服务类型。您使用的是无法直接应用于类的接口拦截器。见Comparison of Interception Techniques。
解决办法是:
VirtualMethodInterceptor。将被拦截的任何服务方法标记为virtual.
示例注册:
<register type="Service1" >
<interceptor type="VirtualMethodInterceptor"/>
<interceptionBehavior type="LogPerformanceDataBehavior"/>
</register>https://stackoverflow.com/questions/8755263
复制相似问题