C# Source Code: Using non default constructors when creating Remote objects
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Using non default constructors when creating Remote objects
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, April 13, 2004
Hits:
663
Category:
Remoting/Web Services
Article:
If your application design requires that your remote objects are created with non default constructs then you should implement a ROC ("remote object creator"), in which your Server Activated Object will include parameterised methods that return new instances of the Client Activated Object. These parameterised methods will supply any non default constructors that you wish to use. Below are three example projects which demonstrates this process using .NET remoting service. The code is split into three name spaces (projects): 1. vbUsers.Remoting.Client. This is the client code. 2. vbUsers.Remoting.Server. This is the server code. 3. vbUsers.Remoting.Shared. This is the shared library that you will need to reference from both the other projects. Do this by opening either of the other two projects. Next right click on the project solution in the solution explorer and select "Add" > "Existing Project", then browse to the location of the share assembly and double click on the solution (.sln) file. Finally, right click on either the server or client project (depending on which one you are adding the reference to) in the solution explorer and select "Add Reference". Then click on the "Projects" tab and double click the share assembly project in the top window and then press "OK". Notes: 1. All the client and server projects should be created as console applications with the shared library created as class library. 2. You will need to add a reference to the "System.Runtime.Remoting" assembly. /* ----------SHARED PROJECT CODE---------- --------------------------------------- --------------------------------------- Copyright Andrew Baker (www.vbusers.com) */ using System; //The shared interface required by the client and the server namespace vbUsers.Remoting.Shared { public interface IRemoteData { string Message { get; set; } } public interface IRemoteObjectCreator { IRemoteData getRemoteData(); IRemoteData getRemoteData(string message); } } /* ----------SERVER PROJECT CODE---------- --------------------------------------- --------------------------------------- Copyright Andrew Baker (www.vbusers.com) */ using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Messaging; //References to Private assemblies using vbUsers.Remoting.Shared; namespace vbUsers.Remoting.Server { //Class containing the implimentation of the remote data object class RemoteData: MarshalByRefObject, IRemoteData { protected string zsMessage; //Constructors public RemoteData(string message) { Console.WriteLine("Created RemoteData({0})",message); zsMessage = message; } public RemoteData() { Console.WriteLine("Created RemoteData()"); } //Properties public string Message { get{return zsMessage;} set{zsMessage = value;} } } class RemoteObjectCreator: MarshalByRefObject,IRemoteObjectCreator { public RemoteObjectCreator() { Console.WriteLine("Created RemoteObjectCreator()"); } public IRemoteData getRemoteData() { Console.WriteLine("getRemoteData.getRemoteData()"); return new RemoteData(); } public IRemoteData getRemoteData(string message) { Console.WriteLine("getRemoteData.getRemoteData({0})",message); return new RemoteData(message); } } //Server component class ServerStartup { static void Main(string[] args) { Console.WriteLine ("ServerStartup.Main()"); HttpChannel chnl = new HttpChannel(1234); ChannelServices.RegisterChannel(chnl); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObjectCreator),"RemoteObjectCreator.soap",WellKnownObjectMode.Singleton); //Suspend the current thread so that the server keeps running. System.Threading.Thread.CurrentThread.Suspend(); } } } /* ----------CLIENT PROJECT CODE---------- --------------------------------------- --------------------------------------- Copyright Andrew Baker (www.vbusers.com) */ using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels.Http; using System.Runtime.Remoting.Channels.Tcp; using System.Runtime.Remoting.Channels; //References to Private assemblies using vbUsers.Remoting.Shared; namespace vbUsers.Remoting.Client { class Client { static void Main(string[] args) { Console.WriteLine("Client.Main"); //Register channels HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); //Register server object Console.WriteLine("Client.Main. Getting reference to Server Object"); IRemoteObjectCreator oRemoteObjectCreator = (IRemoteObjectCreator)Activator.GetObject(typeof(IRemoteObjectCreator),"http://localhost:1234/RemoteObjectCreator.soap"); Console.WriteLine("Client.Main: Creating byval objects using server instance of IRemoteObjectCreator"); //Create an object with using the default constructor IRemoteData obj1 = oRemoteObjectCreator.getRemoteData(); obj1.Message = "Object create with default constructor"; //Create an object with using the default constructor Console.WriteLine("Client.Main(): Acquiring second object from oRemoteObjectCreatorory"); IRemoteData obj2 = oRemoteObjectCreator.getRemoteData("Object created with non default constructor!"); Console.WriteLine("obj1.Message: {0}",obj1.Message); Console.WriteLine("obj2.Message: {0}",obj2.Message); Console.ReadLine(); } } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet