Click here to Skip to main content
15,886,861 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 371.5K   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.Diagnostics;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Contexts; 


namespace MyRemoteObject
{
	[Serializable]
	public class User : ILogicalThreadAffinative 
	{
		private string FirstName;
		private string LastName;
		public string lastname{ get{ return LastName; } set{LastName = value;}} 
		public string firstname{ get{ return FirstName; } set{FirstName = value;}} 
	}

	public class RemoteObject : MarshalByRefObject
	{
		private string	_name = "Mike";
		private int		_id = 0;
		public string Name { get{ return _name; } set{ _name = value; }}
		public int Id { get{ return _id; } set{ _id = value; }}

		public RemoteObject()
		{
			Trace.WriteLine("RemoteObject ctor");
		}
		public object SayHello(string reqmsg, out string rspmsg, int i, out object str)
		{
			User user = null;

			try 
			{
				user = (User)CallContext.GetData("User");
				if(user == null) 
				{
					user = new User();
					user.firstname = "Admin";
					user.lastname = "";
				}
					
				if(reqmsg == "throw")
					throw new Exception(string.Format("Hi {0}, this is an Exception test", user.firstname));
			
				rspmsg = string.Format("[#{0}] Hello {1}. You've typed '{2}'.", i, user.firstname, reqmsg);
				str = string.Format("State: {0}, {1}", _name, _id++);

				user.firstname = user.firstname.ToUpper();

				Trace.WriteLine(string.Format("RemObject.SayHello({0}, user is {1} {2})", reqmsg, user.firstname, user.lastname));
			}
			catch(Exception ex)
			{
				Trace.WriteLine(string.Format("RemoteObject.SayHello cautch: {0}",ex.Message));
				throw ex;
			}
			
			return "Done";
		}
		[OneWay]
		public void OneWay(string msg, int ms)
		{
			Thread.Sleep(ms);
			Console.WriteLine("RemoteObject.OneWay({0})", msg);
		}
	}
}

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