Archive for April, 2008

CHAPTER 18 (Yahoo web space) THE .NET REMOTING LAYER 591

Monday, April 21st, 2008

CHAPTER 18 THE .NET REMOTING LAYER 591 Finally, you need to update the client application, not only by way of the *.config file (or programmatically in the code base) to request access to the remote CAO, but also to indeed trigger the custom constructor of the CarProvider type. Here are the relevant updates to the client-side Main() method: static void Main(string[] args) { // Read updated *.config file. RemotingConfiguration.Configure(”CAOCarProviderClient.exe.config”); // Create array of types to pass to provider. JamesBondCar[] cars = { new JamesBondCar(”Viper”, 100, true, false), new JamesBondCar(”Shaken”, 100, false, true), new JamesBondCar(”Stirred”, 100, true, true) }; // Now trigger the custom ctor. CarProvider cp = new CarProvider(cars); … } The updated client-side *.config file also makes use of the element, as opposed to . In addition, the element now requires the url property to define the location of the registered CAO. Recall that when the server registered the CarProvider as aWKO, the client specified such information within the element. If you would rather hard-code the client s request to the CAO type, you can make use of the RegistrationServices.RegisterActivatedClientType() method as follows: static void Main(string[] args) { // Use hard-coded values. RemotingConfiguration.RegisterActivatedClientType( typeof(CAOCarGeneralAsm.CarProvider), “tcp://localhost:32469″); … } If you now execute the updated server and client assemblies, you will be pleased to find that you are able to pass your custom array of JamesBondCar types to the remote CarProvider via the overloaded constructor.
We recommend high quality webhost to host and run your jsp application: christian web host services.

CHAPTER 18 (Web host music) 590 THE .NET REMOTING LAYER

Sunday, April 20th, 2008

CHAPTER 18 590 THE .NET REMOTING LAYER Understanding Client-Activated Objects All of these current remoting examples have made use of WKOs. Recall that WKOs have the following characteristics: WKOs can be configured either as singleton or single call. WKOs can only be activated using the type s default constructor. WKOs are instantiated on the server on the first client-side member invocation. CAO types on the other hand, can be instantiated using any constructor on the type and are created at the point the client makes use of the C# new keyword or Activator type. Furthermore, the lifetime of CAO types is monitored by the .NET leasing mechanism. Do be aware that when you configure a CAO type, the .NET remoting layer will generate a specific CAO remote object to service each client. Again, the big distinction is the fact that CAOs are always alive (and therefore stateful) beyond a single method invocation. To illustrate the construction, hosting, and consumption of CAO types, let s retrofit the previous automobile-centric general assembly. Assume that your MBR CarProvider class has defined an additional constructor that allows the client to pass in an array of JamesBondCar types that will be used to populate the generic List<>: public class CarProvider : MarshalByRefObject { private List theJBCars = new List(); public CarProvider(JamesBondCar[] theCars) { Console.WriteLine(”Car provider created with custom ctor”); theJBCars.AddRange(theCars); } … } To allow the caller to activate the CarProvider using your new constructor syntax, you need to build a server application that registers CarProvider as a CAO type rather than aWKO type. This may be done programmatically ( la the RemotingConfiguration.RegisterActivatedServiceType() method) or using a server-side *.config file. If you wish to hard-code the name of the CAO object within the host server s code base, all you need to do is pass in the type information of the type(s) (after creating and registering a channel) as follows: // Hard-code the fact that CarProvider is a CAO type. RemotingConfiguration.RegisterActivatedServiceType( typeof(CAOCarGeneralAsm.CarProvider)); If you would rather leverage the *.config file, replace the element with the element as follows:
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Mac os x web server - CHAPTER 18 THE .NET REMOTING LAYER 589

Wednesday, April 16th, 2008

CHAPTER 18 THE .NET REMOTING LAYER 589 static void Main(string[] args) { RemotingConfiguration.Configure(”CarProviderClient.exe.config”); // Make the car provider. CarProvider cp = new CarProvider(); // Get first JBC. JamesBondCar qCar = cp.GetJBCByIndex(0); // Get all JBCs. List allJBCs = cp.GetAllAutos(); // Use first car. UseCar(qCar); // Use all cars in List<>. foreach(JamesBondCar j in allJBCs) UseCar(j); Console.WriteLine(”Client started! Hit enter to end”); Console.ReadLine(); } } } The client side *.config file is also what you would expect. Simply update the activation URL: Now, run your server and client applications (in that order, of course) and observe the output. Your client-side console window will whirl through the JamesBondCars and print out the statistics of each type. Recall that as you interact with the List<> and JamesBondCar types, you are operating on their members within the client s application domain, as they have both been marked with the [Serializable] attribute. To prove that point, update the UseCar() helper function to call the TurnOnRadio() method on the incoming JamesBondCar. Now, run the server and client applications once again. Notice that the message box appears on the client machine! Had the Car, Radio, and JamesBondCar types been configured as MBR types, the server would be the machine displaying the message box prompts. If you wish to verify this, derive each type from MarshalByRefObject and recompile all three assemblies (to ensure Visual Studio 2005 copies the latest CarGeneralAsm.dll into the client s and server s application directory). When you run the application once again, the message boxes appear on the remote machine. Source Code The CarGeneralAsm, CarProviderServer, and CarProviderClient projects are located under the Chapter 18 subdirectory.
You want to have a cheap webhost for your apache application, then check apache web hosting services.

CHAPTER 18 588 THE .NET REMOTING LAYER (Web hosting bandwidth)

Tuesday, April 15th, 2008

CHAPTER 18 588 THE .NET REMOTING LAYER namespace CarProviderServer { class CarServer { static void Main(string[] args) { RemotingConfiguration.Configure(”CarProviderServer.exe.config”); Console.WriteLine(”Car server started! Hit enter to end”); Console.ReadLine(); } } } The related *.config file is just about identical to the server-side *.config file you created in the previous example. The only point of interest is to define an object URI value that makes sense for the CarProvider type: Building the Client Assembly Last but not least, we have the client application that will make use of the MBR CarProvider type in order to obtain discrete JamesBondCars types as well as the List<> type. Once you obtain a type from the CarProvider, you ll send it into the UseCar() helper function from processing: using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using CarGeneralAsm; using System.Collections.Generic; namespace CarProviderClient { class CarClient { private static void UseCar(JamesBondCar c) { Console.WriteLine(”-> Name: {0}”, c.PetName); Console.WriteLine(”-> Max speed: {0}”, c.MaxSpeed); Console.WriteLine(”-> Seaworthy? : {0}”, c.isSeaWorthy); Console.WriteLine(”-> Flight worthy? : {0}”, c.isFlightWorthy); Console.WriteLine(); }
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Web hosting india - CHAPTER 18 THE .NET REMOTING LAYER 587

Monday, April 14th, 2008

CHAPTER 18 THE .NET REMOTING LAYER 587 namespace CarGeneralAsm { // This type is an MBR object that provides // access to related MBV types. public class CarProvider : MarshalByRefObject { private List theJBCars = new List(); // Add some cars to the list. public CarProvider() { Console.WriteLine(”Car provider created”); theJBCars.Add(new JamesBondCar(”QMobile”, 140, true, true)); theJBCars.Add(new JamesBondCar(”Flyer”, 140, true, false)); theJBCars.Add(new JamesBondCar(”Swimmer”, 140, false, true)); theJBCars.Add(new JamesBondCar(”BasicJBC”, 140, false, false)); } // Get all the JamesBondCars. public List GetAllAutos() { return theJBCars; } // Get one JamesBondCar. public JamesBondCar GetJBCByIndex(int i) { return (JamesBondCar)theJBCars[i]; } } } Notice that the GetAllAutos() method returns the internal List<> type. The obvious question is how this member of the System.Collections.Generic namespace is marshaled back to the caller. If you look up this type using the .NET Framework 2.0 SDK documentation, you will find that List<> has been decorated with the [Serializable] attribute: [SerializableAttribute()] public class List : IList, ICollection, IEnumerable Therefore, the entire contents of the List<> type will be marshaled by value to the caller (provided the contained types are also serializable)! This brings up a very good point regarding .NET remoting and members of the base class libraries. In addition to the custom MBV and MBR types you may create yourself, understand that any type in the base class libraries that is decorated with the [Serializable] attribute is able to function as an MBV type in the .NET remoting architecture. Likewise, any type that derives (directly or indirectly) from MarshalByRefObject will function as an MBR type. Note Be aware that the SoapFormatter does not support serialization of generic types. If you build methods that receive or return generic types (such as the List<>), you must make use of the BinaryFormatter and the TcpChannel object. Building the Server Assembly The server host assembly (CarProviderServer.exe) has the following logic within Main(): using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; using CarGeneralAsm;
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Web site builder - CHAPTER 18 586 THE .NET REMOTING LAYER

Sunday, April 13th, 2008

CHAPTER 18 586 THE .NET REMOTING LAYER you are able to update the client s Main() method as follows: static void Main(string[] args) { RemotingConfiguration.Configure(”SimpleRemoteObjectClient.exe.config”); // Using *.config file, the client is able to directly ‘new’ the type. RemoteMessageObject simple = new RemoteMessageObject(); simple.DisplayMessage(”Hello from the client!”); Console.WriteLine(”Server says: {0}”, simple.ReturnMessage()); Console.WriteLine(”Client started! Hit enter to end”); Console.ReadLine(); } Of course, when you run the application, the output is identical. If the client wishes to make use of the TCP channel, the url property of the element and ref property must make use of the tcp token in place of http. Source Code The SimpleRemoteObjectServerWithConfig and SimpleRemoteObjectClientWithConfig projects are located under the Chapter 18 subdirectory (both of which make use of the SimpleRemotingAsm.dll created previously). Working with MBV Objects Our first remoting applications allowed client-side access to a single WKO type. Recall that WKO types are (by definition) MBR types, and therefore client access takes place via an intervening proxy. In contrast, MBV types are local copies of a server-side object, which are typically returned from a public member of an MBR type. Although you already know how to configure an MBV type (mark a class with the [Serializable] attribute), you have not yet seen an example of MBV types in action (beyond passing string data between the two parties). To illustrate the interplay of MBR and MBV types, let s see another example involving three assemblies: The general assembly named CarGeneralAsm.dll The client assembly named CarProviderClient.exe The server assembly named CarProviderServer.exe As you might assume, the code behind the client and server applications is more or less identical to the previous example, especially since these applications will again make use of *.config files. Nevertheless, let s step through the process of building each assembly one at a time. Building the General Assembly During our examination of object serialization in Chapter 17, you created a type named JamesBondCar (in addition to the dependent Radio and Car classes). The CarGeneralAsm.dll code library will reuse these types, so begin by using the Project .Add Existing Item menu command and include these *.cs files into this new Class Library project (the automatically provided Class1.cs file can be deleted). Given that each of these types has already been marked with the [Serializable] attribute, they are ready to be marshaled by value to a remote client. All you need now is an MBR type that provides access to the JamesBondCar type. To make things a bit more interesting, however, your MBR object (CarProvider) will maintain a generic List<> of JamesBondCar types. CarProvider will also define two members that allow the caller to obtain a specific JamesBondCar as well as receive the entire List<> of types. Here is the complete code for the new class type:
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

Ftp web hosting - CHAPTER 18 THE .NET REMOTING LAYER 585

Saturday, April 12th, 2008

CHAPTER 18 THE .NET REMOTING LAYER 585 Notice that much of the relevant server-side remoting information is wrapped within the scope of the (not server) element. The child element makes use of three attributes (mode, type, and objectUri) to specify the well-known object to register with the .NET remoting layer. The child element contains any number of elements that allow you to define the type of channel (in this case, HTTP) to open on the server. TCP channels would simply make use of the tcp string token in place of http. As the SimpleRemoteObjectServer.exe.config file contains all the necessary information, the server-side Main() method cleans up considerably. All you are required to do is make a single call to RemotingConfiguration.Configure() and specify the name of your configuration file. static void Main(string[] args) { // Register a ‘well-known’ object using a *.config file. RemotingConfiguration.Configure(”SimpleRemoteObjectServer.exe.config”); Console.WriteLine(”Server started! Hit enter to end”); Console.ReadLine(); } Building Client-Side *.config Files Clients are also able to leverage remoting-centric *.config files. Unlike a server-side configuration file, client-side configuration files make use of the element to identify the name of the well-known object the caller wishes to interact with. In addition to providing the ability to dynamically change the remoting information without the need to recompile the code base, client-side *.config files allow you to create the proxy type directly using the C# new keyword, rather than the Activator.GetObject() method. Thus, if you have the following client-side *.config file:
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Web site design - CHAPTER 18 584 THE .NET REMOTING LAYER

Friday, April 11th, 2008

CHAPTER 18 584 THE .NET REMOTING LAYER Remoting Configuration Files At this point you have successfully built a distributed application using the .NET remoting layer. One issue you may have noticed in these first examples is the fact that the client and the server applications have a good deal of hard-coded logic within their respective binaries. For example, the server specifies a fixed port ID, fixed activation mode, and fixed channel type. The client, on the other hand, hard-codes the name of the remote object it is attempting to interact with. As you might agree, it is wishful thinking to assume that initial design notes remain unchanged once an application is deployed. Ideally, details such as port ID and object activation mode (and whatnot) could be altered on the fly without needing to recompile and redistribute the client or server code bases. Under the .NET remoting scheme, all the aforementioned issues can be circumvented using the remoting configuration file. As you will recall from Chapter 11, *.config can be used to provide hints to the CLR regarding the loading of externally referenced assemblies. The same *.config files can be used to inform the CLR of a number of remoting-related details, on both the client side and the server side. When you build a remoting *.config file, the element is used to hold various remoting-centric details. Do be aware that if you re building an application that already has a *.config file that specifies assembly resolution details, you re free to add remoting elements within the same file. Thus, a single *.config file that contains remoting and binding information would look something like this: If your configuration file has no need to specify assembly binding logic, you can omit the element and make use of the following skeleton *.config file: Building Server-Side *.config Files Server-side configuration files allow you to declare the objects that are to be reached via remote invocations as well as channel and port information. Basically, using the , , and elements, you are able to replace the following server-side logic: // Hard-coded HTTP server logic. HttpChannel c = new HttpChannel(32469); ChannelServices.RegisterChannel(c); RemotingConfiguration.RegisterWellKnownServiceType( typeof(SimpleRemotingAsm.RemoteMessageObject), “RemoteMsgObj.soap”, WellKnownObjectMode.Singleton); with the following *.config file:
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

CHAPTER 18 THE .NET REMOTING LAYER 583 (Best web site)

Thursday, April 10th, 2008

CHAPTER 18 THE .NET REMOTING LAYER 583 { … // Create a new TcpChannel TcpChannel c = new TcpChannel(32469); ChannelServices.RegisterChannel(c); // Register a ‘well-known’ object in single call mode. RemotingConfiguration.RegisterWellKnownServiceType( typeof(SimpleRemotingAsm.RemoteMessageObject), “RemoteMsgObj.rem”, WellKnownObjectMode.SingleCall); Console.ReadLine(); } Notice that you are now registering a System.Runtime.Remoting.Channels.Tcp.TcpChannel type to the .NET remoting layer. Also note that the object URI has been altered to support a more generic name (RemoteMsgObj.rem) rather than the SOAP-centric *.soap extension. The client-side updates are equally as simple: // Client adjustments! using System.Runtime.Remoting.Channels.Tcp; … static void Main(string[] args) { … // Create a new TcpChannel TcpChannel c = new TcpChannel(); ChannelServices.RegisterChannel(c); // Get a proxy to remote object. object remoteObj = Activator.GetObject( typeof(SimpleRemotingAsm.RemoteMessageObject), “tcp://localhost:32469/RemoteMsgObj.rem”); // Use object. RemoteMessageObject simple = (RemoteMessageObject)remoteObj; simple.DisplayMessage(”Hello from the client!”); Console.WriteLine(”Server says: {0}”, simple.ReturnMessage()); Console.ReadLine(); } The only point to be aware of here is that the client s activation URL now must specify the tcp:// channel qualifier rather than http://. Beyond that, the bulk of the code base is identical to the previous HttpChannel logic. Source Code The TCPSimpleRemoteObjectServer and TCPSimpleRemoteObjectClient projects are located under the Chapter 18 directory (both projects use the SimpleRemotingAsm.dll created previously). A Brief Word Regarding the IpcChannel Before moving on to an examination of remoting configuration files, recall that .NET 2.0 also provides the IpcChannel type, which provides the fastest possible manner in which two applications on the same machine can exchange information. Given that this chapter is geared toward covering distributed programs that involve two or more computers, interested readers should look up IpcChannel in the .NET Framework 2.0 SDK documentation (as you might guess, the code is just about identical to working with HttpChannel and TcpChannel).
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

CHAPTER 18 (Web hosting servers) 582 THE .NET REMOTING LAYER

Wednesday, April 9th, 2008

CHAPTER 18 582 THE .NET REMOTING LAYER Once you have recompiled and run the server application, again launch three clients. This time you can see that a new RemoteMessageObject is created for each client request. As you might be able to gather, if you wish to share stateful data between multiple remote clients, singleton activation provides one possible alternative, as all clients are communicating with a single instance of the remote object. Source Code The SimpleRemotingAsm, SimpleRemoteObjectServer, and SimpleRemoteObjectClient projects are located under the Chapter 18 directory. Deploying the Server to a Remote Machine At this point, you have just crossed an application and process boundary on a single machine. If you re connected to an additional machine, let s extend this example to allow the client to interact with the RemoteMessageObject type across a machine boundary. To do so, follow these steps: 1. On your server machine, create and share a folder to hold your server-side assemblies. 2. Copy the SimpleRemoteObjectServer.exe and SimpleRemotingAsm.dll assemblies to this server-side share point. 3. Open your SimpleRemoteObjectClient project workspace and retrofit the activation URL to specify the name of the remote machine, for example: // Get a proxy to remote object. object remoteObj = Activator.GetObject( typeof(SimpleRemotingAsm.RemoteMessageObject), “http://YourRemoteBoxName:32469/RemoteMsgObj.soap”); 4. Execute the SimpleRemoteObjectServer.exe application on the server machine. 5. Execute the SimpleRemoteObjectClient.exe application on the client machine. 6. Sit back and grin. Note Activation URLs may specify a machine s IP address in place of its friendly name. Leveraging the TCP Channel Currently, your remote object is accessible via the HTTP network protocol. As mentioned, this protocol is quite firewall-friendly, but the resulting SOAP packets are a bit on the bloated side (given the nature of XML data representation). To lighten the payload, you can update the client and server assemblies to make use of the TCP channel, and therefore make use of the BinaryFormatter type behind the scenes. Here are the relevant updates to the server assembly: Note When you are defining an object to be URI-accessible via a TCP endpoint, it is common (but not required) to make use of the *.rem (i.e., remote) extension. // Server adjustments! using System.Runtime.Remoting.Channels.Tcp; … static void Main(string[] args)
Check Tomcat Web Hosting services for best quality webspace to host your web application.