How to: Use Visual C # .NET to get the object to the remote server (from MSDN)

xiaoxiao2021-03-06  57

The release number of this article has been CHS307546

This article discusses a Beta version of Microsoft products. The information in this article is provided as "as", if there is any change without notice.

For this Beta product, Microsoft does not provide formal product support. For information on gain support for Beta versions, see the documentation included in the beta product file or view the site you downloaded.

For Microsoft Visual Basic .NET versions of this article, see

301116.

This task content

Summary

Create a remote server object Create a remote server application to create a client application reference

SUMMARY This article demonstrates how to press values ​​to seal the object to the remote server. When the value is enamel the object, a copy of the object is created and serialized it to the server. Any method call to the object is performed on the server. Because you need to transmit object serialization to the server, you must use

[Serializable] Property Modification To pass the class definition of the object.

This article describes some of the following concepts described in the following Microsoft Knowledge Base Articles:

300951 HOW TO: CREATE A Remote Server Using Visual Basic .NET (HOW TO: Create a remote server using Visual Basic .NET)

300943 How to: Create a Client To a Remote Server Using Visual Basic .NET (How to: Create a client using Visual Basic .NET)

Note: Complete steps in this article

You don't need to read these articles.

Back to top

The following table summarizes the recommended hardware, software, network architecture, and service pack required:

Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server or Windows NT 4.0 Server Microsoft Visual Studio .NET This article assumes that you are familiar with the following topics:

Visual Studio .NET Network Basics

Back to top

Creating a remote server object Creating a server application is to create a server object. Server objects On servers, client applications must instantiate it and communicate with it. The client application is achieved by a proxy object created on the client. Your name

HelloServer's server objects will be placed in a class library (DLL). In the same project, it also defines the class that will be passed from the client to the server. Such name

FORWARDME. Because you want to get a value

Forwardme class, so you need to use

[Serializable] attribute modification

ForwardMe class.

Open Visual Studio .NET. Create a new Visual C # .NET class library application. Create Class1.cs by default. In the Solution Explorer, rename the class1.cs file to ServerClassValue.cs. Open ServerClassValue.cs. Import the SYSTEM.DIAGNOSTICS namespace, so that you don't need to fully qualify the name in the code. Using system;

Using system.diagnostics; add two classes, HelloServer and Forwardme. HelloServer is inherited from the MarshalByrefObject class, and the ForwardMe class uses the Custom Properties. This tag allows the ForwardMe class to input and output the server in the form of a FORWARDME class. HelloServer is the primary class used by client applications. ForwardME is used to send object data from the client to the server. Because Forwardme is not inherited from MarshalByRefObject, it passes it to the server by value. Public Class HelloServer: MarshalByrefObject {

}

[Serializable]

Public Class ForwardMe

{

} Add public methods HellometHod to HelloServer, the latter uses the ForwardMe object as a parameter. Use this method to pass the ForwardMe object to the server. This method calls the CallME method of this object. Your HelloServer class should now look like this: Public Class HelloServer: MarshalByrefObject

{

Public void HellometHod (Forwardme Obj)

{

Obj.callme ();

}

} Add a common method to the ForwardMe class and name this method according to the process of executing this code. This code is run in the server's process because you pass the entire object sequence to the server (by value). [Serializable]

Public Class ForwardMe

{

Public void calm ()

{

Console.writeline ("Callme WAS EXECUTED IN:

Process.getcurrentProcess (). ProcessName.toString ());

}

} Generate this item to create a ServerClassValue.dll assembly. Save and close the project.

Back to top

Creating a Remote Server Application After creating a client object that will communicate with it, you must register this object in the remote processing framework. Registration includes not only registering objects, but also launches a server and allowing it to listen to a port so that the client is connected. To do this, you need a project type that creates an executable file. The server object is included in another project so that you can easily reference server objects from the client. If you include a server object in this project, you cannot reference it because you can only set to the DLL file reference.

Open Visual Studio .NET. Create a Visual C # .NET console application to launch a remote server. Create Class1.cs by default. In the Solution Explorer, rename the class1.cs file as ServerObjectValue.cs. Add a reference to the project to the System.Runtime.Remoting namespace. Add a reference to the ServerClassValue.dll assembly (created in the Remote Server Object section). Using the USING statement for Remoting, Remoting.Channels and Remoting.Channels.tcp Namespace, this is not required to define declarations in these namespaces in the later code. The USING statement must be before all other declarations. Using system.runtime.remoting;

Using system.runtime.remoting.channels;

Using system.runtime.remoting.channels.tcp; declare the corresponding variable to initialize a TCPChannel object, the object monitors a port so that the client is connected (this example is 8085 port). The registration client will be used to communicate with the channel service using the RegisterChannel method. Add a declaration code during the MAIN of MAIN: TCPChannel CHAN = New Tcpchannel (8085); ChannelServices.RegisterChannel (CHAN); call the REGISTERWELLKNOWNTYPE method of the RemotingConfiguration object to register the ServerClassValue object to the remote processing frame. In the code, you must specify the following parameters:

