Developing distributed applications using. NET Remoting--Configuration files

Source: Internet
Author: User

We already know that server channels and remote clients can be configured in a coded way, and configuration files can be used to configure server channels and remote clients. The advantage of using configuration files for remote client and server objects is that users can configure channels and remote objects without having to modify any line of code or recompile.

. NET provides a Remoting configuration file standards, based on XML format.

A Configuration file

1 . Server configuration file:

Let's take a look at an instance of a server configuration file, and then I'll explain it in detail:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>    <system.runtime.remoting>        <Application>            <Service>                <wellknownMode= "Singleton"type= "Remotingconfigdemo.helloserver, General"Objecturi= "SayHello" />            </Service>            <Channels>                <ChannelPort= "8086"ref= "http"/>            </Channels>        </Application>    </system.runtime.remoting></Configuration>

In the server configuration file, the outermost element is <configuration>, which is the commonality of all configuration files (including the Web. config profile ).

All remote configuration items must be added as child elements to the <system.runtime.remoting> below.

<application> element uses name property specifies the name of the server, which provides the service and requests the channel configuration for the service.

the services provided by the application must act as <service> , which is the remote object itself, can be used <wellknown> element to specify a remote object, Mode property can be specified as SingleCall or Singleton , we'll talk about it later. Using the type property to specify an object that already has a type defined, you only need to specify the name of the Assembly, and the extension DLL is not required .

in the <channels> element, we define the channel to be used by the server, using the ref property can reference a pre-defined channel, and must use the Port property to assign a port to the channel, because the server must have a port number that the client is familiar with so that the client can take advantage of that port number. These channels have been defined in the machine configuration file has a predefined 6 , we can open the machine.config file to see the default path is % Systemroot%\microsoft.net\framework\<vx.x.x>\config.

2 . Client configuration file:

A typical client configuration file is as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>    <system.runtime.remoting>        <Application>            <Client>                <wellknowntype= "Remotingconfigdemo.helloserver, General"URL= "Http://localhost:8086/SayHello" />            </Client>            <Channels>                <Channelref= "http"Port= "0"></Channel>            </Channels>        </Application>    </system.runtime.remoting></Configuration>

The same as the element of the server configuration file, the difference is that this is the client channel, so it does not need to specify the port number, we can temporarily specify as 0 number. The rest remains the same.

Two Sample Program

1 . Remote object code:

usingSystem;usingSystem.Text;usingSystem.Runtime.Remoting.Lifetime;namespaceremotingconfigdemo{ Public classHelloserver:marshalbyrefobject { PublicHelloServer () {Console.WriteLine ("Server Activation ..."); }         Publicstring HelloMethod (string name) {Console.WriteLine ("Server side: {0}", name); return "here is:"+name; }     }}

2 . Server

Configuration file:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>    <system.runtime.remoting>        <Application>            <Service>                <wellknownMode= "Singleton"type= "Remotingconfigdemo.helloserver, General"Objecturi= "SayHello" />            </Service>            <Channels>                <ChannelPort= "8086"ref= "http"/>            </Channels>        </Application>    </system.runtime.remoting></Configuration>

Server code:

usingSystem;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;usingSystem.Runtime.Remoting.Channels.Http;namespaceRemotingconfigdemo { Public classServer { Public Static intMain (string[] args) {RemotingConfiguration.Configure ("Server.exe.config"); System.Console.WriteLine ("Press any key to exit ...");            System.Console.ReadLine (); return 0; }    }}

3 . Client

Configuration file:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>    <system.runtime.remoting>        <Application>            <Client>                <wellknowntype= "Remotingconfigdemo.helloserver, General"URL= "Http://localhost:8086/SayHello" />            </Client>            <Channels>                <Channelref= "http"Port= "0"></Channel>            </Channels>        </Application>    </system.runtime.remoting></Configuration>

Client code:

usingSystem;usingSystem.Runtime.Remoting;usingSystem.Runtime.Remoting.Channels;usingSYSTEM.RUNTIME.REMOTING.CHANNELS.TCP;usingSystem.Runtime.Remoting.Channels.Http;usingSystem.IO;namespaceRemotingconfigdemo { Public classClient { Public Static voidMain (string[] args) {            //using the HTTP channel to get the remote objectRemotingConfiguration.Configure ("Client.exe.config"); HelloServer Obj2=NewHelloServer (); if(Obj2 = =NULL) {System.Console.WriteLine ("connection to HTTP Server failed ..."); } Console.WriteLine ("Client2 HTTP hellomethod {0}", Obj2. HelloMethod ("Caveman2"));        Console.ReadLine (); }    }}

Three Some points to be aware of

1 . The name of the assembly is often confused with the name of the file where the assembly is stored. The name of the assembly is HelloServer, and the assembly file name is HelloServer.dll. When you use a method call, you need to use the name of the assembly as an argument, rather than using the file's extension.

2 . You must copy the assembly of the remote object class to the directory of the executable file of the service program, or by adding a DLL Reference. Because the remote object class in the remote framework is instantiated by reading the configuration file, the assembly must be located where it can be found.

3 . In general, we can make the application's configuration file name and executable file the same file name, followed by the file extension . config.

4 . If you use App. Config as your server or client profile, be aware that the App. Config file automatically changes to [ Application name ].exe.config.

5 . To prevent the configuration file from being found, we can set it in the properties of the project and fill in the Copy directory statement in the post-build event:

Copy"$ (ProjectDir) \*.config" "$ (TargetDir)"



6. In the code, you can not write the configuration file name hard-coded to die, with the following statement instead, this is a good programming practice, but also a recommended way of writing.

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

7 . Last and foremost, it is recommended to use the configuration file in your project!

Source: http://www.cnblogs.com/Terrylee/archive/2005/11/17/278366.html

Developing distributed applications using. NET Remoting--Configuration files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.