added samples
[windows-sources.git] / sdk / samples / WCFSamples / TechnologySamples / Extensibility / Transport / UdpActivation / CS / Client / Client.cs
blob3bf5d7fc74df3d784efbe12b82757e54e8566274
1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2003-2005 Microsoft Corporation, All rights reserved.
3 // ----------------------------------------------------------------------------
5 using System;
7 using System.ServiceModel.Channels;
8 using System.Diagnostics;
9 using System.ServiceModel;
10 using System.Globalization;
12 namespace Microsoft.ServiceModel.Samples
14 #region Contracts
15 [ServiceContract]
16 public interface ICalculatorContract
18 [OperationContract]
19 int Add(int x, int y);
22 [ServiceContract]
23 public interface IDatagramContract
25 [OperationContract(IsOneWay = true)]
26 void Hello(string greeting);
29 [ServiceContract]
30 public interface IStatusContract
32 [OperationContract]
33 void Start();
35 [OperationContract]
36 string GetStatus();
38 #endregion
40 class UdpTestConsole
42 /// <summary>
43 /// Example of using UDP where bindings and addresses are specified in config.
44 /// </summary>
45 static void Main(string[] args)
47 Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));
48 Console.WriteLine("Testing Udp Activation.");
50 // 1. Start status service
51 Console.WriteLine("Start the status service.");
52 IStatusContract status = ChannelFactoryHelper.CreateSingleChannel<IStatusContract>("status");
53 status.Start();
55 // 2. Sending datagrams
56 Console.WriteLine("Sending UDP datagrams.");
57 Console.Write("Type a word that you want to say to the server: ");
58 string input = Console.ReadLine();
59 IDatagramContract datagram = ChannelFactoryHelper.CreateSingleChannel<IDatagramContract>("datagram");
60 for (int i = 0; i < 5; i++)
62 string greeting = string.Format(CultureInfo.InvariantCulture, "{0}[{1}]", input, i);
63 Console.WriteLine(" Sending datagram: {0}", greeting);
64 datagram.Hello(greeting);
66 ((IChannel)datagram).Close();
68 // 3. Calling UDP duplex contract
69 Console.WriteLine("Calling UDP duplex contract (ICalculatorContract).");
70 ICalculatorContract calculator = ChannelFactoryHelper.CreateSingleChannel<ICalculatorContract>("calculator");
71 for (int i = 0; i < 5; ++i)
73 Console.WriteLine(" {0} + {1} = {2}", i, i * 2, calculator.Add(i, i * 2));
75 ((IChannel)calculator).Close();
77 // 4. Check status service
78 Console.WriteLine("Getting status and dump server traces:");
79 string statusResult = status.GetStatus();
80 string[] traces = statusResult.Split('|');
81 for (int i = 0; i < traces.Length; i++)
83 Console.WriteLine(" {0}", traces[i]);
85 ((IChannel)status).Close();
87 Console.WriteLine("Press <ENTER> to complete test.");
88 Console.ReadLine();
92 public static class ChannelFactoryHelper
94 static void BindLifetimes(IChannelFactory factory, IChannel channel)
96 channel.Closed += delegate
98 IAsyncResult result = factory.BeginClose(FactoryCloseCallback, factory);
99 if (result.CompletedSynchronously)
100 factory.EndClose(result);
104 static void FactoryCloseCallback(IAsyncResult result)
106 if (result.CompletedSynchronously)
107 return;
108 IChannelFactory factory = (IChannelFactory)result.AsyncState;
109 factory.EndClose(result);
112 public static T CreateSingleChannel<T>(string endpointConfigurationName)
114 return CreateSingleChannel<T>(new ChannelFactory<T>(endpointConfigurationName));
117 public static T CreateSingleChannel<T>(Binding binding, EndpointAddress address)
119 return CreateSingleChannel<T>(new ChannelFactory<T>(binding, address));
122 static T CreateSingleChannel<T>(ChannelFactory<T> factory)
124 T channel = factory.CreateChannel();
125 BindLifetimes(factory, (IChannel)channel);
126 return channel;