3

I am consuming a WCF service which has multiple operations, this service requires me to add outgoing message properties whenever I call any of its operations.

At the moment i am just adding the properties every time i call the service inside the code.

Here is my code to do it:

using (new OperationContextScope(client.InnerChannel))
{
      OperationContext.Current.OutgoingMessageProperties.Add("P1", "Test User");
      OperationContext.Current.OutgoingMessageProperties.Add("P2", "Test Type");
      response = client.process(request);
}

How can I create a WCF extension that adds these message properties dynamically?

I have few knowledge about the extension but not enough to intercept and add these headers.

4

3 回答 3

4

要自动向消息添加一些标头,您必须实现IClientMessageInspector。它将允许您更改传出消息的任何内容。

编辑2:

public class ClientMessageInspector : IClientMessageInspector
{
    public object BeforeSendRequest(ref Message request, IClientChannel channel)
    {
        //following example is for adding http header.   
        //If you have another protocol you can add any other message  
        //manipulation code instead.

        //HttpResponseMessageProperty.Name returns "httpResponse".

        HttpResponseMessageProperty httpProp = null;
        if (request.Properties.ContainsKey(HttpResponseMessageProperty.Name))
        {
            httpProp = (HttpResponseMessageProperty)request.Properties[HttpResponseMessageProperty.Name];
        }
        else
        {
            httpProp = new HttpResponseMessageProperty();
            request.Properties.Add(HttpResponseMessageProperty.Name, httpProp);
        }

        httpProp.Headers.Add("YouHeader", "YourValue");

        //as I mentioned you can change a message in a way you need
        request.Properties.Add("P1", "Test User");
        request.Headers.Add(MessageHeader.CreateHeader("P1", "htt p://site.com/", "Test User"));

        return null;
    }

    public void AfterReceiveReply(ref Message reply, object correlationState) { }
}

public class ClientMessageInspectorBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(new ClientMessageInspector());
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {

    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

public class ClientMessageInspectorExtensionElement : BehaviorExtensionElement
{
    protected override object CreateBehavior()
    {
        return new ClientMessageInspectorBehavior();
    }

    public override Type BehaviorType
    {
        get
        {
            return typeof(ClientMessageInspectorBehavior);
        }
    }
}

你的配置文件应该是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.serviceModel>
    <client>
          <endpoint 
            address="http://localhost:8000/CoolerService" 
            behaviorConfiguration="coolerBehaviour" 
            binding="webHttpBinding"
            contract="CoolerService.ICoolerService"
            name="coolerEndpoint">
          </endpoint>
    </client>

    <extensions>
      <behaviorExtensions>
        <add name="customHeaderAdder" type="Extensions.ClientMessageInspectorExtensionElement, Extensions" />
      </behaviorExtensions>
    </extensions>

    <behaviors>
      <endpointBehaviors>
        <behavior name="coolerBehaviour">
          <customHeaderAdder />
        </behavior>
      </endpointBehaviors>
    </behaviors>

  </system.serviceModel>

</configuration>
于 2014-05-04T21:09:58.030 回答
1

在我看来,你可以简单地设置OperationContext.Current而不创建OperationScope这样的:

OperationContext.Current = new OperationContext(client.InnerChannel);
OperationContext.Current.OutgoingMessageProperties.Add("P1", "Test User");

起初这可能看起来像一个不好的做法,但是如果您查看文档,您会发现该OperationContext.Current属性实际上是针对当前线程的(使用ThreadStatic属性),因此是线程安全的。

请注意,client同一线程中的其他实例不会添加消息属性,因为OperationContext它绑定到客户端通道。

于 2017-03-09T14:50:24.170 回答
0

像前面的答案所说的那样实现 IClientMessageInspector,但是如果要添加 HTTP 标头,请使用与您使用过的类似的代码块 - 在 BeforeSendRequest 中。

using (new OperationContextScope((IContextChannel)channel))
{
      OperationContext.Current.OutgoingMessageProperties.Add("P1", "Test User");
      OperationContext.Current.OutgoingMessageProperties.Add("P2", "Test Type");
      response = client.process(request);
}
于 2015-01-08T21:01:18.557 回答