| home / programming / csharp / webservices / chap6 / 2 | [previous] [next] |
|
Instead of using the new operator, we can create client-activated objects with the
Activator class. Well-known objects have to be created with the GetObject()
method; client-activated objects require the method CreateInstance() because here the
remote object really is instantiated on request of the client. With the method
CreateInstance() it is also possible to invoke non-default constructors.
The following code example shows a client without a configuration file. The channel is created
and registered as before. With Activator.CreateInstance() we instantiate a client-activated
remote object. This method accepts activation attributes. One of these attributes must be the URL to the
remote server. The class System.Runtime.Remoting.Activation.UrlAttribute can be used to define
the URL of the remote object that is passed to the CreateInstance() method.
We can also use overloaded versions of
CreateInstance()that don't accept activation arguments if we use a configuration file or the utility classRemotingConfigurationto define the URL to the remote object.
The second argument of CreateInstance allows passing arguments to the constructor. To
allow a flexible number of arguments, this parameter is of type object[] where we can pass
any data type. In the MyRemoteObject class we now have a constructor that accepts a single
int value, so we create an object array with one element, and assign an int
value to that element of the array. The attrs array that is also an object array also
has only one element. This is a URLAttribute where we pass the URL to the remote object
to the constructor of this class. Both arrays are passed into the CreateInstance() method
together with the type of the remote object to create the remote object in the right place using the
appropriate constructor:
// SimpleClient.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Activation;
namespace Wrox.Samples
{
class SimpleClient
{
static void Main(string[] args)
{
TcpClientChannel channel = new TcpClientChannel();
ChannelServices.RegisterChannel(channel);
object[] constr = new object[1];
constr[0] = 333;
object[] attrs = {
new UrlAttribute("tcp://localhost:9000/SimpleServer") };
MyRemoteObject obj = (MyRemoteObject)Activator.CreateInstance(
typeof(MyRemoteObject), constr, attrs);
int x = obj.State;
Console.WriteLine(x);
}
}
}
A well-known single-call object can be garbage-collected after the method call of the client because it
doesn't hold state. This is different for long-lived objects, which means both client-activated and
well-known singleton objects. If the remote object is deactivated before the client stops using it, the
client will get a RemotingException that the object has been disconnected with the next
method call that is made from the client. The object has to be activated as long as the client needs it.
What if the client crashes and if it cannot inform the server that the object is not needed any more?
Microsoft's DCOM technology used a ping mechanism where the client regularly pings the server to inform
it that the client is still alive and which objects it needs, but this is not scalable to internet
solutions. Imagine thousands or millions of continuous ping requests going through the server. The
network and the server would be loaded unnecessarily. .NET Remoting uses a lease-based lifetime
mechanism instead. Comparing this mechanism to cars, if we lease a car we do not hear anything
from the leasing company for months after the lease was instantiated. The object also remains
activated until the lease time runs out with the .NET Remoting leasing facility.
If the exception
RemotingException: Object <URI> has been disconnected or does not exist at the serveroccurs, this may be because of an expired lease time.
There are some ways in which we can influence leasing times of objects. One way is to use configuration files, and another way is to do it programmatically for the server, the remote object, or in the client. Let us look at what can be configured first.
The remote object has a maximum time to lease which is defined with the LeaseTime option.
If the client does not need the remote object for this time period, the object will be deactivated.
Every time the client invokes a method with the remote object the leasing time is incremented by a
value that is defined with RenewOnCallTime.
| 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/5.html