| home / programming / csharp / webservices / chap6 / 2 | [previous] [next] |
|
|
Nothing has really changed with the server and the remote object, so we can use the same client application we created earlier to communicate with the new server. To complete the picture, we will change the client code as can be seen below:
// SimpleClient.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Wrox.Samples
{
class SimpleClient
{
static void Main(string[] args)
{
// Create and register the client channel
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel);
// Register the name and port of the remote object
WellKnownClientTypeEntry entry = new WellKnownClientTypeEntry(
typeof(MyRemoteObject),
"tcp://localhost:9000/SimpleServer/MyRemoteObject");
RemotingConfiguration.RegisterWellKnownClientType(entry);
Now a client channel from the class TcpClientChannel is instantiated and registered.
We use the WellKnownClientTypeEntry class to define the remote object. In contrast to
the server version of this class, the complete path to the remote object must be specified here as
we have to know the server name in the client, but the mode is not specified because this is defined
from the server. The RemotingConfiguration class is then used to register this remote
object in the remoting runtime.
As we saw earlier, the remote object can be instantiated with the new operator. To demonstrate
that a well-known object gets activated with every method call, the method Hello() is now
called five times in a for loop:
MyRemoteObject obj = new MyRemoteObject();
for (int i=0; i < 5; i++)
Console.WriteLine(obj.Hello());
}
}
}
Running this program we can see in the output of the server (see screenshot below) that a new instance
of the remote object is not created by calling the new operator in the client code, but with
every method call instead. Well-known objects could also be called server-activated objects as
compared to client-activated objects. With client-activated objects a new object gets instantiated
on the server when the client invokes the new operator:

One important fact that we should remember with well-known objects:
A well-known object doesn't have state. A new instance is created with every method call.
| home / programming / csharp / webservices / chap6 / 2 | [previous] [next] |
Created: February 25, 2002
Revised: February 25, 2002
URL: http://webreference.com/programming/csharp/webservices/chap6/2/2.html