.NET Remoting

xiaoxiao2021-03-06  38

.NET Remoting provides a powerful and efficient way to process the remote object. From the structure, the .NET Remote object is ideal for accessing resources through the network without having to handle the challenges of SOAP-based WebServices. .NET Remoting uses simpler than Java's RMI, but it is more difficult than creating a Web Service. In this article, we will create a remote object that reads content from the database. The article also includes a replacement object that ignores the database function so that the readers that can be used without a database can still use .NET Remoting. Step 1: Create a shared library Click "File" -> "New Create" -> "Project" to select Create a C # library, then name the OK button. This will create a "shared command set" for our .NET Remote client and server. The front is a complete code. If you want to skip the database access section, you can replace the ResumeLoader object using the following code: public class resuminer: System.MarshalByrefObject {public resumeLoader () {System.console.writeLine ("New Reference Added!"); } Public Resume getResuMeByUserId (Decimal Userid) {Return New Resume (1);} The name space is required for the object. Keep in mind that if you get the information that does not exist in system.Runtime.Remoting.Channels.TCP namespace, check if you add a reference to System.Runtime.Remoting.dll as the code above. Using system; using system.data.runtime; using system.data.sqlclient; We use the namespace for objects to DotNetRemotetest, the following object is MarshalByrefObject, where we created a reference and including server-side database operations All required to complete all required jobs. namespace DotNetRemoteTest {public class ResumeLoader: System.MarshalByRefObject {private SqlConnection dbConnection; public ResumeLoader () {this.dbConnection = new System.Data.SqlClient.SqlConnection (); this.dbConnection.ConnectionString = "data source = GRIMSAADO2K; initial catalog = Underground; integrated security = SSPI; PERS " " IST security info = true; workstation id = grimsaado2k; packet size = 4096 "; / * The specific connection string will be different, which is beyond the scope of this article. If you don't know how to create a database connection, use another version of this object.

* / System.Console.WriteLine ( "New Referance Added!");} Public Resume GetResumeByUserID (decimal userID) {Resume resume = new Resume (); try {dbConnection.Open (); SqlCommand cmd = new SqlCommand ( "SELECT ResumeID , userID, Title, Body FROM Resume as theResume WHERE theResume.UserID = " userID " ", dbConnection); SqlDataReader aReader = cmd.ExecuteReader (); if (aReader.Read ()) {resume.ResumeID = aReader.GetDecimal (0); resume.Userid = areader.getdecimal (1); resume.title = areader.getstring (2); resume.body = areader.getstring (3);} areader.close (); dbconnection.close (); } CatCH (Exception x) {resume.title = "error:" x;} Return Resume;}} Resume needs to be serialized so that it can be used as a return type of the .NET Remote object that is remotely called because it is The object will be converted to the original data transmitted through the network, and then assembled into an object on the other end of the network. This object is very simple, in order to make this article look simpler, the constructor even initializes some of the domains in the default content. [Serializable] public class Resume {private decimal resumeID, userID; private String body, title; public Resume (decimal resumeID) {this.ResumeID = resumeID; this.UserID = 1; this.Body = "This is the default body of the ";" this is the default title ";} public decimal resumeid {get {return resumeid;} set {this.resumeid = value;}} public decimal userid}} public decimal userid}} public decimal userid;}. Userid = value;}} public string body {get {return.body;} set} public string title;}} public string title;}}}}}}}} // resume Object End} // DOTNETREMOTETEST Name Space End Compilation Create Project, you get a DLL file and use it in other projects.

Step 2: Creating a Server object There are several ways to create a Server object. The most intuitive method is the following method: in Visual Studio.net, click "File" -> "New Creation" -> "Project", select Create a "Command Line Application" and name it RESUSUPERSERVER. The most important thing is that we need to add applications that have been created in the first step, which can operate correctly. Click "Project" -> Add References in turn, then add a reference to the DLL file created in the first step by clicking the "Browse" button. To use the .NET Remote feature, you must add a reference to the DLL file by selecting "Project" -> Add Reference. Select System.Runtime.Remoting.dll in the .NET tab, and then click the "OK" button. Then, you need to add a reference to System.Runtime.Remoting.DLL as we in the first step. The following object is quite simple and intuitive, I will explain each line of the three-line code related to .NET Remoting. TCPServerChannel is one of the two channel types supported by .NET Remoting, which will set our object to respond to the request from which port, ChannelServices.RegisterChannel will put the port number and the TCP / IP in the operating system. Stack binding. TcpServerChannel channel = new TcpServerChannel (9932); ChannelServices.RegisterChannel (channel); a channel may be provided another type of HTTP, it is simply used HttpServerChannel System.Runtime.Remoting.Channels.Http namespace objects to get. The difference between the HTTP and TCP channels can be simply concatenated: if the application is running on the LAN, it is best to use the TCP channel because its performance is better than the HTTP channel; if the application is running on the Internet, then Sometimes the HTTP is the only choice according to the configuration of the firewall. It is necessary to remember that if a firewall software is used, the firewall should configure the TCP data traffic through the port selected by you. RemotingConfiguration.regiSterwellknownServiceType (Type Foader "," ResumeLoader ", WellkNownObjectMode.singlecall); This line code sets some of the parameters in the service and binds the name of the object you want to use with the remote object. The first parameter is bound. Objects, the second parameter is a string of the remote object name in the TCP or HTTP channel, and the third parameter allows the container to know when there is a request to the object, how to handle the object. Although WellkNownObjectMode.single uses an instance of an object to all callars, it generates an instance of this object for each customer.

Complete object code as follows: using System; using System.Runtime; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using System.Data.SqlClient; using DotNetRemoteTest; namespace ResumeServerServer {public class ResumeSuperServer {public static void Main (String [] args) {TcpServerChannel channel = new TcpServerChannel (9932); ChannelServices.RegisterChannel (channel); RemotingConfiguration.RegisterWellKnownServiceType (typeof (ResumeLoader), "ResumeLoader", WellknownObjectMode.singlecall; system.console.writeline ("press any key"); system.console.readline ();}}} Compiles this program and pay attention to the location of the generated .exe file. Step 3: Creating a Remote client program ResumeClinet is created for testing of the RESUMESUPERSERVER far and object created above. To create this project, click "File" -> "Create" -> "Project" in turn, then select Create a console application type, the name is the project name of ResumeClient. As in the second step, we need to add a reference to the DLL file created in the first step and System.Runtime.Remoting DLL. Both the following code is especially important for .NET Remoting. The first line creates a TCP client channel that is not binding on a port; the second row gets a reference to the remote ResumeAder object. The activator.getObject method returns a value of an object type, and we will then return the value it returned to ResumeLoader. We pass the parameters that we passed to RemotingConfiguration in the server engineering, the first parameter is the object type, and the second parameter is the URI of the remote object.

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

New Post(0)