added samples
[windows-sources.git] / sdk / samples / WCFSamples / TechnologySamples / Basic / Service / OperationContextScope / CS / Client / Client.cs
blob0697b6cc873db26816eba89cbbbb3c74cf73e5e1
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.ServiceModel;
5 using System.ServiceModel.Channels;
7 namespace Microsoft.ServiceModel.Samples
9 //Utility class that acts as the header that client will add to list of incoming headers
10 public static class CustomHeader
12 public static readonly String HeaderName = "MessageHeaderGUID";
13 public static readonly String HeaderNamespace = "http://Microsoft.ServiceModel.Samples/GUID";
16 class MessageHeaderClient
18 static void Main(string[] args)
20 //Create two clients to the remote service.
21 MessageHeaderReaderClient client1 = new MessageHeaderReaderClient();
22 MessageHeaderReaderClient client2 = new MessageHeaderReaderClient();
25 //Create an OperationContextScope with client1 so we can add headers.
26 using (new OperationContextScope(client1.InnerChannel))
28 //Create a new GUID that we will send as header.
29 String guid = Guid.NewGuid().ToString();
31 //Create a MessageHeader for the guid we just created.
32 MessageHeader customHeader = MessageHeader.CreateHeader(CustomHeader.HeaderName, CustomHeader.HeaderNamespace, guid);
34 //Add the header to the OutgoingMessageHeader collection.
35 OperationContext.Current.OutgoingMessageHeaders.Add(customHeader);
37 //Now call RetreieveHeader on both the proxies. Since the OperationContextScope is tied to
38 //client1's InnerChannel, the header should only be added to calls made on that client.
39 //Calls made on client2 should not be sending the header across even though the call
40 //is made in the same OperationContextScope.
41 Console.WriteLine("Using client1 to send message");
42 Console.WriteLine("Did server retrieve the header? : Actual: {0}, Expected: True", client1.RetrieveHeader(guid));
44 Console.WriteLine();
45 Console.WriteLine("Using client2 to send message");
46 Console.WriteLine("Did server retrieve the header? : Actual: {0}, Expected: False", client2.RetrieveHeader(guid));
49 //Close the proxies.
50 client1.Close();
51 client2.Close();
53 Console.WriteLine();
54 Console.WriteLine("Press <ENTER> to terminate client.");
55 Console.ReadLine();