我遵循这篇文章并创建了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‘不是从正确的扩展基类型myMessageInspector派生的。
如何将MyEndPointBehavior添加到web.config中?
发布于 2014-09-13 14:08:44
您还必须创建一个自定义BehaviorExtensionElement并在web.config文件中使用它。有许多文章可以帮助你喜欢这些。
http://weblogs.asp.net/paolopia/writing-a-wcf-message-inspector
https://github.com/geersch/WcfMessageLogging
http://burcakcakiroglu.com/?p=2083
http://trycatch.me/adding-custom-message-headers-to-a-wcf-service-using-inspectors-behaviors/
无论如何,用这样的方式修改代码。
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
}在这里添加新的BehaviorExtensionElement
public class CustomBehaviorExtensionElement : BehaviorExtensionElement
{
protected override object CreateBehavior()
{
return new MyEndPointBehavior();
}
public override Type BehaviorType
{
get
{
return typeof(MyEndPointBehavior);
}
}
}并更新您的web.config
<extensions>
<behaviorExtensions>
<add name="myMessageInspector" type="MessageInspectorProject.CustomBehaviorExtensionElement, MessageInspectorProject"/>
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior>
<myMessageInspector />
</behavior>
</endpointBehaviors>
</behaviors>发布于 2020-07-12 16:52:41
对我来说,这足以删除web.config中的程序集版本。
Version=3.0.0.0, Culture=neutral, PublicKeyToken=nullhttps://stackoverflow.com/questions/15636874
复制相似问题