Remoting in C#
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   UnCategorized
Current Lesson:
Remoting in C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  GDI+ Drawing outside the lines [next Lesson]  XML transformation using Xslt in C#
Remoting in C#
  by: farazfastian

Remoting in C#

by: farazfastian

Remoting is a framework built into Common Language Runtime (CLR) in order to provide developers classes to build distributed applications and wide range of network services. Remoting provides various features such as Object Passing, Proxy Objects, Activation, Stateless and Stateful Object, Lease Based LifeTime and Hosting of Objects in IIS. I’m not going into detail of these features because it will take 3 to 4 tutorials. Here I’m presenting a simple client/server based application in order to provide you easy and fast hands on Remoting.

Remoting Object

This is the object to be remotely access by network applications. The object to be accessed remotely must be derived by MarshalByRefObject and all the objects passed by value must be serializable.
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingSamples
{
    public class RemoteObject : MarshalByRefObject
    {
        //////////////////////////////////////////////////////////////////////////////
        ///constructor
        public RemoteObject()
        {
            Console.writeline("Remote object activated");
        }
    
        //////////////////////////////////////////////////////////////////////////////
        ///return message reply
        public String ReplyMessage(String msg)
        {
            Console.WriteLine("Client : "+msg);//print given message on console 
            return "Server : Yeah! I'm here";
        }
    }
}

The remote object must be compiled as follows to generate remoteobject.dll which is used to generate server and client executable.
csc /t:library /debug /r:System.Runtime.Remoting.dll remoteobject.cs

Server


This is the server application used to register remote object to be access by client application. First, of all choose channel to use and register it, supported channels are HTTP, TCP and SMTP. I have used here TCP. Than register the remote object specifying its type.
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace RemotingSamples
{
    public class Server
    {
        /////////////////////////////////////////////////////////////////////////////
        ///constructor
        public Server()
        {
        }
        /////////////////////////////////////////////////////////////////////////////
        ///main method
        public static int Main(string [] args)
        {
            //select channel to communicate
            TcpChannel chan = new TcpChannel(8085);
            ChannelServices.RegisterChannel(chan);    //register channel
            //register remote object
            RemotingConfiguration.RegisterWellKnownServiceType(
                Type.GetType("RemotingSamples.RemoteObject,object"),
                "RemotingServer",
                WellKnownObjectMode.SingleCall);
            //inform console
            Console.WriteLine("Server Activated");

            return 0;
        }
    }
}


The server must be compiled as follows to produce server.exe.
csc /debug /r:remoteobject.dll /r:System.Runtime.Remoting.dll server.cs

Client


This is the client application and it will call remote object method. First, of all client must select the channel on which the remote object is available, activate the remote object and than call proxy’s object method return by remote object activation.
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemotingSamples;

namespace RemotingSamples
{
    public class Client
    {
        /////////////////////////////////////////////////////////////////////////////
        ///constructor
        public Client()
        {
        }

        //////////////////////////////////////////////////////////////////////////////
        ///main method
        public static int Main(string [] args)
        {
            //select channel to communicate with server
            TcpChannel chan = new TcpChannel();
            ChannelServices.RegisterChannel(chan);
            RemoteObject remObject = (RemoteObject)Activator.GetObject(
                typeof(RemotingSamples.RemoteObject),
                "tcp://localhost:8085/RemotingServer");
            if (remObject==null)
                Console.WriteLine("cannot locate server");
            else
                remObject.ReplyMessage("You there?");

            return 0;
        }
    }
}

The client must be compiled as follows in order to produce client.exe
csc  /debug  /r:remoteobject.dll  /r:System.Runtime.Remoting.dll  client.cs

About Author:


Faraz Ahmed Siddiqui is a software engineer at KalSoft (Pvt) Limited Karachi Pakistan and worked on Microsoft technologies.

1 


Build Your Own ASP.NET Website Using C# & VB.NET

Chapter:  UnCategorized
Current Lesson:
Remoting in C#
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  GDI+ Drawing outside the lines [next Lesson]  XML transformation using Xslt in C#


Today's Top Movers
vulpes 6800
MadHatter 2220
jal 867
Jeff1203 857
muster 791

Yesterday Top Movers
shakti sin.. 9
MadHatter 3
C#fanatic 2
Al_Pennywo.. 2
carlos_roc.. 1

Monthly Leaders
vulpes 6800
MadHatter 2260
jal 867
Jeff1203 857
muster 791

Top Members
mosessaur 18457
Rincewind 7074
stanleytan 6995
vulpes 6800
Gsuttie 6046

Great Offers
.net hosting
Go To My Pc
Remote Pc Control
zonealarm
spam blocker
web hosting directory
ad server   C#
snadtech GoToMyPc

Top of Page

Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center