18.2 Creating Host Applications for Remotable Types

 <  Day Day Up  >  

You want to host the remotable object in a custom application to make it available to client applications.


Technique

To offer an object by .NET Remoting, you have to create a network channel and register the object with the remoting runtime. When you register the channel, a port is opened, and the server listens for requests .

You create a network channel by instantiating an object of a channel class, such as TcpChannel , as in the code here. With the server, you also have to specify a port number where the server is listening. Next, you must register the channel with the .NET Remoting runtime using ChannelServices.RegisterChannel :

 
 TcpChannel channel = new TcpChannel(8088); ChannelServices.RegisterChannel(channel); 

After the channel is registered, you can register the remote object class with the runtime. You do so by using the RemotingConfiguration class. The method RegisterWellKnownServiceType registers the object as a well-known object, which means that in cooperation with the parameter WellKnownObjectMode.SingleCall , the object will be created anew with every method call. This way is a scalable way of dealing with resources on the server because no server resources are kept with the server after method calls. However, with .NET Remoting, you can also create singletons and stateful objects. Client-activated stateful objects are covered in Recipe 18.4; singletons are covered in Recipe 18.5, "Creating Server-Activated Singleton Objects."

 
 RemotingConfiguration.RegisterWellKnownServiceType(                           typeof(_1_RemoteObject.RemoteObject),                           "Demo", WellKnownObjectMode.SingleCall); 

After registering the object, you just have to ensure that the server doesn't get stopped ; the server keeps running to listen for requests.

Comments

Remote objects can be hosted in any application type. You can create a Windows Forms application where multiple users can use peer-to-peer communication. You can create a Windows service that hosts remoting objects. .NET enterprise services have a built-in functionality for .NET Remoting, and it is also possible to host remoting objects within Internet Information Server (IIS). All you have to do is create a channel and register the remote object.

With the .NET Framework 1.1, you get channels for two different protocols: TCP and HTTP. The HttpChannel class communicates using the HTTP protocol, and the TcpChannel class uses a faster transport mechanism by using the TCP protocol directly.

With both the HTTP and TCP channels, you get server-side and client-side channels. TcpServerChannel and HttpServerChannel are the classes that offer server-side functionality, and TcpClientChannel and HttpClientChannel offer client-side functionality.

The classes TcpChannel and HttpChannel have the functionality of both a client and a server channel. You need them on the client and on the server if you do callbacks from the server to the client.

With all the channel classes, you have different constructors to instantiate the channels. You can set a port number, which is a requirement with the server, but you can also specify a priority that is used if multiple channels are offered . You can specify a sink provider that should be used with the channel. Sink providers are discussed later in this chapter.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net