簡體   English   中英

如何在服務的 web.config 中添加自定義 EndPointBehavior?

[英]How to add a Custom EndPointBehavior to the web.config of the service?

我已經按照這篇文章創建了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?

您還必須創建自定義BehaviorExtensionElement並在 web.config 文件中使用它。 有很多文章可以幫助你喜歡這些

http://weblogs.asp.net/paolopia/writing-a-wcf-message-inspector

http://cgeers.com/2011/05/10/wcf-message-logging/

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>

對我來說足以刪除 web.config 中的程序集版本

Version=3.0.0.0, Culture=neutral, PublicKeyToken=null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM