OperationContextScope

OperationContextScope 示例演示如何使用头在 Windows Communication Foundation (WCF) 调用中发送额外的信息。在此示例中,服务器和客户端都是控制台应用程序。

提示

本主题的末尾介绍了此示例的设置过程和生成说明。

此示例演示客户端如何使用 OperationContextScopeMessageHeader 的方式发送额外的信息。OperationContextScope 对象是通过将其范围设置为通道来创建的。必须转换为远程服务的头可以添加到 OutgoingMessageHeaders 集合中。可以通过访问 IncomingMessageHeaders 在服务上检索添加到此集合中的头。它的调用是在多个通道进行的,添加到客户端的头然后将只应用于用来创建 OperationContextScope 的通道。

MessageHeaderReader

这是从客户端接收消息,并尝试在 IncomingMessageHeaders 集合中查找头的示例服务。客户端传递在头中发送的 GUID,服务则检索自定义头,如果存在自定义头,则将其与客户端作为参数传递的 GUID 进行比较。

public bool RetrieveHeader(string guid)
{
     MessageHeaders messageHeaderCollection = 
             OperationContext.Current.IncomingMessageHeaders;
     String guidHeader = null;

     Console.WriteLine("Trying to check if IncomingMessageHeader " +
               " collection contains header with value {0}", guid);
     if (messageHeaderCollection.FindHeader(
                       CustomHeader.HeaderName, 
                       CustomHeader.HeaderNamespace) != -1)
     {
          guidHeader = messageHeaderCollection.GetHeader<String>(
           CustomHeader.HeaderName, CustomHeader.HeaderNamespace);
     }
     else
     {
          Console.WriteLine("No header was found");
     }
     if (guidHeader != null)
     {
          Console.WriteLine("Found header with value {0}. "+ 
         "Does it match with GUID sent as parameter: {1}", 
          guidHeader, guidHeader.Equals(guid));
      }

      Console.WriteLine();
      //Return true if header is present and equals the guid sent by
      // client as argument
      return (guidHeader != null && guidHeader.Equals(guid));
}

MessageHeaderClient

这是使用通过 ServiceModel Metadata Utility Tool (Svcutil.exe) 生成的代理与远程服务通信的客户端实现。它首先创建 MessageHeaderReaderClient 的两个代理对象。

//Create two clients to the remote service.
MessageHeaderReaderClient client1 = new MessageHeaderReaderClient();
MessageHeaderReaderClient client2 = new MessageHeaderReaderClient();

客户端然后创建 OperationContextScope 并将其范围设置为 client1。它将 MessageHeader 添加到 OutgoingMessageHeaders 中,并在两个客户端上都进行一次调用。它通过检查从 RetrieveHeader 调用返回的值,确保只在 client1 上发送头,不在 client2 上发送头。

using (new OperationContextScope(client1.InnerChannel))
{
    //Create a new GUID that is sent as the header.
    String guid = Guid.NewGuid().ToString();

    //Create a MessageHeader for the GUID we just created.
    MessageHeader customHeader = MessageHeader.CreateHeader(CustomHeader.HeaderName, CustomHeader.HeaderNamespace, guid);

    //Add the header to the OutgoingMessageHeader collection.
    OperationContext.Current.OutgoingMessageHeaders.Add(customHeader);

    //Now call RetreieveHeader on both the proxies. Since the OperationContextScope is tied to 
    //client1's InnerChannel, the header should only be added to calls made on that client.
    //Calls made on client2 should not be sending the header across even though the call
    //is made in the same OperationContextScope.
    Console.WriteLine("Using client1 to send message");
    Console.WriteLine("Did server retrieve the header? : Actual: {0}, Expected: True", client1.RetrieveHeader(guid));

    Console.WriteLine();
    Console.WriteLine("Using client2 to send message");
    Console.WriteLine("Did server retrieve the header? : Actual: {0}, Expected: False", client2.RetrieveHeader(guid));
}

此示例是自承载的。下面提供了运行示例的示例输出:

Prompt> Service.exe
The service is ready.
Press <ENTER> to terminate service.

Trying to check if IncomingMessageHeader collection contains header with value 2239da67-546f-42d4-89dc-8eb3c06215d8
Found header with value 2239da67-546f-42d4-89dc-8eb3c06215d8. Does it match with GUID sent as parameter: True

Trying to check if IncomingMessageHeader collection contains header with value 2239da67-546f-42d4-89dc-8eb3c06215d8
No header was found


Prompt>Client.exe
Using client1 to send message
Did server retrieve the header? : Actual: True, Expected: True

Using client2 to send message
Did server retrieve the header? : Actual: False, Expected: False

Press <ENTER> to terminate client.

设置、生成和运行示例

  1. 请确保已经执行了 Windows Communication Foundation 示例的一次性安装过程

  2. 若要生成 C# 或 Visual Basic .NET 版本的解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。

  3. 若要用单机配置或跨计算机配置来运行示例,请按照运行 Windows Communication Foundation 示例中的说明进行操作。

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.