我已经关注了这篇文章,并创建MyMessageInspector
和MyEndPointBehavior
分类如下:
public class MyMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
Console.WriteLine("Incoming request: {0}", request);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
public class MyEndPointBehavior : IEndpointBehavior
{
#region IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
ChannelDispatcher channelDispatcher = endpointDispatcher.ChannelDispatcher;
if (channelDispatcher != null)
{
foreach (EndpointDispatcher ed in channelDispatcher.Endpoints)
{
ed.DispatchRuntime.MessageInspectors.Add(new MyMessageInspector());
}
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
#endregion
}
如何将 MyEndPointBehavior 添加到 web.config?
我添加了以下扩展:
<extensions>
<behaviorExtensions>
<add name="myMessageInspector" type="MessageInspectorProject.MyEndPointBehavior, MessageInspectorProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
但是当我尝试在下面使用它时,它会抱怨:
<serviceBehaviors>
<behavior>
<myMessageInspector/>
它的抱怨如下:
配置中的元素无效。扩展“myMessageInspector”不是从正确的扩展基类型“System.ServiceModel.Configuration.BehaviorExtensionElement”派生的。
如何添加MyEndPointBehavior
到 web.config?