A Simple Guide to .Net Remoting
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:
A Simple Guide to .Net Remoting
[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]  null keyword [next Lesson]  unchecked keyword
A Simple Guide to .Net Remoting
  by: jamiemoffat

A Simple Guide to .Net Remoting

By jamiemoffat

Remoting is .Net’s way to create and uses object instances on a remote machine, without any of the hassle of DCOM.

This guide provides a (very) quick guide to getting you up and running and calling across your network. Most of the examples that Microsoft provide use a console application to host your remote object, which wouldn’t be the way most of us will use the technology.

This tutorial will cover the most common hosting method, which is within IIS.

I haven’t gone too deep into all the settings that are available with .Net Remoting, I will cover those in a future article.

1) Create a C# class that inherits MarshalByRefObject: E.g.
public class myObj: MarshalByRefObject
{
     // ...
}




Add some classes, properties etc.

2) Compile it to produce myObj.dll

3) Now, the object needs to be hosted somewhere. The easiest way to do this is to host the object within IIS.

First of all though, we need to describe our object so it can be accessed. To this end, create a web.config file with the following text:

E.g.
 <configuration> 
  <system.runtime.remoting>
    <application>
      <service>
        <wellknown mode="SingleCall" type="myNamespace.myObj, myObj" 
                        objectUri="myObj.soap" />
      </service>
    </application>
  </system.runtime.remoting>
</configuration>

In this XML:


type ="myNamespace.myObj, myObj" is the full type name, followed by the assembly name.

On the Remote Server:

1) Create a directory called “myRemoteObject”. Create a Bin directory beneath it.

2) Create a new IIS virtual Directory called myIISObj that points to the myRemoteObject directory.

3) Place your compiled versions of myObj.dll in the myRemoteObject \Bin directory

4) Place the Web.Config file in the myRemoteObject directory.

On the Client:

In your application set a reference to the local object on your client machine. This allows he CLR to pick up the meta data of the object so that we can create the object remotely (There is another way to generate the meta data without needing the actual object on the client, a utility called SoapSuds.exe, but I leave that for you to read about )

Now we need a client.config file analogous to the web.config file, which tells the CLR how to access (ie. where to access) the remote object.

<configuration>
  <system.runtime.remoting>
    <application name="Client">
      <client url="http://andy2k/myIISObj">
        <wellknown type="myNamespace.myObj, myObj"         
                    url="http://andy2k/myIISObj/myObj.soap"/>
      </client>
      <channels>
        <channel ref="http" />
      </channels>
    </application>
  </system.runtime.remoting>
</configuration>




(Note: replace “andy2k” with the name of your remote server)

Now in the client code, simply create the object as usual but, first of all, load the config file.

//Load the Http Channel from the config file
try
{
    RemotingConfiguration.Configure("client.config");
}
catch{}

//instantiate the remote object on the server
//(this is really just a proxy object here)
myNamespace.myObj anObj = new myNamespace.myObj();

The .Net runtime picks up the fact that this is not a local object (because of the config file read in the previous line) and creates the object on the remote machine.  You now access the object as if it was created locally, and .Net does the rest.


(if you’re not convinced, create a method in the class that simply returns a string representing the name of the local machine

Something like:

Public string getCompName()
{
    return System.Environment.MachineName;
}

that should convince you that the object has been created on the remote machine) 


And there you have it – you’ve just created a remote object!

1 


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

Chapter:  UnCategorized
Current Lesson:
A Simple Guide to .Net Remoting
[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]  null keyword [next Lesson]  unchecked keyword


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
garyweik1 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