Send information in Soap Header to WCF service in

2019-09-05 14:36发布

问题:

i want a web application to create a service reference to my WCF service, insert information to the header of the soap call and call my WCF method.

i read about MessageContract attribute and declared one in the interface file:

[MessageContract]
public class BasicServiceHeader
{
    [MessageHeader]
    public string myString;
}

my WCf interface is:

   [ServiceContract]
public interface IBasicService
{

    [OperationContract]       
    [WebGet(UriTemplate = "GetData?value={value}")]     // Add support for HTTP GET Requests
    string GetData(int value);}

i don't want the BasicServiceHeader to be passed as a parameter of GetData function , i want to keep the function as it is and to extract the BasicServiceHeader inside the function, can i do that ?

回答1:

Client side, you can pass a header prior invoking the operation:

MessageHeader messageHeader = MessageHeader.CreateHeader(_headerName, _headersNameSpace, _headerValue);
OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);

and extract it using FindHeader service side