How to add a custom HTTP header to every WCF call?

后端 未结 13 2478
不知归路
不知归路 2020-11-22 05:33

I have a WCF service that is hosted in a Windows Service. Clients that using this service must pass an identifier every time they\'re calling service methods (because that i

13条回答
  •  野的像风
    2020-11-22 06:33

    Here is another helpful solution for manually adding custom HTTP Headers to your client WCF request using the ChannelFactory as a proxy. This would have to be done for each request, but suffices as a simple demo if you just need to unit test your proxy in preparation for non-.NET platforms.

    // create channel factory / proxy ...
    using (OperationContextScope scope = new OperationContextScope(proxy))
    {
        OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = new HttpRequestMessageProperty()
        {
            Headers = 
            { 
                { "MyCustomHeader", Environment.UserName },
                { HttpRequestHeader.UserAgent, "My Custom Agent"}
            }
        };    
        // perform proxy operations... 
    }
    

提交回复
热议问题