More Articles
How to create a remote object through remoting without having to ship your remote object source.
Introduction
So lets say your new to remoting and all you want to do is get an remote your way to an object without sending your implementation source code to all the clients.
No problem, all you need to do is have a dll that contains the definition for your remoteable object that you include in both the client and server.
Here I have created a simple remoteable object with a share dll. For the purposes of this article we will create a simple Server Activated Object. For more information
on remoting object types click here.
For this example I am going to create a very simple remote object. To start with we first need to create a dll we will call 'sharedll.dll'. Also create two console
applications called Client and Server. Add these all to the same solution so we can compile and examine the files easily.
After we are done our relevant class and exe breakdown will be as follows.

Step by Step
Now that I've explained the basics, lets get down to the code.
Step 1: Setting up definitions
Create a solution and add a class library (I called mine ShareDLL), a console app called Client and a console app called Server. Build the solution, then go to
the ref section of Client and Server and add a reference to the ShareDLL to both. Also go into the class files for the client and server and add the using ShareDLL
(assuming you used this name for your namespace on the ShareDLL). This will allow both the Client and Server to see the class definitions we will store in the ShareDLL.
Add an abstract class "RemoteObjectDef" to the class library and inherit it from MarshalByRefObject. This class will serve as the basis for the common type that
both the client and server see. The class is marked abstract because we will only ever use them as a definition that points to the actual implementation which will
be in the server.
Let's also add a function that returns a string to test that we have correctly instantiated our remote object.
Your sharedll.dll project should contain only the follow code.
// Contents of sharedll.dll
using System;
namespace ShareDLL
{
public abstract class RemoteObjectDef : MarshalByRefObject
{
public abstract string Test();
}
}
Step 2: Fill in functionality
Now that we have our class defined, lets create the server object and fill in the functionality. Add a class to the server that inherits from your RemoteObjectRef
class.
using System;
using ShareDLL;
namespace Server
{
public class RemoteObject : ShareDLL.RemoteObjectDef
{
public override string Test()
{
return "Hello World!";
}
}
}
Step 3: Serve your object
Now our object is ready to be used so lets serve it up from the server. First we create a channel (I chose port 8675). After that you need to register the object
with the remoting configuration. This will start the object listening.
Go to the run area of your server class1.cs and add the following code.
[Code Based Option] -------------------------------------------
// server.cs in project server
ChannelServices.RegisterChannel( new TcpChannel(8675) );
Type RemoteType = Type.GetType("Server.RemoteObject,Server");
RemotingConfiguration.RegisterWellKnownServiceType( RemoteType, "RemoteObjectURI",
WellKnownObjectMode.SingleCall );
[Config File Option] -------------------------------------------
// Server.exe.config
<configuration>
<system.runtime.remoting>
<application>
<channels>
<channel ref="tcp" port="8675" />
</channels>
<service>
<wellknown mode="SingleCall" type="Server.RemoteObject,Server"
objectUri="RemoteObjectURI" />
</service>
</application>
</system.runtime.remoting>
</configuration>
// server.cs in project server
RemotingConfiguration.Configure( @"..\..\Server.exe.config" ); // change this when
you go into production
Step 4: Instantiate your objects
Now that we have the object available lets see about instancing it on the client.
// client.cs in project client
Console.WriteLine("Creating Remote Object.");
string url = "tcp://localhost:8675/RemoteObjectURI";
RemoteObjectDef ro = (ShareDLL.RemoteObjectDef)Activator.GetObject(
typeof(ShareDLL.RemoteObjectDef), url );
Console.WriteLine("Remote Object says " + ro.Test() );
And that is all you need. You now have a working remote object that you didn't have to ship the implementation code for. All your private processing routines are
safe.
| Version |
Date |
Change |
Download |
Buy |
| 1.0 |
12/10/2003 |
Initial version. |
Download 1.0 |
|