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