如何将自定义 EndPointBehavior 添加到服务的 web.config?

Posted

技术标签:

【中文标题】如何将自定义 EndPointBehavior 添加到服务的 web.config?【英文标题】:How to add a Custom EndPointBehavior to the web.config of the service? 【发布时间】:2013-03-16 05:41:05 【问题描述】:

我已关注this article 并创建了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”派生的。

如何在 web.config 中添加MyEndPointBehavior

【问题讨论】:

你想要做的事情的好链接:weblogs.asp.net/paolopia/archive/2007/08/23/… 您是否尝试过使用此标签: 错误信息说明了一切,您的自定义扩展类型必须继承自 System.ServiceModel.Configuration.BehaviorExtensionElement。您可以创建一个新类,或者使MyMessageInspector 从中派生。您必须覆盖 CreateBehavior()BehaviorType 成员。 BehaviorType 将返回 typeof(MyEndPointBehavior)CreateBehavior() 将返回此类的一个实例。 【参考方案1】:

您还必须创建一个自定义 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>

【讨论】:

【参考方案2】:

对我来说,删除 web.config 中的程序集版本就足够了

Version=3.0.0.0, Culture=neutral, PublicKeyToken=null

【讨论】:

以上是关于如何将自定义 EndPointBehavior 添加到服务的 web.config?的主要内容,如果未能解决你的问题,请参考以下文章

Netsuite 将自定义字段添加到交易列字段

向 UIScrollView 添加多个子视图

如何将自定义挂钩添加到 Woocommerce 的自定义插件

如何将自定义类型保存到 QSettings?

如何将自定义标记导出回 AutoCAD

如何从 ADLS 将自定义数据框写入 eventthub