1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2003-2005 Microsoft Corporation, All rights reserved.
3 // ----------------------------------------------------------------------------
7 using System
.ServiceModel
.Channels
;
8 using System
.Diagnostics
;
9 using System
.ServiceModel
;
10 using System
.Globalization
;
12 namespace Microsoft
.ServiceModel
.Samples
16 public interface ICalculatorContract
19 int Add(int x
, int y
);
23 public interface IDatagramContract
25 [OperationContract(IsOneWay
= true)]
26 void Hello(string greeting
);
30 public interface IStatusContract
43 /// Example of using UDP where bindings and addresses are specified in config.
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");
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.");
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
)
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
);