NET Remoting Example

Source: Internet
Author: User

. NET Remoting is the infrastructure on the. NET platform that allows objects that exist in different application domains to know each other and communicate with each other. The calling object is called the client, and the called object is called the server or server object. In short, it is. NET platform to implement the framework of distributed object System. As long as the access across the AppDomain, all belong to remoting. Remoting Programming Fundamentals: When a client creates an instance of a remote Remotableclass,. NET Framework generates a proxy in the client application domain. The proxy looks just like the actual object. After the agent receives the call, it connects to the remote object through the channel.

The remoting transmits messages over channels (channel). There are two main types of remoting channels: TCP and HTTP. In. NET, the IChannel interface is defined in System.Runtime.Remoting.Channel. The IChannel interface includes the TcpChannel channel type and the HTTP channel type. They correspond to each of these two types of remoting channels.

The TcpChannel type is placed in the namespace SYSTEM.RUNTIME.REMOTING.CHANNEL.TCP. The TCP channel provides a socket-based transport tool that uses the TCP protocol to transmit serialized message flows across the remoting boundary. The TcpChannel type uses binary format to serialize the message object by default, so it has higher transmission performance. The HttpChannel type is placed in the namespace System.Runtime.Remoting.Channel.Http. It provides a way to use the HTTP protocol to transmit a serialized message flow over the Internet through a firewall . By default, the HttpChannel type serializes the message object using SOAP format, so it has better interoperability. Usually we use TcpChannel in the LAN, and HttpChannel if we are going through the firewall.

I wrote an example myself, with the following engineering structure:

1, create a class library to create the interfaces we need

namespace remotinginterface{    interfacestring Greeting (string sName);}}    

2, create an implementation of the interface, you need to add a reference to the above project

 namespace   remotingimp{ public  class   Myclass:marshalbyrefobject, MyInterface {  #region  MyInterface members public  string   Greeting (string   SName) { return   hello!          + SName;   #endregion  }}   

3, server-side implementation, need to add the above two references

namespaceremotingsever{classProgram {Static voidMain (string[] args) {TcpChannel Channel=NewTcpChannel (8080); ChannelServices.RegisterChannel (channel,false); RemotingConfiguration.RegisterWellKnownServiceType (typeof(Remotingimp.myclass),"Remotingpersonservice", WellKnownObjectMode.SingleCall); System.Console.WriteLine ("server:press Enter key to exit");            System.Console.ReadLine (); }    }}

4, client, need to add the class library where the interface resides

namespaceremotingclient{classProgram {Static voidMain (string[] args) {TcpChannel Channel=NewTcpChannel (); ChannelServices.RegisterChannel (channel,false); MyInterface obj= (MyInterface) Activator.GetObject (typeof(Remotinginterface.myinterface),"Tcp://localhost:8080/remotingpersonservice"); if(obj = =NULL) {Console.WriteLine ("couldn ' t crate Remoting Object."); } Console.WriteLine ("Please enter your name:"); String name=Console.ReadLine (); Try{Console.WriteLine (obj.            Greeting (name)); }            Catch(System.Net.Sockets.SocketException e) {Console.WriteLine (e.tostring ());        } console.readline (); }    }}

NET Remoting Example

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.