使用 Unity 2.0 拦截无法拦截 WCF 服务
在我的 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>
当我尝试使用 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; }
}
}
可能出了什么问题?
In my WCF web application I have configured the Unity container for Interception. Following is my unity configuration.
<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>
When I try to invoke a method on the service using wcftestclient tool, following exception is thrown.
ArgumentException - The type WCFServiceLib1.Service1 is not interceptable.
Parameter name: interceptedType
I used the svctraceviewer tool to get the above exception details.
Following is the implementation of the class 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; }
}
}
What could possibly be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于 WCF 实例提供程序未解析接口。它解决了服务类型。您正在使用接口拦截器,它不能直接应用于类。请参阅拦截技术比较。
修复方法是:
VirtualMethodInterceptor
。虚拟
。注册示例:
The problem is that the WCF instance provider isn't resolving an interface. Its resolving the service type. You are using an interface interceptor, which cannot be directly applied to a class. See Comparison of Interception Techniques.
The fix is:
VirtualMethodInterceptor
.virtual
.Example registration: