.NET Remoting and Delphi - The ChannelServices Class
(Page 5 of 11 )
The ChannelServices class provides static class methods to register remoting channels, and it's used in both clients and servers.
The following snippet of code shows how to create and register an HTTP communication channel listening on port 9088 for use on the server side:
var Channel : HttpChannel;
begin
Channel := HttpChannel.Create(9088);
ChannelServices.RegisterChannel(Channel);
Clients do not specify a port number when registering channels. The URI information necessary to access remote objects is specified in the call to the Activator object at a later time, and this will also include a port number.
The following snippet of code shows how to create a client channel and acquire a reference to the remote service from the client application:
type
TCalculator = class(MarshalByRefObject, ICalculator)
private
protected
// ICalculator
function Sum(A,B : integer): integer;
public
end;
Remotable Objects
Objects intended to be accessed by different domains are called remotable objects. Two types of remotable objects exist in .NET: Marshal-By-Reference, and Marshal-By-Value.
Note - A third type, Context-bound is also available, but it won't be covered in this book because of the length and complexity of the topic. You can think of a .NET context as a subdivision of an application domain in which context-bound objects live. For more information about Contexts, refer to the .NET Framework SDK Documentation at "ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/
cpconremotableobjects.htm".
Marshal-By-Reference Objects
Simply put, instances of this type can be seen as a collection of methods that can be invoked from remote clients.
Marshal-By-Reference objects are created on the server where they live for the duration of a method call (see "Single-call Instantiation" later) or are shared among different clients (see "Singleton Activation" later) for the duration of a lease (see "Leases and Sponsors" later).
In order to create a Marshal-By-Reference object, you need to make its class descend from MarshalByRefObject, which is defined in the System namespace.
The following is an example of a Marshal-By-Reference class:
type
TCalculator = class(MarshalByRefObject, ICalculator)
private
protected
// ICalculator
function Sum(A,B : integer): integer;
public
end;
The methods of this type of objects can have any kind of simple data type (that is, integers, strings, and so on) or object parameters(as long as they are Marshal-By-Value objects).
Marshal-By-Value Objects
Instances of this type cross application domain boundaries after being serialized in a transportable format.
When they reach the target domain, they are deserialized and a new instance of the original class is created in the target application domain.
In order to create a serializable object, you need to mark its class with the [Serializable] attribute like this:
{ TAccount }
[Serializable]
TAccount = class
private
fNumber : integer;
fBalance: double;
fName: string;
public
constructor Create(const aNumber : integer;
const aName : string;
const aBalance : double);
property Number : integer read fNumber;
property Name : string read fName;
property Balance : double read fBalance write fBalance;
end;
The purpose of these types of objects is generally to share data between applications in a structured manner. They are usually used as input or output parameters of remote methods.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Object Activation >>
More .NET Articles
More By Xavier Pacheco