Click here to Skip to main content
11,640,866 members (65,124 online)
Click here to Skip to main content
Add your own
alternative version

.NET Remoting – Basic Maneuvers

, , 6 Oct 2003 159.5K 9.6K 126
A tutorial on .NET Remoting
cremoteobj.zip
CRemoteObj
CRemoteObj.csproj.user
cremoteobjclient.zip
CRemoteObjClient
App.ico
config file
CRemoteObjClient.csproj.user
cremoteobjinterface.zip
CRemoteObjInterface
CRemoteObjInterface.csproj.user
cremoteobjserver.zip
CRemoteObjServer
App.ico
config file
CRemoteObjServer.csproj.user
web_vdir
bin
CRemoteObj.dll
CRemoteObjInterface.dll
CRemoteObjServer.exe
using System;
using System.Runtime.Remoting.Messaging; //For [OneWay] attribute.

namespace nsCRemoteObjInterface
{

	//(1) Refer to CRemoteObj.UpdateProfile
	//(2) Marked Serializable since this object will be passed back and forth between remote object and client.
	[Serializable]
	public class CProfile
	{
		public string fname;
		public string lname;
		public string user;
		public string passwd;

		public CProfile()
		{
		}

		public CProfile(string firstname, string lastname, string username, string password)
		{
			fname=firstname;
			lname=lastname;
			user=username;
			passwd=password;
		}

	};

	//ATTENTION: 
	//(1) You must either put this class in this interface module or in remote object class (CRemoteObj.cs).
	//Otherwise, you may get some reflection problem when you try to register StatusHandler with event evStatus -
	//The framework will look for StatusHandler when in client code:
	//		CStatusEventSink esStatusEvSink = new CStatusEventSink();
	//		StatusEvent event_delegate = new StatusEvent(esStatusEvSink.StatusHandler);
	//		obj.evStatus += event_delegate; 
	//When obj.evStatus += event_delegate, the framework loads the assembly that contains the handler - in this case, it's the sink class.
	//(2) Since event sink is instantiated in client's code. Event handler runs in client process.
	//(3) For one reason or another (actually, event is fired on remote object/server side, and handler sink is on the client side), event sink class need to derive from MarshalByRefObject.
	//(4) And, methods it implements must be asynchronous - marked by [OneWay] attribute.
	public class CStatusEventSink : MarshalByRefObject
	{
		public CStatusEventSink()
		{

		}

		[OneWay]
		public void StatusHandler(object sender, StatusEventArgs e)
		{
			Console.Write("StatusHandler fired. ");
			Console.WriteLine("Event message: {0}", e.Message);
		}

	};


	//ATTN:
	//When CRemoteObj.AuthenticateLoser() fires "event StatusEvent evStatus", an instance of StatusEventArgs will be serialized to an instance of CStatusEventSink in client's process space.
	//Therefore, StatusEventArgs must be marked Serializable.
	[Serializable]
	public class StatusEventArgs //Event argument class
	{
		public StatusEventArgs()
		{
			message="Event fired!";
		}

		public StatusEventArgs(string m)
		{
			message=m;
		}

		public string Message
		{
			get { return message; }
			set { message = Message; }
		}

		private string message;
	};


	public interface IRemoteObj
	{
		//Object exposes three dummy methods to client:
		bool AuthenticateLoser(string sUserName, string sPasswd); //simple in/out/return.
		string SetupUser(string sname, string spasswd, out string sTime); //output+return values
		CProfile UpdateProfile(CProfile p); //Pass custom object in/out.

		//Get assembly version from here:
		string GetAssembly();

		
        
	};
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Authors

raymond.fung
Web Developer
Hong Kong Hong Kong
No Biography provided

Norman Fung
Hong Kong Hong Kong
Me too, I'm a developer.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 7 Oct 2003
Article Copyright 2003 by raymond.fung, Norman Fung
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid