- Part 2
- Topic 2
Create a client application to access a remote object.
- Create a remote object.
- Configure a client application programmatically.
- Configure a client application manually by using configuration files.
- Access the remoting service by calling a remote method.
- Call a remote method asynchronously in a client application.
Before we get much further, now is a good time to explain the types of activation types and lifetimes are available through remoting. There are two types of activations: server activation and client activation. Server activated objects exist only when needed and are easier to scale because they only exist when they are needed. There are two types of server side activation singleton and single call. Single call objects are created only when a method on the clients proxy object is invoked. Singleton objects only allow for one instance to serve all clients. Because an instance exists for Singleton objects, a client may specify a lifetime lease
Client activation involves keeping an instance of the object on the server that is invoked through a proxy object created on the client. Because the Server keeps the instance in memory, this option doesn’t scale as well as server activated objects, however the object will be able to maintain state between requests. Because the object exists on the server, the client must specify a lifetime lease so that the server will know when it is ok to dispose of the object.
Now back to the section. To create a remote object on the client side you must register the application as a client for the remote object and then invoke it as if it were just another object in that applications app domain.
Like the Server environment, you can register the client either programmatically or through configuration.
To register the remoting type and channel programmatically, you must register a channel and then the type.The following sample code from msdn for registering a type and a channel from msdn is below:RemotingConfiguration.RegisterWellKnownClientType(typeof(HelloService),
"tcp://localhost:8082/HelloServiceApplication/MyUri"); HelloService service = new HelloService();
To register the remoting type and channel through configuration simply call the RemotingConfiguration.Configure method passing in the name/path to the configuration file. A sample configuration file from msdn is below:
Once you have the instance of the remote object, you can just invoke a method as if it were a method of a local object. Depending on the activation type, the server will either create the object on the fly to invoke the method, call a singleton or continue using a client object.
The remoting client can invoke the object synchronously or through using async call backs. The following example of using an AsyncCallBack with remoting is from msdn:
// This is the only thing you must do in a remoting scenario
// for either synchronous or asynchronous programming
RemotingConfiguration.Configure("SyncAsync.exe.config"); // The remaining steps are identical to single-
// AppDomain programming.
ServiceClass obj = new ServiceClass(); // This delegate is a remote synchronous delegate.
RemoteSyncDelegate Remotesyncdel = new RemoteSyncDelegate(obj.VoidCall); // When invoked, program execution waits until the method returns.
// This delegate could be passed to another application domain
// to be used as a callback to the obj.VoidCall method.
Console.WriteLine(Remotesyncdel()); // This delegate is an asynchronous delegate. Two delegates must
// be created. The first is the system-defined AsyncCallback
// delegate, which references the method that the remote type calls
// back when the remote method is done.
AsyncCallback RemoteCallback = new AsyncCallback(this.OurRemoteAsyncCallBack); // Create the delegate to the remote method you want to use
RemoteAsyncDelegate RemoteDel = new RemoteAsyncDelegate(obj.TimeConsumingRemoteCall); // Start the method call. Note that execution on this
// thread continues immediately without waiting for the return of
// the method call.
IAsyncResult RemAr = RemoteDel.BeginInvoke(RemoteCallback, null); // If you want to stop execution on this thread to
// wait for the return from this specific call, retrieve the
// IAsyncResult returned from the BeginIvoke call, obtain its
// WaitHandle, and pause the thread, such as the next line:
// To wait in general, if, for example, many asynchronous calls
// have been made and you want notification of any of them, or,
// like this example, because the application domain can be
// recycled before the callback can print the result to the
// console. //e.WaitOne(); // This simulates some other work going on in this thread while the
// async call has not returned.
int count = 0;
Console.Write("\rNot completed: " + (++count).ToString());
// Make sure the callback thread can invoke callback.
Other Resources & Links:
Building a Client Application
.Net Remoting - Part2 Object Activation, Lifetime And Configuration
Remoting Example: Asynchronous Remoting