The full type name of the object being registered (this example is ServerClassValue.Helloserver, followed by the assembly name ServerClassValue. The name and class name of the namespace must be specified. Since there is no namespace specified in the previous part, the default root namespace is used. The object will be issued to the name of the endpoint. The client needs to know this name to connect to the object. Please use RemotetestValue. Object mode can be SingleCall or Singleton. This example specifies SingleCall. The object mode specifies the validity period when the object is activated on the server. For SingleCall objects, a new instance of the class will create a new instance of the client, even if the same client is called multiple times. Alternatively, only one Singleton object is created, all clients communicate with the same object. RemotingConfiguration.registerWellkNownServiceType

Type.gettype ("ServerClassValue.helloServer, ServerClassValue",

"Remotetestref",

WellknownObjectMode.singlecall; READLINE method to keep the server application using the Console object: console.writeline ("Press to EXIT ...");

Console.readline (); generated project. Save and close the project.

Back to top

Create a client application

Open Visual Studio .NET. Create a console application in Visual C # .NET. Create Class1.cs by default. In the Solution Explorer, rename the class1.cs file to ClientAppValue.cs. Add a reference to the project to the System.Runtime.Remoting namespace. Add a reference to the ServerClassValue.dll assembly (created in the Remote Server Object section). Using the USING statement for Remoting, Remoting.Channels and Remoting.Channels.tcp Namespace, this is not required to define declarations in these namespaces in the later code. The USING statement must be before all other declarations. Using system.runtime.remoting;

Using system.runtime.remoting.channels;

Using system.runtime.remoting.channels.tcp;

USING ServerClassValue; Disclaims the corresponding variable to initialize a TCPChannel object, and the client will use the object to connect to the server application. Register the channel to the channel service using the RegisterChannel method. Then, a new ForwardMe object must be initialized, which will be passed to the remote server object. Remember to pass the value by value. Add a declaration code during the MAIN of MAIN: TCPChannel CHAN = New Tcpchannel (); ChannelServices.registerChannel (CHAN);

Forwardme Objforwardme = new forwardme (); declaration, and instantiate the remote server. In this example, instantiate the HelloServer object using the getObject method of the ACTIVATOR object. In the code, you must specify the following parameters:

The full type name of the object being registered (this example is ServerClassValue.Helloserver, followed by the assembly name ServerClassValue. The name and class name of the namespace must be specified. Since there is no namespace specified in the previous part, the default root namespace is used. The Unified Resource Identifier (URI) of the object to be activated. The URI must include the endpoint of the protocol (TCP), computer name (localhost), port (8085), and server object. To access the ServerClassValue remote server on the local server, the URI is TCP: // localhost: 8085 / RemotetestValue. HelloServer Objhelloserver;

Objhelloserver = (HelloServer) activator.getObject

TypeOf (HelloServer),

"TCP: // localhost: 8085 / transtetestref");

IF (ObjhelloServer == Null)

Console.writeline ("Could Not Locate Server");

Else

{

// See next step

} If the server object is instantiated, call the HellometHod of the server object and passes the newly created ObjForWardMe object to the method. Remember, HellometHod calls the callme method of the ForwardMe object, which displays the name of the process where the code is running to the console window. HelloServer Objhelloserver;

Objhelloserver = (HelloServer) activator.getObject

TypeOf (HelloServer),

"TCP: // localhost: 8085 / transtetestref");

IF (ObjhelloServer == Null)

Console.writeline ("Could Not Locate Server");

Else

{

Objhelloserver.hellomethod (Objforwardme);

Console.writeLine ("Method Executed, Check Server Window for Output.");

} Keep the client application to run the READLINE method: console.writeline ("Press to EXIT ...");

Console.readline (); generated project. Make sure the server application is running. Run the client project to test the communication between the client and the server. When you run a client, you will receive a notification when the method performs an end. Note that the output of the CallME method is displayed in the server's console window, not in the console window of the client. This is because the object is a values ​​to the server.

Back to top

Reference

For more information, please visit the following .NET Framework Class Library Web Site:

Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemRuntImeremotingChannelStcptcpchannelClasStopic.asp

For more information on the RegisterWellkNownServiceType method, please visit the following .NET Framework class library Web site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemRuntimeRemotingRemotingConfigurationClassRegisterWellKnownServiceTypeTopic.asp an introduction to Microsoft .NET Remoting Framework (general .NET Development Technical Articles Please visit the following MSDN sites:

http://msdn.microsoft.com/library/techart/remoting.htm

Back to top

The information in this article applies to:

Microsoft Visual C # .NET Beta 2

Recent Updated: 2001-11-5 (1.0) Keyword KBDsupport Kbhowto KbhowTomaster Kbnokeyword KB307546 KBAUDDEVELOPER

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

New Post(0)