PageRenderTime 23ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSRemotingClient/Program.cs

#
C# | 245 lines | 110 code | 42 blank | 93 comment | 6 complexity | f90aa6db32a7f2a435ec8efb3ec63ec8 MD5 | raw file
  1. /******************************** Module Header ********************************\
  2. Module Name: Program.cs
  3. Project: CSRemotingClient
  4. Copyright (c) Microsoft Corporation.
  5. .NET remoting provides an abstract approach to interprocess communication that
  6. separates the remotable object from a specific client or server application
  7. domain and from a specific mechanism of communication.
  8. .NET Remoting allows an application to make a remotable object available across
  9. remoting boundaries, which includes different appdomains, processes or even
  10. different computers connected by a network. .NET Remoting makes a reference of a
  11. remotable object available to a client application, which then instantiates and
  12. uses a remotable object as if it were a local object. However, the actual code
  13. execution happens at the server-side. Any requests to the remotable object are
  14. proxied by the .NET Remoting runtime over Channel objects, that encapsulate the
  15. actual transport mode, including TCP streams, HTTP streams and named pipes. As a
  16. result, by instantiating proper Channel objects, a .NET Remoting application can
  17. be made to support different communication protocols without recompiling the
  18. application. The runtime itself manages the act of serialization and marshalling
  19. of objects across the client and server appdomains.
  20. CSRemotingClient is a .NET Remoting client project. It accesses the remote objects
  21. (SingleCall objects or Singleton objects or client-activated objects) exposed by
  22. the .NET Remoting server project CSRemotingServer.
  23. There are generally two ways to create the .NET Remoting client: using a
  24. configuration file or writing codes. The AccessRemotingServerByConfig method
  25. demonstrates the former, and the AccessRemotingServerByCode method illustrates
  26. the latter method.
  27. This source is subject to the Microsoft Public License.
  28. See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  29. All other rights reserved.
  30. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  31. EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  32. WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  33. \***************************************************************************/
  34. #region Using directives
  35. using System;
  36. using System.Collections;
  37. using System.Runtime.Remoting.Channels.Tcp;
  38. using System.Runtime.Remoting.Channels;
  39. using System.Runtime.Remoting;
  40. using System.Runtime.Serialization.Formatters;
  41. using System.Runtime.InteropServices;
  42. using System.Net.Sockets;
  43. using RemotingShared;
  44. using System.Diagnostics;
  45. #endregion
  46. namespace CSRemotingClient
  47. {
  48. class Program
  49. {
  50. static void Main(string[] args)
  51. {
  52. if (args.Length > 0 && (args[0].StartsWith("-") || args[0].StartsWith("/")))
  53. {
  54. string cmd = args[0].Substring(1);
  55. if (cmd.Equals("configfile", StringComparison.OrdinalIgnoreCase))
  56. {
  57. // Access the .NET Remoting server using a configuration file.
  58. AccessRemotingServerByConfig();
  59. }
  60. else if (cmd.Equals("code", StringComparison.OrdinalIgnoreCase))
  61. {
  62. // Access the .NET Remoting server using code.
  63. AccessRemotingServerByCode();
  64. }
  65. else
  66. {
  67. PrintInstructions();
  68. }
  69. }
  70. else
  71. {
  72. // By default, access the .NET Remoting server using a
  73. // configuration file.
  74. AccessRemotingServerByConfig();
  75. }
  76. }
  77. static void PrintInstructions()
  78. {
  79. Console.WriteLine("CSRemotingClient Instructions:");
  80. Console.WriteLine("Access the .NET Remoting server");
  81. Console.WriteLine(" -configfile using a config file");
  82. Console.WriteLine(" -code using code");
  83. }
  84. #region Access the .NET Remoting server using a configuration file.
  85. /// <summary>
  86. /// Access the .NET Remoting server using a configuration file.
  87. /// </summary>
  88. static void AccessRemotingServerByConfig()
  89. {
  90. // Read the configuration file and configure the remoting
  91. // infrastructure for the client project.
  92. // The format for .NET Remoting configuration file:
  93. // http://msdn.microsoft.com/en-us/library/ms973907.aspx
  94. RemotingConfiguration.Configure("CSRemotingClient.exe.config", true);
  95. try
  96. {
  97. //
  98. // Create a remotable object.
  99. //
  100. // Create a SingleCall server-activated proxy.
  101. SingleCallObject remoteObj = new SingleCallObject();
  102. Console.WriteLine("A SingleCall server-activated proxy is created");
  103. // [-or-] Create a Singleton server-activated proxy.
  104. //SingletonObject remoteObj = new SingletonObject();
  105. //Console.WriteLine("A Singleton server-activated proxy is created");
  106. // [-or-] Create a client-activated object.
  107. //ClientActivatedObject remoteObj = new ClientActivatedObject();
  108. //Console.WriteLine("A client-activated object is created");
  109. //
  110. // Use the remotable object as if it were a local object.
  111. //
  112. string remoteType = remoteObj.GetRemoteObjectType();
  113. Console.WriteLine("Call GetRemoteObjectType => {0}", remoteType);
  114. Console.WriteLine("The client process and thread: {0}, {1}",
  115. Process.GetCurrentProcess().Id, GetCurrentThreadId());
  116. uint processId;
  117. uint threadId;
  118. remoteObj.GetProcessThreadID(out processId, out threadId);
  119. Console.WriteLine("Call GetProcessThreadID => {0} {1}", processId, threadId);
  120. Console.WriteLine("Set FloatProperty += {0}", 1.2f);
  121. remoteObj.FloatProperty += 1.2f;
  122. Console.WriteLine("Get FloatProperty = {0}", remoteObj.FloatProperty);
  123. }
  124. catch (SocketException ex)
  125. {
  126. Console.WriteLine(ex.Message);
  127. }
  128. }
  129. #endregion
  130. #region Access the .NET Remoting server using code.
  131. /// <summary>
  132. /// Access the .NET Remoting server using code.
  133. /// </summary>
  134. static void AccessRemotingServerByCode()
  135. {
  136. // Create and register a channel (TCP channel in this example) that
  137. // is used to transport messages across the remoting boundary.
  138. // Set the properties of the channel.
  139. IDictionary props = new Hashtable();
  140. props["typeFilterLevel"] = TypeFilterLevel.Full;
  141. // Set the formatters of the messages for delivery.
  142. BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
  143. BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
  144. serverProvider.TypeFilterLevel = TypeFilterLevel.Full;
  145. // Create a TCP channel.
  146. TcpChannel tcpChannel = new TcpChannel(props, clientProvider, serverProvider);
  147. // Register the TCP channel.
  148. ChannelServices.RegisterChannel(tcpChannel, true);
  149. try
  150. {
  151. //
  152. // Create a remotable object.
  153. //
  154. // Create a SingleCall server-activated proxy.
  155. SingleCallObject remoteObj = (SingleCallObject)Activator.GetObject(
  156. typeof(SingleCallObject),
  157. "tcp://localhost:6100/SingleCallService");
  158. Console.WriteLine("A SingleCall server-activated proxy is created");
  159. // [-or-] Create a Singleton server-activated proxy
  160. //SingletonObject remoteObj = (SingletonObject)Activator.GetObject(
  161. // typeof(SingletonObject),
  162. // "tcp://localhost:6100/SingletonService");
  163. //Console.WriteLine("A Singleton server-activated proxy is created");
  164. // [-or-] Create a client-activated object
  165. //RemotingConfiguration.RegisterActivatedClientType(
  166. // typeof(ClientActivatedObject),
  167. // "tcp://localhost:6100/RemotingService");
  168. //ClientActivatedObject remoteObj = new ClientActivatedObject();
  169. //Console.WriteLine("A client-activated object is created");
  170. //
  171. // Use the remotable object as if it were a local object.
  172. //
  173. string remoteType = remoteObj.GetRemoteObjectType();
  174. Console.WriteLine("Call GetRemoteObjectType => {0}", remoteType);
  175. Console.WriteLine("The client process and thread: {0}, {1}",
  176. Process.GetCurrentProcess().Id, GetCurrentThreadId());
  177. uint processId;
  178. uint threadId;
  179. remoteObj.GetProcessThreadID(out processId, out threadId);
  180. Console.WriteLine("Call GetProcessThreadID => {0} {1}", processId, threadId);
  181. Console.WriteLine("Set FloatProperty += {0}", 1.2f);
  182. remoteObj.FloatProperty += 1.2f;
  183. Console.WriteLine("Get FloatProperty = {0}", remoteObj.FloatProperty);
  184. }
  185. catch (SocketException ex)
  186. {
  187. Console.WriteLine(ex.Message);
  188. }
  189. }
  190. #endregion
  191. /// <summary>
  192. /// Get current thread ID.
  193. /// </summary>
  194. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  195. internal static extern uint GetCurrentThreadId();
  196. }
  197. }