.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", select Create a C # Library and name it RESUMESERVERLIBRARY, then click 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 use the following code to replace the ResumeLoader object:
Public Class ResumeLoader: System.MarshalByrefObject
{
Public ResumeLoader ()
{
System.console.writeline ("New Reference Added!");
}
Public Resume getResuMeByUserId (Decimal UserID)
{
Return New Resume (1);
}
}
The namespace 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.runtime;
Using system.data.sqlclient;
We use the name space for objects to DotNetRemotest, the following objects are MarshalByrefObject, where we created a reference and all the work required to complete all of the server-side database operations.
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 vary, 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 Reference 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 the aresume where there =RID =" userid ""
DBConnection
);
SqldataReader areader = cmd.executeReader ();
is (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 the object will be converted to the original data transmitted through the network, and then mounted in the other end of the network to an object. .
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 Resume)
{
THIS.RESUMEID = ResumeId;
THIS.USERID = 1;
THIS.BODY = "this is the default body of the resme";
THIS.TITLE = "this is the default title";
}
Public Decimal ResumeID
{
Get {return resumeid;}
Set {this.resumeid = value;
}
Public Decimal Userid
{
Get {return userid;}
Set {this.userid = value;
}
Public String Body
{
Get {return body;}
Set {this.body = value;
}
Public String Title
{
Get {returnit;}
Set {this.title = value;}
}
} // Resume end
} // DotNetRemotetest name space end
Compiling and created projects will get a DLL file and can be used in other projects.
Step 2: Create 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" (Command line application) and name it RESUESUPERSERVER.
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);
Another channel type that can be set is http, as long as it simply uses the httpserverchannel object in the system.runtime.remoting.channels.http namespace, you can get it. 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 (Typeof (ResumeLoader),
"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 the object that is bound, the second parameter is a string of the remote object name in the TCP or HTTP channel. The third parameter allows the container to know how the object should be handled when there is a request to the object. Although WellkNownObjectMode.single uses an instance of an object to all callars, it generates an instance of this object for each customer.
The complete object code is 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 DotNetRemotest;
Namespace ResumeServerServer
{
Public Class ResumeSuperServer
{
Public static void main (string [] args)
{
TCPServerChannel Channel = New TCPSERVERCHANNEL (9932);
ChannelServices.RegisterChannel (Channel);
RemotingConfiguration.registerwellknownServiceType (TypeLoader), "ResumeLoader", WellknownObjectMode.singlecaLL);
System.console.writeLine ("Press Any Key");
System.console.readline ();
}
}
}
Compile this process and pay attention to the location of the generated .exe file.
Step 3: Create a REMOTE client program
ResumeClinet is created for testing the RESUMESUPERSERVER, which is 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.
ChannelServices.RegisterChannel (New TcpClientChannel ());
ResumeLoader Loader = (ResumeLoader) Activator.getObject
TypeOf (ResumeLoader), "TCP: // localhost: 9932 / resmeplander");
The total code of ResumeClient is as follows:
Using system;
Using system.runtime.remoting;
Using system.runtime.remoting.channels;
Using system.runtime.remoting.channels.tcp;
Using DotNetRemotest;
Namespace ResumeClient
{
Public Class ResumeClient
{
Public static void main (string [] args)
{
ChannelServices.RegisterChannel (New TcpClientChannel ());
ResumeLoader Loader = (ResumeLoader) Activator.getObject
TYPEOF (RESUMESERVER), "TCP: // localhost: 9932 / resmeplander");
IF (rs == null)
{Console.WriteLine ("Unable to get remote remote refer");
Else
{
Resume resume = loadinger.getResumeByUserId (1);
Console.writeline ("ResumeID:" resume.ResumeID);
Console.writeline ("UserID:" resume.Userid);
Console.writeline ("Title:" resume.title);
Console.writeline ("Body:" resume.body;
}
Console.readLine (); // Do not let the window close before you can see the result
} // End of main Method
} // End of resultsClient Object
} // End of resultsClientNamespace
test
Create a table with the following structure in the database:
Table Name-Resume
ResumeID, Numeric (Autonumber)
Userid, Numeric
Title, Char (30)
Body, Text
Double-click the Server.exe that we created in the second step, then double-click the Client executable created in the third step. If everything is fine, we should be able to see the record line of the value of ResumeID in the database.
In short, .NET Remoting is very simple, and provides an excellent method for the resource within the network or even Internet.
From:
http://edu.cn700.com