OperationContextScope 示例演示如何使用标头在 Windows Communication Foundation (WCF) 调用上发送额外信息。 在此示例中,服务器和客户端都是控制台应用程序。
注释
本示例的设置过程和生成说明位于本主题末尾。
此示例演示客户端如何使用 MessageHeader 以 OperationContextScope 的方式发送额外的信息。 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 元数据实用工具工具(Svcutil.exe) 生成的代理与远程服务通信的客户端实现。 它首先创建两个代理 MessageHeaderReaderClient
对象。
//Create two clients to the remote service.
MessageHeaderReaderClient client1 = new MessageHeaderReaderClient();
MessageHeaderReaderClient client2 = new MessageHeaderReaderClient();
然后,客户端创建 OperationContextScope 并将其限定为 client1
. 它将 MessageHeader 添加到 OutgoingMessageHeaders 中,并在两个客户端上都进行一次调用。 它通过检查从 client1
调用返回的值,确保只在 client2
上发送标头,不在 RetrieveHeader
上发送标头。
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 RetrieveHeader 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.
设置、生成和运行示例
确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。
若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。
若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。