Building a basic. NET Remoting Application

Source: Internet
Author: User
Tags exit command line config readline
Program to build a use. NET Remoting framework for applications that communicate between application domains (application domain) is simple. You must implement the remote type (remotable type), the service application domain used to listen for requests, and the client application domain. At the same time, you must configure the remoting system for each application domain (remoting systems) so that remote activation (remote activation) can be used to activate the remoted type.
First, create the remote type (remotable type):
To enable objects in other application domains to use your class instance, your class must derive from the System.MarshalByRefObject class. The following code shows how to create a remote class:
[Visual Basic]
' Remotabletype.vb
Imports System

Public Class RemotableType
Inherits MarshalByRefObject
Private _internalstring as String = "This is the RemotableType."

Public Function Stringmethod () as String
Return _internalstring
End Function ' Stringmethod
End Class ' RemotableType
[C #]
RemotableType.cs
Using System;
public class remotabletype:marshalbyrefobject{
private string _internalstring = "This is the RemotableType.";
public string Stringmethod () {
return _internalstring;
}
}
To compile the class in the previous example into a library file, using the command-line tools that are included with the. NET Framework SDK, Just save it as remotabletype.language-extension (here language-extension is the language you use, such as CS, VB). Compile-time using the following command:
[Visual Basic]
Vbc/t:library Remotabletype.vb

[C #]
Csc/noconfig/t:library RemotableType.cs

Second, create service applications (host application):
To create instances of remote objects in different application domains (application domain), you must create a service application to accomplish two tasks:
(1) Select and register a channel (channel). The channel is an object that handles network protocols and serial format (serialization format).
(2) in the. NET remote systems (. NET Remoting system) register the remote type you created so that the remote system can use your channel to listen for various requests for your remote type.
. NET Framework has two default channels: System.Runtime.Remoting.Channels.Http.HttpChannel (using SOAP format) and System.Runtime.Remoting.Channels.Tcp.TcpChannel (using binary format). In some cases, HttpChannel can pass through the firewall without opening ports (port), and it supports standard security and authentication protocols.
You can use any type of application domain (Windows Forms application, ASP.net Web application, console application, Windows service, and other managed application domains) to create listening applications. Because the remote configuration is based on each application domain, the application domain must listen for requests.
"Note" differs from COM, and the remoting framework does not initiate a listener application or server application for you.
Remote configuration can be implemented at the programming stage, or it can be implemented using the application or machine configuration file. Using a configuration file allows you to change the remoting configuration without recompiling your executable file. See the following code:
[Visual Basic]
' Listener.vb
Imports System
Imports System.Runtime.Remoting

Public Class Listener
Public Shared Sub Main ()
RemotingConfiguration.Configure ("Listener.exe.config")
Console.WriteLine ("Listening for requests.") Press Enter to exit ... ")
Console.ReadLine ()
End Sub ' Main
End Class ' Listener
[C #]
Listener.cs
Using System;
Using System.Runtime.Remoting;

public class listener{
public static void Main () {
RemotingConfiguration.Configure ("Listener.exe.config");
Console.WriteLine ("Listening for requests.") Press Enter to exit ... ");
Console.ReadLine ();
}
}
To compile the above class into a listening service executable using the command-line tools that are included with the. NET Framework SDK Just save it as listener.language-extension (here language-extension is the language you use, such as CS, VB). Save the file to the same directory in RemotableType.dll. Compile-time using the following command:
[Visual Basic]
Vbc/r:remotabletype.dll Listener.vb

[C #]
Csc/noconfig/r:remotabletype.dll Listener.cs

The Listener class must be able to locate the Listener.exe.config file to load the configuration for the RemotableType class. The profile must be saved in the same directory as Listener.exe. If not found, an exception will be generated. The following is a description of the configuration file Listener.exe.config:
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown
Mode= "Singleton"
Type= "RemotableType, RemotableType"
Objecturi= "Remotabletype.rem"
/>
</service>
<channels>
<channel ref= "http" port= "8989"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
The remoting system uses the information in the configuration file to listen and route remote requests for instances of the remote type. The file specifies a single service activation mode (server-activation mode), the type name, the assembly that contains the type to be listened to, or the URI of the object (or the external name of the object) that specifies that the remoting system listens with HttpChannel on port 8989.

Third, the creation of customer applications:
The client application must be registered. The remoting system intercepts the client's call, sends the call to the remote object, and returns the result to the client. The customer applies the following code:
[Visual Basic]
' Client.vb
Imports System
Imports System.Runtime.Remoting

Public Class Client
Public Shared Sub Main ()
RemotingConfiguration.Configure ("Client.exe.config")
Dim RemoteObject as New remotabletype ()
Console.WriteLine (Remoteobject.stringmethod ())
End Sub ' Main
End Class ' Client
[C #]
Client.cs
Using System;
Using System.Runtime.Remoting;

public class client{

public static void Main () {
RemotingConfiguration.Configure ("Client.exe.config");
RemotableType remoteobject = new RemotableType ();
Console.WriteLine (Remoteobject.stringmethod ());
}
}
To compile the corresponding client executable file, simply save it as client.language-extension (here language-extension is the language you are using, such as CS, VB). Save the file to the same directory in RemotableType.dll. Compile-time using the following command:
[Visual Basic]
Vbc/r:remotabletype.dll Client.vb

[C #]
Csc/noconfig/r:remotabletype.dll Client.cs

As you can see, the client class must be able to find the Client.exe.config file to load the configuration for the RemotableType class. The profile must be saved in the same directory as Client.exe. If not found, an exception will be generated. The following is a description of the configuration file Client.exe.config:
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown
Type= "RemotableType, RemotableType"
Url= "Http://localhost:8989/RemotableType.rem"
/>
</client>
</application>
</system.runtime.remoting>
</configuration>
This profile notifies the remoting system that the type information for the remote object can be found in the RemotableType Assembly (RemotableType). At the same time, the client should attempt to create and use the RemotableType object located in Http://localhost:8989/RemotableType.rem. When you try to run the program on the network, you must replace the "localhost" in the client profile with the remote computer name.

Compile and execute the entire application:
Save all previously built files into a directory named Listener and use the following command line:
Visual Basic]
Vbc/t:library Remotabletype.vb

Vbc/r:remotabletype.dll Listener.vb

Vbc/r:remotabletype.dll Client.vb

[C #]
Csc/noconfig/t:library RemotableType.cs

Csc/noconfig/r:remotabletype.dll Listener.cs

Csc/noconfig/r:remotabletype.dll Client.cs

Run the application:
1. To create a subdirectory named client
2, copy RemotableType.dll, Client.exe and Client.exe.config to the Client directory
3, in the Listener directory, the use of the following command:
Listener
4. When the listener program is running, open a new command window in the client directory and type:
Client

Change Channel:
You can change the channel as long as you modify the Listener.exe.config and Client.exe.config files. The Client.exe.config file is modified as follows:
<wellknown
Type= "RemotableType, RemotableType"
Url= "Tcp://localhost:8989/remotabletype.rem"
/>

The Listener.exe.config file is modified as follows:
<channel ref= "tcp" port= "8989"/>

"Note" The content covered in this article is limited to the. NET Framework 1.x.



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.