.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Remoting >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Remoting In .NET

Posted By:Pankaj Mishra       Posted Date: August 09, 2008    Points: 25    Category: Remoting    URL: http://www.dotnetspark.com  
 

What is Remoting?
In a simple sentence we can define remoting a process of program or component interacting across certain boundrais..Net extends this concept to include to ability to define additional context within the running application.

Your First Remoting Application
This sample remoting application exposes a server side MarshalByRefObject in a singalton mode. We can call this object CustomerManagerAn it will provide a method to load a custom object
 

Defining the Remote Interface
First we have to define the interface ICustomerManager which will implemented by the serverin this we will define a single method GetCustomer() which will return a customer object to the caller function.

Public interface ICustomer
{
Customer GetCustomer(int liCustID);//this will load the customer object by it given liCustID
}

Defining the Data Object
Public class Customer
{
public string FirstName;
public string LastName;
public DateTime DOB;
public Customer()
{
Console.WriteLine(“Customer.constructor: object Created”);
}
public int GetAge()
{
TimeSpan loTimeSpan = DateTime.Today.Subtract(DOB);
Return loTimeSpan.Days /3665;
}
}


Now we will implement the Server
On server we need to implement the IcustomerManager this will allow to load a Customer

Using System.Runtime.Remoting;
Using Customer; //Namespace os client object
Using System.Runtime.Remoting.Channels;
Using System.Runtime.Remoting.Http;
Public class CustomerManager : MarshalByRefObject, IcustomerManager
{
public CustomerManager()
{
Console.WriteLine(“Customer Manager .constructor: object created”);
Public customer GetCustomer(int liCustID)
{
Customer loCustomer = new Customer();
loCustomer .FirstName = “Pankaj”;
loCustomer .LastName =”Mishra”;
loCustomer .DOB = new DateTime(1980,2,3);
return loCustomer ;
}
}
}
 

Server SatrtUp

Now we have to implementing the server startup code this will run our server program and whenever the request will come with http cnannel than it swill provide the result to the caller

Class ServerSatrtup
{
static void Main(string [] args)
{
HttpChannel lo HttpChannel = new HttpChannel(1111);
ChannelServices.RegisterChannel(HttpChannel);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(CustomerManager),” CustomerManager.soap”,WellKnownObjectMode.Singaelton);
Console.WriteLine();//This will keep alive the server until any key is press
}
}

Now Implementing the Client.

Which is caller here for the object Customer which is on some remote system.

Using System.Runtime.Remoting;
Using Customer; //Namespace os client object
Using System.Runtime.Remoting.Channels;
Using System.Runtime.Remoting.Http;
Class Client
{
static void Main (String [] args)
{
HttpChannel lo HttpChannel = new HttpChannel(1111);
ChannelServices.RegisterChannel(HttpChannel);
ICustomerManager loMgr = (ICustomerManager)Activator.GetObject(typeof(ICustomerManager),Http://localhost:1111/IcustomerManager.soap);
}
Customer loCustomer = loMgr.GetCustomer(5000);
Int age = loCustomer.GetAge();
Console.WriteLine(“Client.Main():Customer{0}{1} is {2} years old”)”, loCustomer.FirstName, loCustomer.LastName,age);
Console.ReadLine();
}

Cheers
Pankaj

 

 

 Subscribe to Articles

     

Further Readings:

Responses
Author: Kriso         Company URL: http://www.dotnetspark.com
Posted Date: August 29, 2008

can you please provide a sample application of this in .net 2.0 . please mail the link to borntobfree@gmail.com
Author: Pankaj Mishra         Company URL: http://www.dotnetspark.com
Posted Date: August 29, 2008

Hi Kriso,

Sure i will email you within 2-3 days.

Cheers
Pankaj

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend