Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Using MSMQ for Custom Remoting Channel.

, 10 Dec 2001
This article describes how to design, implement (C#) and configure the Custom Remoting Channel using MSMQ.
msmqchannel_src.zip
Client
bin
Debug
Client.exe
Client.pdb
MSMQChannelLib.dll
MSMQChannelLib.pdb
RemoteInterface.dll
RemoteInterface.pdb
Client.csproj.user
obj
Debug
Client.exe
Client.exe.incr
Client.pdb
temp
TempPE
MSMQChannelLib
bin
Debug
MSMQChannelLib.dll
MSMQChannelLib.pdb
MSMQchannelLib.csproj.user
MSMQChannelLib.snk
obj
Debug
MSMQChannelLib.dll
MSMQChannelLib.dll.incr
MSMQChannelLib.pdb
temp
TempPE
RemoteInterface
bin
Debug
RemoteInterface.dll
RemoteInterface.pdb
obj
Debug
RemoteInterface.dll
RemoteInterface.dll.incr
RemoteInterface.pdb
temp
TempPE
RemoteInterface.csproj.user
RemoteInterface.snk
RemoteObject
bin
Debug
RemoteInterface.dll
RemoteInterface.pdb
RemoteObject.dll
RemoteObject.pdb
obj
Debug
RemoteObject.dll
RemoteObject.dll.incr
RemoteObject.pdb
temp
TempPE
RemoteObject.csproj.user
RemoteObject.snk
Server
bin
Debug
MSMQChannelLib.dll
MSMQChannelLib.pdb
Server.exe
Server.pdb
obj
Debug
Server.exe
Server.exe.incr
Server.pdb
temp
TempPE
Server.csproj.user
//--------------------------------------------
// Written by Roman Kiss (rkiss@pathcom.com)
// December 5, 2001
// 
// History:
//			12-05-2001	RK	Initial Release	
//--------------------------------------------
using System;
using System.Threading;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

using RemoteInterface;
// using RKiss.MSMQChannelLib;		// necessary for option 1

public class Client 
{
	private delegate string delegateSayHello(string text);
	private delegate void delegateOneWay(string text);
	private delegate void delegateDoIt(int session); 

	// worker for sync calls
	public class WorkerSync 
	{
		public int result;
		private int numOfLoops;

		public WorkerSync(int numOfLoops) 
		{
			result = 0;
			this.numOfLoops = numOfLoops;
		}
		public void doit(int session) 
		{
			try 
			{
				// activate a remote object
				Type interfaceType = typeof(IRemInterface);
				string objectUrl = @"msmq://./reqchannel/endpoint"; 
				IRemInterface robj = (IRemInterface)Activator.GetObject(interfaceType, objectUrl);

				for(int ii = 0; ii < numOfLoops; ii++) 
				{
					robj.OneWay(string.Format("This is a Sync test #{0}.{1}", session, ii));
					Console.WriteLine(robj.SayHello(string.Format("World from Sync #{0}.{1}", session, ii)));
					lock(this) { result++; }
				}
				Trace.WriteLine(string.Format("obj.SayHello - Sync DONE #{0}", session));
			}  
			catch(Exception ex) 
			{
				Console.WriteLine(ex.Message);
			}
		}
	}

	// worker for Async calls
	public class WorkerAsync 
	{
		public int result;
		private int numOfLoops;

		public WorkerAsync(int numOfLoops) 
		{
			result = 0;
			this.numOfLoops = numOfLoops;
		}
		public void doit(int session) 
		{
			try 
			{
				// activate a remote object
				Type interfaceType = typeof(IRemInterface);
				string objectUrl = @"msmq://./reqchannel/endpoint"; 
				IRemInterface robj = (IRemInterface)Activator.GetObject(interfaceType, objectUrl);

				for(int ii = 0; ii < numOfLoops; ii++ ) 
				{							
					// OneWay
					delegateOneWay dow = new delegateOneWay(robj.OneWay);
					string arg = string.Format("This is a Async test #{0}.{1}", session, ii);
					IAsyncResult ar = dow.BeginInvoke(arg, null, null);
				
					// SayHello
					delegateSayHello dsh = new delegateSayHello(robj.SayHello);
					AsyncCallback ac = new AsyncCallback(OnAsyncResult);
					arg = string.Format("World from Async #{0}.{1}", session, ii);
					ar = dsh.BeginInvoke(arg, ac, dsh); 
					
				}
				Trace.WriteLine(string.Format("obj.SayHello - Async-BeginInvoke DONE #{0}", session));
			}  
			catch(Exception ex) 
			{
				Console.WriteLine(ex.Message);
			}
		}
		public void OnAsyncResult(IAsyncResult ar) 
		{	int num;

			delegateSayHello dsh = (delegateSayHello)ar.AsyncState;
			string retval = dsh.EndInvoke(ar);
			Console.WriteLine();
			Console.WriteLine("AsyncResult = {0}", retval);
			lock(this) { num = result++; }
			Trace.WriteLine(string.Format("obj.SayHello - Async done {0}", num));
		}
	}

	public static void Main()
	{
		int numOfLoops = 0;
		WorkerSync	wtSync = null;	
		WorkerAsync wtAsync = null;
	
		Console.Write("Enter number of test loops: ");
	
		try 
		{
			numOfLoops = Convert.ToInt32(Console.ReadLine());

			wtSync = new WorkerSync(numOfLoops);	
			wtAsync = new WorkerAsync(numOfLoops);	
			
			/*
			// option 1 
			MSMQSender chan = new MSMQSender();
			ChannelServices.RegisterChannel(chan);
			MSMQSender chan1 = new MSMQSender("msmq_1");
			ChannelServices.RegisterChannel(chan1);
			*/

			// option 2
			RemotingConfiguration.Configure(@"..\..\Client.exe.config");

			// create workers to perform this test
			for(int ii = 0; ii < numOfLoops; ii++) 
			{
				delegateDoIt ddtAsync = new delegateDoIt(wtAsync.doit);
				ddtAsync.BeginInvoke(ii, null, null);
				//
				delegateDoIt ddtSync = new delegateDoIt(wtSync.doit);
				ddtSync.BeginInvoke(ii, null, null);

			}
		}  
		catch(Exception ex) 
		{
			Console.WriteLine(ex.Message);
		}

		Console.Write("Hit <enter> to see a result of the test\n");
		Console.ReadLine();

		// result of the test
		int totalLoops = numOfLoops * numOfLoops;
		Console.WriteLine("**** Sync  Test result: {0} = {1} ****", totalLoops, wtSync.result);
		Console.WriteLine("**** Async Test result: {0} = {1} ****", totalLoops, wtAsync.result);
		//
		Console.Write("Hit <enter> to exit this test client\n");
		Console.ReadLine();
	}
}

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 Author

Roman Kiss
Software Developer (Senior)
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 11 Dec 2001
Article Copyright 2001 by Roman Kiss
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid