Click here to Skip to main content
15,898,036 members
Articles / Programming Languages / C#

Using Web Services for Remoting over the Internet.

Rate me:
Please Sign up or sign in to vote.
4.76/5 (38 votes)
15 Feb 2002CPOL8 min read 372.3K   4.2K   222  
This article describes a design and implementation (C#) of the Remoting over Internet using the Web Service as a gateway into the Remoting infrastructure.
using System;
using System.Threading;
using System.Diagnostics;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using MyRemoteObject;

namespace ConsoleClient
{
	class Client
	{
		static void Main(string[] args)
		{	
			int ii = 0;

			try 
			{
				RemotingConfiguration.Configure(@"..\..\ConsoleClient.exe.config");
				
				// activate a remote object
				Type objectType = typeof(MyRemoteObject.RemoteObject);
				//string objectUrl = @"ws://localhost/WebServiceListener/Listener.asmx; tcp://localhost:8090/endpoint"; 
				string objectUrl = @"ws://localhost/WebServiceListener/Listener.asmx; msmq://./reqchannel/endpoint"; 
				RemoteObject ro = (RemoteObject)Activator.GetObject(objectType, objectUrl);
				
				Console.Write("Type your name: ");
				string name = Console.ReadLine();
				string[] firstlastName = name.Split(new char[]{' '});
				// Call Context
				User user = new User();
				user.firstname = firstlastName.Length > 0 ? firstlastName[0] : "";
				user.lastname = firstlastName.Length == 2 ? firstlastName[1] : "";
				CallContext.SetData("User", user); 
				
				while(true) 
				{
					Console.Write("Hit any key to make a test.");
					string reqmsg = Console.ReadLine();
					Console.WriteLine(string.Format("State: Id={0}; Name={1}\n", ro.Id, ro.Name));
					string respond = null;
					object str = null;
					object retval = ro.SayHello(reqmsg, out respond, ii++, out str);
					Console.WriteLine(string.Format("\nReturned Values: {0} {1}", respond, str));
					Console.WriteLine(retval);
					User u = (User)CallContext.GetData("User"); 
					Console.WriteLine("CallContext: {0}, {1}, {2}", u.GetType(), u.firstname, u.lastname);
				}
			}  
			catch(Exception ex) 
			{
				Console.WriteLine(ex.Message);
			}

			System.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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions