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

后端 未结 13 2314
不知归路
不知归路 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:26

    You add it to the call using:

    using (OperationContextScope scope = new OperationContextScope((IContextChannel)channel))
    {
        MessageHeader header = new MessageHeader("secret message");
        var untyped = header.GetUntypedHeader("Identity", "http://www.my-website.com");
        OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
    
        // now make the WCF call within this using block
    }
    

    And then, server-side you grab it using:

    MessageHeaders headers = OperationContext.Current.IncomingMessageHeaders;
    string identity = headers.GetHeader("Identity", "http://www.my-website.com");
    

提交回复
热议问题