WebReference.com - Part 1 of Chapter 6: Professional C# Web Services, from Wrox Press Ltd (2/8)

To page 1current pageTo page 3To page 4To page 5To page 6To page 7To page 8
[previous] [next]

Professional C# Web Services

.NET Remoting Fundamentals

The methods that will be called from the client are implemented in a remote object class. In the figure below we can see an instance of this class as the Remote Object. Because this remote object runs inside a process that is different from the client process – usually also on a different system – the client can't call it directly. Instead the client uses a proxy. For the client, the proxy looks like the real object with the same public methods. When the methods of the proxy are called, messages will be created. These are serialized using a formatter class, and are sent into a client channel. The client channel communicates with the server part of the channel to transfer the message across the network. The server channel uses a formatter to deserialize the message, so that the methods can be dispatched to the remote object:

.NET Remoting client/server architecture diagram

In the simplest case, we have to create a remote object class and instantiate a channel for a .NET Remoting application. The formatter and the proxy will be supplied automatically. The architecture is very flexible in that different formatters and channels can be used. We cover the use of the TCP and HTTP channels, and the SOAP and binary formatters in the next chapter.

In the next section we'll start with the simplest case to develop a remoting object, server, and client. In this section we will not go into the details of the remoting architecture, as we will cover this later after we've finished a simple client and server.

Remote Object

A remote object is implemented in a class that derives from System.MarshalByRefObject. MarshalByRefObject defines methods for lifetime services that will be described later when we use the leasing features. A remote object is confined to the application domain where it is created. As we already know, a client doesn't call the methods directly; instead a proxy object is used to invoke methods on the remote object. Every public method that we define in the remote object class is available to be called from clients.

What is an application domain? Before .NET, processes were used as a security boundary so that one process couldn't crash another process because it used private virtual memory. With the help of the managed environment of .NET, the code of an application can be checked, and there's no way to crash the process. To reduce the overhead with different processes, the concept of an application domain was introduced with .NET. Multiple applications can run in the same process without influencing each other if they are called within different application domains. If one of these applications throws an exception that isn't handled, only the application domain is terminated, and not the complete process. To invoke a method in an object running in a different application domain, .NET remoting must be used.

The following code sample shows a simple remote object class, and the code for this simple example can be found in the SimpleTest folder of the code download for this chapter, which is available from http://www.wrox.com. The method Hello() is declared public to make it available for a remoting client:

It is useful to implement the class of the remote object in a different assembly from that of the remote server itself. This assembly can then be used in different server applications, and the client application can use it to get the metadata needed to build the proxy.
// MyRemoteObject.cs
using System;
namespace Wrox.Samples
{
   public class MyRemoteObject : System.MarshalByRefObject
   {
      public MyRemoteObject() 
      {
         Console.WriteLine("Constructor called");
      }
      public string Hello()
      {
         Console.WriteLine("Hello called");
         return "Hello, .NET Client!";
      }
   }
}

We build the assembly MyRemoteObject from the class MyRemoteObject as follows:

csc /t:library /out:MyRemoteObject.dll MyRemoteObject.cs

To page 1current pageTo page 3To page 4To page 5To page 6To page 7To page 8
[previous] [next]

Created: February 13, 2002
Revised: February 13, 2002


URL: http://webreference.com/programming/csharp/webservices/chap6/1/2.html