3

我在 c# .Net framework 3.5 中有一个简单的 Web 客户端,它像这样调用 SOAP 服务 HelloWorld:

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

我正在使用一些中间件,通过请求授权标头来增加基本安全性:“Authorization=Bearer 123456abcd”,它与 REST 服务一起使用,但我想通过上面的 .Net 客户端使用该服务......

如何将标头添加到服务调用?是否存在类似:myservice.addHeader("authorization=blah");

4

1 回答 1

3

你应该使用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();
      }

授权

var messageProperty = new HttpRequestMessageProperty();
messageProperty.Headers.Add(HttpRequestHeader.Authorization, AuthorizationHeader);
于 2012-08-06T21:23:27.773 回答