Click here to Skip to main content
15,886,055 members
Articles / Programming Languages / C#

.NET Remoting – Basic Maneuvers

,
Rate me:
Please Sign up or sign in to vote.
4.46/5 (51 votes)
6 Oct 200313 min read 191.2K   9.9K   126  
A tutorial on .NET Remoting
using System;
using System.Collections;
using System.Threading;

using nsCRemoteObjInterface; //For IRemoteObj interface.


namespace nsCRemoteObj
{
	//Event delegate:
	public delegate void StatusEvent(object sender, StatusEventArgs e);


	public class CRemoteObj : System.MarshalByRefObject, IRemoteObj
	{
		//That's your EVENT here.
		public event StatusEvent evStatus;	

		//Indicating the number of times your method AuthenticateLoser has been invoked - during the life time of "this" instance.
		private int num_of_times_invoked;	

		public CRemoteObj()
		{
			Random rand = new Random((int)System.DateTime.Now.Millisecond + System.DateTime.Now.Second + System.DateTime.Now.Minute);
			num_of_times_invoked=0;
					
			m_nObjID=(int) rand.Next(0,100);
			Console.WriteLine("CRemoteObj constructor invoked.");
		}

		~CRemoteObj()
		{
			Console.WriteLine();
			Console.WriteLine();
			Console.WriteLine("CRemoteObj destructor invoked for objID:{0}", this.ObjID);
			Console.WriteLine();
		}

		public int ObjID
		{
			get { return m_nObjID; }
			set { m_nObjID=value; }
		}

		private int m_nObjID;

		public bool AuthenticateLoser(string sUserName, string sPasswd)
		{
			bool bAuth = false;

			System.Collections.Hashtable usertable = new System.Collections.Hashtable();

			usertable["Paul"]="12345";
			usertable["Jack"]="45678";
			usertable["Sascha"]="12345";
			usertable["Mehdi"]="45678";

			Console.WriteLine("AuthenticateLoser invoked. ObjID:{0}; times invoked:{1}", ObjID.ToString(), this.num_of_times_invoked.ToString());
			Console.WriteLine("Authenticating...");
			for(int i=0; i<3; i++)
			{
				Console.Write("...");
				Thread.Sleep(500);
			}
			Console.WriteLine();

			if( usertable.ContainsKey(sUserName) )
			{
				if( usertable[sUserName].ToString() == sPasswd )
				{
					bAuth=true;
				}
			}

			//Fire the event - if a delegate is associated with the event on client side:
			StatusEventArgs eaStatusArg = new StatusEventArgs("Authentication completed.");
			if(evStatus!=null)
			{
				Console.WriteLine("RemoteObj firing event on server side.!");

				//Fire! eaStatusArg will be streamed to client side where the event is handled by sink class handler - therefore, it's got to be marked "Serializable".
				evStatus(this, eaStatusArg); 
			}
			else
			{
				Console.WriteLine("Authentication completed.{0}No delegate attached to evStatus event. Event not fired.", Environment.NewLine);
				Console.WriteLine();
			}

			return bAuth;
		}

		public string SetupUser(string sname, string spasswd, out string sTime)
		{
			Console.WriteLine("CRemoteObj.SetupUser invoked. Sleep 10 sec.");

			string msg;

			DateTime dt = new DateTime();
			dt = System.DateTime.Now;
			
			sTime = dt.ToString();	//Out

			msg = sname+spasswd+sTime;

			Thread.Sleep(10000);	//Sleep 10 seconds.

			Console.WriteLine("CRemoteObj.SetupUser return");

			return msg;
		}

		public CProfile UpdateProfile(CProfile p)
		{
			Console.WriteLine("CRemoteObj.UpdateProfile invoked.");
			Console.WriteLine("user = {0}, passwd = {1}", p.user, p.passwd);
			
			CProfile pout = p;
			pout.passwd= (new Random(DateTime.Now.Minute).Next(1000000)).ToString();
			Console.WriteLine("password override: {0}", pout.passwd);

			return pout;
		}

		public string GetAssembly()
		{
			string assem_info;
			assem_info = "assembly info (CRemoteObj):" + System.AppDomain.CurrentDomain.GetAssemblies().GetValue(0).ToString();
			return assem_info;
		}

	}
}

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


Written By
Hong Kong Hong Kong
Me too, I'm a developer.

Written By
Web Developer
Hong Kong Hong Kong
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions