< Day Day Up > |
TechniqueInstead of creating channels and registering the remote object in the code, you can replace the entire channel and object registration with a single line of code. The method RemotingConfiguration.Configure reads a configuration file and creates and registers all channels and objects accordingly : RemotingConfiguration.Configure("6_Server.exe.config"); To define the configuration of a well-known object with the HTTP channel, the configuration file for the server is shown in Listing 18.1. The <wellknown> element specifies a well-known object. You can specify the SingleCall or Singleton object type with the mode attribute. The type attribute defines the type and the assembly of the remote object. objectUri specifies the name of the object that the client will use. With the <channels> element, the remoting channels are specified. Listing 18.1 A Server Configuration File to Define a Well-Known Object with the HTTP Channel<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.runtime.remoting> <application> <service> <wellknown mode="SingleCall" type="_1_RemotingObject.RemoteObject, 1_RemoteObject" objectUri="Demo" /> </service> <channels> <channel port="8088" ref="http" /> </channels> </application> </system.runtime.remoting> </configuration> Listing 18.2 shows the configuration file for the client. With the client, you need only the url and the type attributes with the <wellknown> element. With the client configuration file, you must specify the protocol, server name, port number, and object name with the url . The <channel> configuration of the server defined the port number of the server because the client application must know this port number. With the client application, there is no need to define a fixed port number. If you set the port number to , any free port number is selected automatically. Listing 18.2 A Client Configuration File to Define a Well-Known Object with the HTTP Channel<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.runtime.remoting> <application> <client> <wellknown url="http://localhost:8088/Demo" type="_1_RemoteObject.RemoteObject, 1_RemoteObject" /> </client> <channels> <channel port="0" ref="http" /> </channels> </application> </system.runtime.remoting> </configuration> To define client-activated objects, you can specify the <activated> element in the configuration file. CommentsUsing configuration files for .NET Remoting makes it easy to change the configuration without the need to change the code. |
< Day Day Up > |