. NET Remoting learning notes (3) channels, remoting learning notes
Directory
- . NET Remoting learning notes (1) Concepts
- . NET Remoting Study Notes (2) Activation Method
- . NET Remoting learning notes (3) Channels
Refer:♂Windmill. Net
The. NET Framework remote processing infrastructure provides the following channel implementation:
- IpcChannel
- TcpChannel
- HttpChannel
IpcChannel
IPCChannel is added in. NET Framework 2.0. It uses the Windows inter-process communication (IPC) system to transmit messages between application domains on the same computer. When communication is performed between application domains on the same computer, the IPC channel is much faster than the TCP or HTTP channel. However, IPC only communicates with local applications. Therefore, when the client and server are on the same machine, we can register IPCChannel to improve the Remoting performance. However, if the client and server are not on the same machine, we cannot register IPCChannel.
IpcChannel performs the following functions:
- Use a named pipe to communicate with the sender and receiver.
- Supports encoding loads in binary and industry-standard SOAP serialization formats.
- Generate and use the ChannelDataStore referenced by the object.
- Supports simulation and delegation.
- You can use the access control list (ACL) on the named pipeline to provide advanced access control.
TcpChannel
The TcpChannel class uses a binary formatter to serialize all messages into a binary stream, and uses the TCP protocol to transmit the stream to the destination Uniform Resource Identifier (URI ).
TcpChannel performs the following functions:
- Use a TCP socket to communicate with the sender and receiver.
- Supports encoding loads in binary and industry-standard SOAP serialization formats.
- Generate and use the ChannelDataStore referenced by the object.
- Supports simulation and delegation.
- Supports SSPI encryption.
HttpChannel
The HttpChannel class uses the SOAP protocol to transmit messages between remote objects. All messages are passed through SoapFormatter. This formatter converts the messages to XML for serialization and adds the required SOAP header to the data stream. If a binary formatter is also specified, a binary data stream is created. Next, data streams are transmitted to the target URI using the HTTP protocol.
HttpChannel complies with the SOAP 1.1 Standard and implements the following functions:
- The HTTP protocol is used for transmission between the sender and the receiver.
- Supports encoding load in SOAP (an XML encoding standard) and binary format.
- Set the receiver to receive HTTP requests and send HTTP responses through ASP. NET and TCP sockets.
- Generate and use the ChannelDataStore referenced by the object.
- Supports simulation and delegation.
- Supports SSPI encryption.
Below is the code:
1. Define remote objects
Using System; using System. runtime. remoting. metadata;/* code interpretation */namespace MessageMarshal {/* create a message sending delegate */public delegate void SendMessageHandler (string messge); [Serializable] public class TestMessageMarshal: export albyrefobject {private Guid ID {get; set;}/* re-create the ID number when creating an object instance */public TestMessageMarshal () {ID = Guid. newGuid ();}/* create a message sending event */public static event SendMessageHandler SendMessageEvent ;/* Send message */[SoapMethod (XmlNamespace = "MessageMarshal", SoapAction = "MessageMarshal # SendMessage")] public void SendMessage (string messge) {if (SendMessageEvent! = Null) SendMessageEvent (ID. ToString () + "\ t" + messge );}}}
2. Define the server
Using System; using System. runtime. remoting; using System. runtime. remoting. channels; using System. runtime. remoting. channels. http; using System. runtime. remoting. channels. ipc; using System. runtime. remoting. channels. tcp; using MessageMarshal; namespace TestRemotingServer {/* code: Sakya Nakhon */class Program {static void Main (string [] args) {// IpcChannel channel_ipc = new IpcChannel ("localhost: 8226"); // HttpChannel channel_http = new HttpChannel (8226); TcpChannel channel_tcp = new TcpChannel (8226 ); /* register the channel server */ChannelServices. registerChannel (channel_tcp, false); RemotingConfiguration. applicationName = "test"; RemotingConfiguration. registerActivatedServiceType (typeof (TestMessageMarshal); Console. writeLine ("started... ");/* receive client events */TestMessageMarshal. sendMessageEvent + = new SendMessageHandler (TestMessageMarshal_SendMessageEvent); Console. read ();} static void TestMessageMarshal_SendMessageEvent (string messge) {Console. writeLine (messge );}}}
3. Define the client:
Using System; using System. runtime. remoting; using System. runtime. remoting. channels; using System. runtime. remoting. channels. http; using System. runtime. remoting. channels. ipc; using System. runtime. remoting. channels. tcp; using System. threading;/* code Releaser */namespace TestRemotingClient {class Program {static void Main (string [] args) {// IpcChannel channel = new IpcChannel (); // HttpChannel channel_http = new HttpChannel (); TcpChannel channel_tcp = new TcpChannel (); ChannelServices. registerChannel (channel_tcp, false);/* Registration Channel Remote processing type * // RemotingConfiguration. registerActivatedClientType (typeof (MessageMarshal. testMessageMarshal), "ipc: // localhost: 8226/test"); // RemotingConfiguration. registerActivatedClientType (typeof (MessageMarshal. testMessageMarshal )," http://localhost:8226/test "); RemotingConfiguration. registerActivatedClientType (typeof (MessageMarshal. testMessageMarshal), "tcp: // localhost: 8226/test");/* create a message entity */MessageMarshal. testMessageMarshal TestMessage = new MessageMarshal. testMessageMarshal (); while (true) {TestMessage. sendMessage ("DateTime. now: "+ System. dateTime. now. toString (); Console. writeLine ("send message... "); Thread. sleep (2000 );}}}}
4. Test
Author: Sakya bitter monk Source: http://www.cnblogs.com/woxpp/p/3997984.html this article copyright belong to the author and blog park a total, welcome to reprint, but without the author's consent must retain this paragraph of the statement, and in the Article Page clearly given the original connection.
Net Remoting programming-client subscribe to server events
Step 1: create a shared library
Choose "file"> "new"> "project", create a c # library, name it resumeserverlibrary, and click "OK. This will create a "shared command set" for communication between our. net remote client and the server ".
The front is the complete code. to skip the database access section, you can use the following code to replace the resumeloader object:
Public class resumeloader: system. externalbyrefobject
{
Public resumeloader ()
{
System. console. writeline ("new referance added! ");
}
Public resume getresumebyuserid (decimal userid)
{
Return new resume (1 );
}
}
Namespace is required by the object. Remember, if you get system. runtime. remoting. channels. the tcp namespace does not exist. Check whether the system is added as the code above. runtime. remoting. dll reference.
Using system;
Using system. runtime;
Using system. data. sqlclient;
The namespace we use for the object is dotnetremotetest. The following object is marshalbyrefobject. In this space, we create a reference and include server-side database operations to complete all the required work.
Namespace dotnetremotetest
{
Public class resumeloader: system. externalbyrefobject
{
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 do not 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 = new resume ();
Try
{
Dbconnection. open ();
Sqlcommand cmd = new sqlcommand (
"Select resumeid, userid, tit ...... remaining full text>
The difference between WebService and Remoting in the net Framework
The difference between Web Service and Remoting is as follows:
Web services are divided into five levels:
1. Http Transmission Channel
2. XML data format
3. SOAP Encapsulation Format
4. Description of WSDL
5. UDDI
The Web Service structure in. NET is relatively simple and easy to understand and apply:
Generally, WebService applications in the. NET structure are based on the. net framework and IIS architecture, so it is relatively easy to deploy.
From the implementation perspective,
First, WebService must inherit the class of the method exposed to the client from the base class: System. Web. Services. WebService.
Second, the exposed methods must be preceded by [WebMethod] or [WebMethodAttribute].
Running Mechanism of WebService
First, the client uses the WSDL from the server to the WebService, and claims a Proxy Class on the client)
This proxy class is responsible for Request and Response with the WebService Server
When a data (in XML format) is encapsulated into a data stream in SOAP format and sent to the server, a process object is generated and the SOAP packet that receives the Request is parsed, then, the transaction is processed. After the processing is completed, the computation result is packaged with SOAP, and then the package is used as a Proxy Class sent to the client as a Response. Similarly, this proxy class also parses the SOAP package and then performs subsequent operations.
This is a running process of WebService.
And. net Remoting is:
. Net Remoting is a technology developed on the basis of DCOM. Its main purpose is to achieve cross-platform, cross-language, and penetration of Enterprise Firewall, which is also its basic feature, different from WebService, WebService supports HTTP and TCP channels. It can not only transmit SOAP packets in XML format, but also transmit binary streams in the traditional sense, this makes it more efficient and flexible. In addition, it does not rely on IIS, and users can develop (Development) and deploy their favorite host servers. Therefore, in these aspects, WebService is actually a special case of. net Remoting.
WebService and Remoting are compared as follows:
WebService features Platform-independent, cross-language (any language that supports XML), and enterprise firewall penetration.
However, its disadvantage is that a Web Server needs to be deployed, and the speed is relatively slow;
. Net Remoting features
The advantage is that users can use both TCP-based binary stream communication and HTTP-based SOAP-based communication.
The efficiency is much higher than that of WebService, but its disadvantages are also obvious.. net remoting can only be applied under the. net framework of MS.
In terms of performance, the efficiency of Remoting is very similar to that of traditional DCOM and COM +!