Adding a Header to .Net 3.5 WCF Client

I have a simple web client in C # .Net framework 3.5 that calls the HelloWorld SOAP service as follows:

HelloService myservice = new HelloService();
string result = myservice.HelloWorld();

I am using some kind of medium product that adds basic security by requesting an authorization header: "Authorization = Bearer 123456abcd", which works with the REST service, but I wanted to use this service with the .Net client above ...

How to add a title to a service call? There is something like myservice.addHeader("authorization=blah");:?

+5
source share
1 answer

You should use OperationContextScope

using (OperationContextScope scope = new OperationContextScope(wcfClient.InnerChannel))
      {
        MessageHeader header
          = MessageHeader.CreateHeader(
          "Service-Bound-CustomHeader",
          "http://Microsoft.WCF.Documentation",
          "Custom Happy Value."
          );
        OperationContext.Current.OutgoingMessageHeaders.Add(header);

        // Making calls.
        Console.WriteLine("Enter the greeting to send: ");
        string greeting = Console.ReadLine();

        //Console.ReadLine();
        header = MessageHeader.CreateHeader(
            "Service-Bound-OneWayHeader",
            "http://Microsoft.WCF.Documentation",
            "Different Happy Value."
          );
        OperationContext.Current.OutgoingMessageHeaders.Add(header);

        // One-way
        wcfClient.Push(greeting);
        this.wait.WaitOne();

        // Done with service. 
        wcfClient.Close();
        Console.WriteLine("Done!");
        Console.ReadLine();
      }

For authorization

var messageProperty = new HttpRequestMessageProperty();
messageProperty.Headers.Add(HttpRequestHeader.Authorization, AuthorizationHeader);
+5
source

All Articles