|
A Simple Guide to .Net Remoting
|
|
|
|
|
Sunday, 08 March 2009
|
|
HTML clipboard
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:
<wellknown type="myNamespace.myObj,
myObj"
url="http:
</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.
try
{
RemotingConfiguration.Configure("client.config");
}
catch{}
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;
}
|
|
Last Updated ( Sunday, 08 March 2009 )
|