如何添加自定义EndPointBehavior到web.服务的配置

本文关键字:web 服务 配置 EndPointBehavior 自定义 何添加 添加 | 更新日期: 2023-09-27 18:06:00

我按照这篇文章创建了MyMessageInspectorMyEndPointBehavior类,如下所示:

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?

如何添加自定义EndPointBehavior到web.服务的配置

你还必须创建一个自定义的BehaviorExtensionElement并在web中使用它。配置文件。有许多文章可以帮助你喜欢这些

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
}

这里添加新的behavioreextensionelement

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>

对我来说足以删除web.config中的汇编版本

Version=3.0.0.0, Culture=neutral, PublicKeyToken=null