Writing distributed applications well is one of those things that rests at the upper echelon of advanced topics. This chapter will get you started. However, if you are going to deploy a distributed application that employs remoting, you should explore in detail such other subjects as management of a remoted object's lifetime, asynchronous behavior and remoting, security issues related to remoting, and implementation of Remote behavior for a variety of host applications. This material will ultimately reside in several books. Ingo Rammer [2002] wrote one of the more comprehensive books currently available on remoting. I have included below a quick overview of these subjects to help guide further exploration.
Managing a Remoted Object's Lifetime
DCOM managed object lifetime by pinging the client to see if the client was still hanging out. This causes a lot of extra network traffic. .NET Remoting uses an approach similar to Java by supplying an object a lease. The basic idea is that an object has a default amount of five minutes of time to live (TTL). After five minutes the object is destroyed. If a remote object is accessed, the TTL is reset to the lease time (in the case of the default setting, five minutes). As long as the object is accessed within the TTL, it stays alive.
You can adjust the lifetime by adding a <lifetime> element to the .config file or implementing a sponsor. The sponsor answers the question, “My lease time is up; should I go away?” Whereas the lifetime is a static value, the sponsor can be dynamic, based on some programmatic logic. You implement a sponsor as a remotable object by implementing the System.Runtime.Remoting.Lifetime.ISponsor interface, by programmatically returning an ILease object from an overridden InitializeLifetimeService method, or by codifying the lifetime in a .config file. Listing 8.15 offers an example lease.
Listing 8.15 Changing the Default Lifetime by Using the Configuration File
<lifetime leaseTime="2M" sponsorshipTimeout="2M" renewOnCallTime="2M" leaseManagerPollTime="5S" />
This example sets the lease time, sponsor time-out, and renew time to two minutes. The polling interval is set to five seconds. (The units of measure are D for days, H for hours, M for minutes, S for seconds, and MS for milliseconds.) You cannot combine intervals. For example, you cannot define a lease time of "2M5S".
In Listing 8.15, leaseTime represents the object's lease on life; sponsorshipTimeout represents the time-out period of a sponsor; and renewOnCallTime represents the extended amount of lease time on an object (this value is not accumulative). Finally, leaseManagerPollTime specifies how long the lease manager waits between polling intervals.
Asynchronous Remoting
.NET Remoting supports both synchronous and asynchronous method invocation. Use the BeginInvoke and EndInvoke methods combined with delegates to invoke remote operations asynchronously. Read Chapter 6 for more information on asynchronous processing.
Remoting Security Issues
You can employ authentication and encryption with .NET Remoting and use secure HTTP (HTTPS) to make distributed applications more secure. For secure HTTP you need a certificate. You can acquire a certificate from VeriSign (http://www.netsol.com) for free. For more information on remoting and security, read information on HTTPS, certificates, encryption, and authentication.
Choosing a Host for Your Server
The examples in this chapter host all the server applications as console applications, but you are not limited to console applications. You can also use .NET Remoting in Windows Forms, Web (hosting the remote server in IIS), and Service (Windows NT Service) applications. To create a remote server using any of these host types, use the project template to create the project for the particular type. Each style of host will have individuated requirements.