Use of .NET Remoting framework (original)

xiaoxiao2021-03-06  72

Microsoft .NET Remoting provides a framework that allows objects to interact with another object via application domain. One of its purposes is to provide a necessary basic interface, a method of hiding the remote object call and the complexity of the return result.

Let's introduce the method of using the framework:

First we have to introduce the necessary name space

Using system;

Using system.runtime.remoting;

Using system.runtime.remoting.channels;

Using system.runtime.remoting.channels.tcp;

The use of the namespace believes that everyone knows. In fact, it is to make it easier to write code (individual think)

Let's introduce the method of these namespaces.

SYSTEM wants me not to say that everyone knows that this name space will be referenced in each .net program.

Name Space Using System.Runtime.Remoting; Provides the base class and interface required to create remote distributed applications, mainly 3 types of RemotingConfiguration, RemToingServices, and Objref classes

RemotingConfiguration class Configure remote distributed programs through a series of static methods

The RemToingServices class provides a range of ways to use and publish remote objects

Objref class encapsulates all necessary information for remote objects

Name Space System.Runtime.Remoting.Channels provides various classes of using and operating "channels"

Name Space System.Runtime.Remoting.Channels.tcp provides various classes for TCP protocols to establish "channels"

Next is the establishment of remote object classes

Public Class RemoteObject: System.MarshalByrefObject

{

Public RemoteObject ()

{}

Publicovid SayHello () // Method for Remote Objects

{

Console.write ("Hello this is a test"); // Print on the client

}

}

Remote objects must inherit from Systen.MarshalByrefObject, which must also define them as a public object to be called by the client, and the remote object we defined here contains a method Sayello.

Remote objects are visited by client access to the server code, let's introduce the server-side code.

Write the following code within the main () method of the server side

RemoteObject RemoteObject; // Initialize the remote object on the server side

Tcpchannel Channel = New TcpChannel (8090); // Initialize the monitor port with constructor

ChannelServices.RegisterChannel (CHANNEL); // Register this port

RemotingConfiguration.registerwellknownServiceType (TypeObject),

"Remotehello", WellkNownObjectMode.singleton;

RemoteObject = (transoteObject) Activator.getObject (TypeObject), "TCP: // localhost: 8090 / remotehello");

The server mainly implements the code. This will explain the meaning of these code.

The REGISTERWELLKNOWNSERVICETYPE of the RemoteConfiguration class is used to register the remote object. It has three parameters. The first parameter is used to specify the type of the remote object. The second parameter is the URI of the remote object. When you want to access the object from the client To this parameter, the last parameter is the mode of the remote object. There are 2 modes to choose both Singleton and SingleCall. The difference between these two methods is indicated by the SINGLECALL means that it is not saved, that is, every time you call it once. Remote objects have to create a new instance; all customers of the Singleton method can coexist objects. The getObject of the Activator class is used to create a remote object. It has 2 parameters. The first parameter is used to specify the type of the remote object. The second parameter is the URI of the remote object. Remotehello is the URI of the object, that is, us Specified when registered with the RegisterWellkNownServiceType method of the RemoteConfiguration class.

Let's introduce the client code, write the following code in the client's main () method

RemoteObject RemoteObject; // Initializing the remote object

TCPChannel Channel = New TcpChannel (0); // Initialize the connection port with constructor, we will explain it below.

ChannelServices.RegisterChannel (CHANNEL); // Register this port

RemoteObject = (transoteObject) Activator.getObject (TypeObject),

"TCP: // localhost: 8090 / transotehello");

RemoteObject.SAYHELLO (); // call remote method

The main code of the client is these

TCPChannel Channel = New TcpChannel (0); The initialized port is not 0 port, which automatically assigns a free port to connect the remote server.

If you want to use new NEW to use New to activate the remote object and there is a method

RemotingConfiguration.registerWellknownClientType (TypeObject),

"Remotehello", WellkNownObjectMode.singleton;

RemoteObject RemoteObject = New RemoteObject ();

转载请注明原文地址:https://www.9cbs.com/read-84855.html

New Post(0)