C# Source Code: A Simple Demonstration Project using .NET Remoting
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
A Simple Demonstration Project using .NET Remoting
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Monday, April 12, 2004
Hits:
833
Category:
Remoting/Web Services
Article:
Below are three example projects which demonstrates how simple it is to set up a .NET remoting service. The code is split into three name spaces (projects): 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; using System.Diagnostics; namespace vbUsers.Remoting.Shared { //Interface of server work manager (client calls this but object only exists on server) public interface IWorkerManager { //Create a new worker Worker getWorker(string WorkerName); //Valid the worker ValidationObject validateWorker(Worker wrker); } //Object to be passed by Val (copied) to client, therefore mark it as "Serializable" [Serializable] public class ValidationObject { //Constructor for returning validation details public ValidationObject (bool Ret, String msg) { Console.WriteLine("Process {0}, Thread {1}. Validation Object created",Process.GetCurrentProcess().Id,AppDomain.GetCurrentThreadId()); this.Success = Ret; this.Message = msg; } public bool Success; public string Message; } //Object to be passed by Val (copied) to client, therefore mark it as "Serializable" [Serializable] public class Worker { public string WorkerName; public DateTime StartDate; public string JobName; public int WorkerID; //Constructor public Worker(string workerName, string jobName, DateTime startDate, int workerID) { this.WorkerName = workerName; this.JobName = jobName; this.StartDate = startDate; this.WorkerID = workerID; Console.WriteLine("Process {0}, Thread {1}. Worker " + workerName + " created for job " + JobName,Process.GetCurrentProcess().Id,AppDomain.GetCurrentThreadId()); } } } /* ----------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; using System.Diagnostics; //Private assemblies using vbUsers.Remoting.Shared; namespace vbUsers.Remoting.Client { class Client { static void Main(string[] args) { //Starting client Console.WriteLine("Process {0}, Thread {1}. Client started",Process.GetCurrentProcess().Id,AppDomain.GetCurrentThreadId()); //Registering HTTP channels HttpChannel channel = new HttpChannel(); ChannelServices.RegisterChannel(channel); //Create Local Proxy containing IWorkerManager interface IWorkerManager srvWkrMgr = (IWorkerManager)Activator.GetObject(typeof(IWorkerManager), "http://localhost:1234/WorkerManager.soap"); while(true) { //Request Worker name Console.WriteLine("-----------------------------------"); Console.WriteLine("Please enter workers name: "); string sWrkName = Console.ReadLine(); if(sWrkName.Length==0) { //No worker name given break; } //Request Worker from server Console.WriteLine("Process {0}, Thread {1}. Client Requesting Worker {0}",Process.GetCurrentProcess().Id,AppDomain.GetCurrentThreadId(), sWrkName); Worker wrker = srvWkrMgr.getWorker(sWrkName); //Perform server side validation on worker ValidationObject validate = srvWkrMgr.validateWorker(wrker); Console.WriteLine("Process {0}, Thread {1}. Validation result for {2} success = {3}. {4}",Process.GetCurrentProcess().Id,AppDomain.GetCurrentThreadId(), wrker.WorkerName, validate.Success.ToString(),validate.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.Diagnostics; //Private assemblies using vbUsers.Remoting.Shared; namespace vbUsers.Remoting.Server { class WorkerManager: MarshalByRefObject, IWorkerManager { protected int WorkerID = 0; //Returns a new worker (by val) to the client public Worker getWorker(string WorkerName) { //Increment the worker ID ++WorkerID; Console.WriteLine("Process {0}, Thread {1}. Server.WorkerManager.getWorker(Name:={2},ID:={3})",Process.GetCurrentProcess().Id,AppDomain.GetCurrentThreadId(),WorkerName,WorkerID); //Start the worker start time to be 10 minutes in the future DateTime nextStartTime = DateTime.Now; nextStartTime = nextStartTime.AddMinutes(10); //Create the new worker object Worker oWrker = new Worker(WorkerName,"DigHole",nextStartTime,WorkerID); //Return the object to the client return oWrker; } //Place business logic on server to validate that the worker is setup correctly public ValidationObject validateWorker(Worker oWrk) { Console.WriteLine("Process {0}, Thread {1}. Server.WorkerManager.validateWorker{2}",Process.GetCurrentProcess().Id,AppDomain.GetCurrentThreadId(), oWrk.WorkerName); ValidationObject tmp = new ValidationObject(true,""); if(oWrk == null) { tmp.Success = false; tmp.Message = "Invalid Job Type."; return tmp; } switch(oWrk.JobName) { case "DigHole": break; default: tmp.Success =false; tmp.Message = "Invalid Job Type."; break; } if(oWrk.StartDate < DateTime.Now) { tmp.Success =false; tmp.Message += "Invalid Job Start Date"; } return tmp; } } //Server component class ServerStartup { static void Main(string[] args) { Console.WriteLine("Process {0}, Thread {1}. Server started",Process.GetCurrentProcess().Id,AppDomain.GetCurrentThreadId()); //Create and configure a new HTTP channel to listen on port 1234. Note, the default transfer format for HTTP is SOAP. HttpChannel chnl = new HttpChannel(1234); ChannelServices.RegisterChannel(chnl); //Register the WorkerManager. The URL will be WorkerManager.soap. Although you can change this to be anything you want, //you should use .soap for consistency. Registering the object as Singleton will ensure only one copy of the manager is //ever created. RemotingConfiguration.RegisterWellKnownServiceType(typeof(WorkerManager),"WorkerManager.soap", WellKnownObjectMode.Singleton); //Suspend the current thread so that the server keeps running. Console.WriteLine("Process {0}, Thread {1}. Server Idle...",Process.GetCurrentProcess().Id,AppDomain.GetCurrentThreadId()); System.Threading.Thread.CurrentThread.Suspend(); } } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet