在WCF BehaviorExtension中没有调用BeforeSendRequest方法?我被卡住了请帮帮我......下面是代码

时间:2015-02-24 05:03:41

标签: wcf

这是我正在尝试的代码。我想修改服务的请求包。通常是请求标题,正文和操作

[CustomBehavior]     公共类Service1:IService1     {        ..... ......     }

public class CustomHeader : MessageHeader
{
    .....

.....        ....     }

/// <summary>
/// This class is used to inspect the message and headers on the server side,
/// This class is also used to intercept the message on the
/// client side, before/after any request is made to the server.
/// </summary>
public class CustomMessageInspector : IClientMessageInspector, IDispatchMessageInspector
{
    #region Message Inspector of the Service

    /// <summary>
    /// This method is called on the server when a request is received from the client.
    /// </summary>
    /// <param name="request"></param>
    /// <param name="channel"></param>
    /// <param name="instanceContext"></param>
    /// <returns></returns>
    public object AfterReceiveRequest(ref Message request,
           IClientChannel channel, InstanceContext instanceContext)
    {
        // Create a copy of the original message so that we can mess with it.
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
        request = buffer.CreateMessage();
        Message messageCopy = buffer.CreateMessage();

        // Read the custom context data from the headers
        ServiceHeader customData = CustomHeader.ReadHeader(request);

        // Add an extension to the current operation context so
        // that our custom context can be easily accessed anywhere.
        ServerContext customContext = new ServerContext();

        if (customData != null)
        {
            customContext.KerberosID = customData.KerberosID;
            customContext.SiteminderToken = customData.SiteminderToken;
        }
        OperationContext.Current.IncomingMessageProperties.Add(
                 "CurrentContext", customContext);
        return null;
    }

    /// <summary>
    /// This method is called after processing a method on the server side and just
    /// before sending the response to the client.
    /// </summary>
    /// <param name="reply"></param>
    /// <param name="correlationState"></param>
    public void BeforeSendReply(ref Message reply, object correlationState)
    {
        // Do some cleanup
        OperationContext.Current.Extensions.Remove(ServerContext.Current);
    }

    #endregion

    #region Message Inspector of the Consumer

    /// <summary>
    /// This method will be called from the client side just before any method is called.
    /// </summary>
    /// <param name="request"></param>
    /// <param name="channel"></param>
    /// <returns></returns>
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        // Prepare the request message copy to be modified
        MessageBuffer buffer = request.CreateBufferedCopy(Int32.MaxValue);
        request = buffer.CreateMessage();

        ServiceHeader customData = new ServiceHeader();

        customData.KerberosID = ClientContext.KerberosID;
        customData.SiteminderToken = ClientContext.SiteminderToken;

        CustomHeader header = new CustomHeader(customData);

        // Add the custom header to the request.
        request.Headers.Add(header);

        return null;
    }

    /// <summary>
    /// This method will be called after completion of a request to the server.
    /// </summary>
    /// <param name="reply"></param>
    /// <param name="correlationState"></param>
    public void AfterReceiveReply(ref Message reply, object correlationState)
    {

    }

    #endregion
}

/// <summary>
/// This class will act as a custom context in the client side to hold the context information.
/// </summary>
public class ClientContext
{
    public static string EmployeeID;
    public static string WindowsLogonID;
    public static string KerberosID;
    public static string SiteminderToken;
}

/// <summary>
/// This custom behavior class is used to add both client and server inspectors to
/// the corresponding WCF end points.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public class CustomBehavior : Attribute,  IEndpointBehavior
{
    #region IEndpointBehavior Members

    void IEndpointBehavior.AddBindingParameters(ServiceEndpoint endpoint,
         System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
        return;
    }

    void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint,
             System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        CustomMessageInspector inspector = new CustomMessageInspector();
        clientRuntime.MessageInspectors.Add(inspector);
    }

    void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint,
             System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
        ChannelDispatcher channelDispatcher = endpointDispatcher.ChannelDispatcher;
        if (channelDispatcher != null)
        {
            foreach (EndpointDispatcher ed in channelDispatcher.Endpoints)
            {
                CustomMessageInspector inspector = new CustomMessageInspector();
                ed.DispatchRuntime.MessageInspectors.Add(inspector);
            }
        }
    }

    void IEndpointBehavior.Validate(ServiceEndpoint endpoint) { return; }

    #endregion

}

public class MyBehaviorExtensionElement : BehaviorExtensionElement
{
    public MyBehaviorExtensionElement() { }
    public override Type BehaviorType
    {
        get { return typeof(CustomBehavior); }
    }

    protected override object CreateBehavior()
    {
        return new CustomBehavior();
    }
}

1 个答案:

答案 0 :(得分:0)

您需要将扩展​​名添加到您的代码或配置中:(下面的配置示例):

<extensions>
  <behaviorExtensions>
    <add name="yourMessageInspector"
      type="MyBehaviorExtensionElement"  />
  </behaviorExtensions>
</extensions>

以下参考资料应提供您需要的背景资料:
https://msdn.microsoft.com/en-us/library/ms730137%28v=vs.110%29.aspx
wcf :custom message inspector does not get wired up