Learn WCF with me (2) -- use. NET Remoting technology to develop distributed applications and wcfremoting

Source: Internet
Author: User
Tags msmq

Learn WCF with me (2) -- use. NET Remoting technology to develop distributed applications and wcfremoting
I. Introduction

In the previous blog, I shared the Message Queue (MSMQ) technology to implement distributed applications. In this blog, I continue to share another distributed technology,. NET Remoting, under the. NET platform.

II. Introduction to. NET Remoting 2.1. NET Remoting

. NET REmoting is different from MSMQ. It does not support offline availability. It is also suitable for communications between. NET platform programs. It provides a framework that allows an object to interact with another object through the application domain .. . NET applications are executed in one primary application domain, and the code in one application domain cannot access data in another application domain. However, in some cases, we need to communicate with other application domains across application domains. NET Remoting technology to interact with objects in another program domain.

2.2. NET Remoting Basic Principles

. NET Remoting technology implements communication between objects between two applications through channels. First, the client obtains the server object through the Remoting technology and access channel, and then resolves it as the client object, also known as transparent proxy. At this time, the obtained client object is only a reference of the server object. This ensures loose coupling between the client and the server, and optimizes the communication performance. In this process, when the client uses a transparent proxy to call the remote object method, the call is encapsulated into a message object, which includes remote object information, the called method name and parameter. Then, the transparent proxy will generate an IMethodCallMessage by entrusting the call to the Invoke method of the real proxy (RealProxy object, then, the message object is serialized into a data stream and sent to the channel. The channel then delivers the data stream to the server. After the server receives formatted data, it first restores the message object through deserialization, activates the remote object on the server end, and calls the corresponding method, the return result process of the method is repeated in the reverse direction according to the previous method. The implementation principle is as follows:

1 namespace RemotingObject 2 {3 // Step 1: Create a remote object 4 // create a remote object -- must inherit MarshalByRefObject, which supports object cross-domain access to 5 public class MyRemotingObject: export albyrefobject 6 {7 // used to test Tcp Channel 8 public int AddForTcpTest (int a, int B) 9 {10 return a + B; 11} 12 13 // used to test Http Channel 14 public int MinusForHttpTest (int a, int B) 15 {16 return a-B; 17} 18 19 // used to test IPC Channel 20 public int MultipleForIPCTest (int a, int B) 21 {22 return a * B; 23} 24} 25}

The remote object defines three methods respectively to test the effects of different channel methods in 3.

Step 2: Create a server and add System. Runtime. Remoting. dll reference. The specific implementation code is as follows:

1 using System; 2 using System. runtime. remoting; 3 using System. runtime. remoting. channels; 4 using System. runtime. remoting. channels. http; 5 using System. runtime. remoting. channels. ipc; 6 using System. runtime. remoting. channels. tcp; 7 8 namespace RemotingServerHost 9 {10 // Step 2: Create the Host application 11 class Server12 {13 static void Main (string [] args) 14 {15 // 1. create three channels 16 17 // create Tcp channel, port number 900118 TcpChannel tcpChannel = new TcpChannel (9001); 19 20 // create Http channel, port 900221 HttpChannel httpChannel = new HttpChannel (9002); 22 23 // create an IPC channel, port 900324 IpcChannel ipcChannel = new IpcChannel ("IpcTest"); 25 26 // 2. register channel 27 ChannelServices. registerChannel (tcpChannel, false); 28 ChannelServices. registerChannel (httpChannel, false); 29 ChannelServices. registerChannel (ipcChannel, false); 30 31 // print channel information 32 // print Tcp channel name 33 Console. writeLine ("The name of the TcpChannel is {0}", tcpChannel. channelName); 34 // print the Tcp channel priority 35 Console. writeLine ("The priority of the TcpChannel is {0}", tcpChannel. channelPriority); 36 37 Console. writeLine ("The name of the HttpChannel is {0}", httpChannel. channelName); 38 Console. writeLine ("The priority of the httpChannel is {0}", httpChannel. channelPriority); 39 40 Console. writeLine ("The name of the IpcChannel is {0}", ipcChannel. channelName); 41 Console. writeLine ("The priority of the IpcChannel is {0}", ipcChannel. channelPriority); 42 43 // 3. register object 44 // register MyRemotingObject.. NET Remoting Runtime Library 45 RemotingConfiguration. registerWellKnownServiceType (typeof (RemotingObject. myRemotingObject), "MyRemotingObject", WellKnownObjectMode. singleton); 46 Console. writeLine ("Press any key to exit"); 47 Console. readLine (); 48} 49} 50}

Step 3: Create a client program. The specific implementation code is as follows:

1 using RemotingObject; 2 using System; 3 4 namespace RemotingClient 5 {6 class Client 7 {8 static void Main (string [] args) 9 {10 // use the Tcp channel to obtain the remote object 11 // TcpChannel tcpChannel = new TcpChannel (); 12 // ChannelServices. registerChannel (tcpChannel, false); 13 MyRemotingObject proxyobj1 = Activator. getObject (typeof (MyRemotingObject), "tcp: // localhost: 9001/MyRemotingObject") as MyRemotingObject; 14 if (proxyobj1 = null) 15 {16 Console. writeLine ("failed to connect to TCP server"); 17} 18 19 // HttpChannel httpChannel = new HttpChannel (); 20 // ChannelServices. registerChannel (httpChannel, false); 21 MyRemotingObject proxyobj2 = Activator. getObject (typeof (MyRemotingObject )," http://localhost:9002/MyRemotingObject ") As MyRemotingObject; 22 if (proxyobj2 = null) 23 {24 Console. writeLine ("failed to connect to Http server"); 25} 26 27 // IpcChannel ipcChannel = new IpcChannel (); 28 // ChannelServices. registerChannel (ipcChannel, false); 29 MyRemotingObject proxyobj3 = Activator. getObject (typeof (MyRemotingObject), "ipc: // IpcTest/MyRemotingObject") as MyRemotingObject; 30 if (proxyobj3 = null) 31 {32 Console. writeLine ("failed to connect to Ipc server"); 33} 34/ /Output 35 Console. writeLine ("This call object by TcpChannel, 100 + 200 = {0}", proxyobj1.AddForTcpTest (100,200); 36 Console. writeLine ("This call object by HttpChannel, 100-200 = {0}", proxyobj2.MinusForHttpTest (100,200); 37 Console. writeLine ("This call object by IpcChannel, 100*200 = {0}", proxyobj1.MultipleForIPCTest (100,200); 38 Console. writeLine ("Press any key to exit! "); 39 Console. ReadLine (); 40} 41} 42}

After the three steps above, we have completed the development of this distributed application. Next we will test whether the program can run normally. First, run the server and you will see the following interface:

1 using System; 2 using System. runtime. remoting; 3 using System. runtime. remoting. channels; 4 5 namespace RemotingServerHostByConfig 6 {7 class Program 8 {9 static void Main (string [] args) 10 {11 RemotingConfiguration. configure ("RemotingServerHostByConfig.exe. config ", false); 12 13 foreach (var channel in ChannelServices. registeredChannels) 14 {15 // print the channel name 16 Console. writeLine ("The name of the Chann El is {0} ", channel. channelName); 17 // print channel priority 18 Console. writeLine ("The priority of the Channel is {0}", channel. channelPriority); 19} 20 Console. writeLine ("press any key to exit ...... "); 21 Console. ReadLine (); 22} 23} 24}

The content of the server configuration file is:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <! -- Server App. config content --> 3 <configuration> 4 <startup> 5 <supportedRuntime version = "v4.0" sku = ". NETFramework, Version = v4.5 "/> 6 </startup> 7 <system. runtime. remoting> 8 <application> 9 <service> 10 <wellknown mode = "Singleton" 11 type = "RemotingObject. myRemotingObject, remotingObject "12 objectUri =" MyRemotingObject "/> 13 </service> 14 <channels> 15 <channel port =" 9001 "ref =" tcp "/> 16 <channel port =" 9002 "ref =" h Ttp "/> 17 <channel portName =" IpcTest "ref =" ipc "/> <! -- The Ipc channel does not require the port number --> 18 </channels> 19 </application> 20 </system. runtime. remoting> 21 </configuration>

The implementation code of the client program is as follows:

1 using RemotingObject; 2 using System; 3 using System. runtime. remoting; 4 5 namespace RemotingClientByConfig 6 {7 class Program 8 {9 static void Main (string [] args) 10 {11 // obtain remote object 12 RemotingConfiguration through the HTTP channel. configure ("RemotingClientByConfig.exe. config ", false); 13 MyRemotingObject proxyobj1 = new MyRemotingObject (); 14 if (proxyobj1 = null) 15 {16 Console. writeLine ("failed to connect to the server"); 17} 18 19 Consol E. writeLine ("This call object by TcpChannel, 100 + 200 = {0}", proxyobj1.AddForTcpTest (100,200); 20 Console. writeLine ("This call object by HttpChannel, 100-200 = {0}", proxyobj1.MinusForHttpTest (100,200); 21 Console. writeLine ("This call object by IpcChannel, 100*200 = {0}", proxyobj1.MultipleForIPCTest (100,200); 22 Console. writeLine ("Press any key to exit! "); 23 Console. ReadLine (); 24} 25} 26}

The client configuration file is:

 1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3     <startup>  4         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 5     </startup> 6   <system.runtime.remoting> 7     <application> 8       <client> 9         <wellknown  type="RemotingObject.MyRemotingObject,RemotingObject"10                     url="http://localhost:9002/MyRemotingObject" />11       </client>12       <channels>    13         <channel ref="tcp" port="0"></channel>14         <channel ref="http" port="0"></channel>15         <channel ref="ipc" port="0"></channel>16       </channels>17     </application>18   </system.runtime.remoting>19 </configuration>

The running results of the distributed program modified using the configuration file are the same as those of the previous run.

V. Summary

Here ,. the sharing of NET Remoting technology is over. NET Remoting technology has made a basic introduction, if you want to learn more. NET Remoting technology, we recommend that you take a look at the topics below to savor C #--. net Remoting topic. In the next article, we will continue to share with you another distributed technology, Web Service.

Download the sample code file in this article:. NETRemotingSample

 


Do you not need to learn about the concept of net remoting?

WCF itself is a set of. NET Remoting, ASP. NET Web Service, MSMQ, and other technologies. Of course, it will be better if you have these foundations. If you do not have any suggestions, you can directly learn about WCF.

Which of the following is the high efficiency of webservice and WCF?

Comparing web Services and wcf, there are a lot of online services. It is better to connect the client to the server using wcf because it can be abstracted as soa and you can adopt the esb approach. This is what our mes product is like!
If it is a small project, you can directly connect to the database. However, this is not safe. All the database items are exposed, and web services cannot be used to push services, however, using wcf is different. The other two are similar. They are all about the soap protocol.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.