.NET Remoting study notes (1) concept

Original: .NET Remoting study notes (1) concept

content

background

I have heard the term Remoting since I got into programming, but I know very little about it. I have some time recently, so I can refer to the research.

The related concepts are not explained in detail in this chapter. For details, you can  read http://baike.baidu.com/view/742675.htm?fr=aladdin   , which is written in great detail.

 

.Net Remoting Concepts

Concept: A way of distributed processing. From the point of view of Microsoft's products, it can be said that Remoting is an upgrade of DCOM  (Distributed Component Object Pattern), which improves many functions and integrates well into the .Net platform.

benefit:

1. Provides a framework that allows an object to interact with another object through an application domain.

In the Windows operating system, applications are separated into separate processes. This process forms a boundary around application code and data. Without the use of inter-process communication (RPC) mechanisms, code executing in one process cannot access another process. This is an operating system's protection mechanism for applications. However, in some cases, we need to cross the application domain, to communicate with another application domain, that is, to cross the boundary.

2. The server object can be published as a service:

The code can run on the server (such as server-activated objects and client-activated objects), and then the client connects to the server through Remoting, obtains the service object, and runs it on the client through serialization.

3. Loose coupling of client-side and server-side related objects

In Remoting, for the object to be passed, the designer does not need to know the format of the data packet except the type and port number of the channel. This not only ensures the loose coupling of related objects on the client and server sides, but also optimizes the performance of communication.

 

.NET Remoting supports channels and protocols

There are two main channels of Remoting: Tcp and Http, IChannel includes TcpChannel, HttpChannel

TcpChannel : The Tcp channel provides a Socket-based transmission tool that uses the Tcp protocol to transmit serialized message streams across Remoting boundaries. By default, the message object is serialized in binary format, which has higher transmission performance. Applicable to local area network.

HttpChannel : It provides a way to use the Http protocol to transmit serialized message streams across firewalls on the Internet . The HttpChannel type uses the Soap format to serialize message objects, so it has better interoperability. Apply to the World Wide Web. 

 

Difference from WCF and WebService

It 's better written here: http://kb.cnblogs.com/page/50681/

  • Remoting can flexibly define the protocols it is based on, such as http, tcp, etc. If it is defined as HTTP, it is the same as Web Service, but webservice is stateless, and remoting is generally defined as TCP, which is slightly more efficient than Web Service Some, and stateful. 
  • Remoting is not a standard, and Web Service is a standard. 
  • Remoting generally needs to be started through a WinForm or Windows service, or can be deployed using IIS, while Web Service must be started in IIS. 
  • In the VS.net development environment, the invocation of Web Service is specially encapsulated, which is more convenient to use than Remoting. 
  • Net remoting can only be applied to MS's .net framework, and the client must install the framework, but WebService is platform-independent, cross-language (as long as it can support XML) and penetrates corporate firewalls.

 

.NET Remoting activation method

Simple understanding: We know that we need remote processing objects in our Remoting application, so how are these objects created? Who created it? …and the activation method is meant to address these questions.

Remote object activation is divided into two categories: server-side activation (WellKnow) and client-side activation.

There are two modes of server-side activation: SingleTon mode and SingleCall.
 

Implement the Remoting step

1. Create the type of remote processing ( because the object passed by Remoting is by reference, the passed remote object class must inherit MarshalByRefObject. )

2. Create a server

3. Create a client

MarshalByRefObject

MarshalByRefObject is the base class for objects that communicate across application domain boundaries by exchanging messages using brokers.

Objects that do not inherit from MarshalByRefObject are implicitly marshaled by value.

When a remote application references a marshaled object by value, a copy of the object is passed across remoting boundaries.

Because you want to communicate using proxy methods instead of copy methods, you need to subclass MarshallByRefObject.
 
The remote object that can be passed in Remoting can be of various types , including complex DataSet objects, as long as it can be serialized . Remote objects can also contain events, but the server-side handling of events is special.

 

a simple case

1. Write the remoting class

using System;
using System.Runtime.Remoting.Metadata;

/* code Sakyamuni */ 
namespace MessageMarshal
{
    /* Create a send message delegate */ 
    public  delegate  void SendMessageHandler( string messge);
     public  class TestMessageMarshal : MarshalByRefObject
    {
        /* Create send message event */ 
        public  static  event SendMessageHandler SendMessageEvent;

        /* Send message */

        [SoapMethod(XmlNamespace = "MessageMarshal", SoapAction = "MessageMarshal#SendMessage")] 
        public void SendMessage(string messge)
        {
            if (SendMessageEvent != null)
                SendMessageEvent(messge);
        }
    }
}

2. Create a server

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

namespace TestRemotingServer
{
    /* code: Sakyamuni */ 
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine( " Create HTTP channel " );
             /* Create HTTP channel */ 
            HttpChannel channel = new HttpChannel( 816 );
             /* Register the channel server */ 
            ChannelServices.RegisterChannel(channel, false );
             /* Server registration, Activate with Singletong */ 
            RemotingConfiguration.RegisterWellKnownServiceType( typeof (MessageMarshal.TestMessageMarshal), " TestMessageMarshal " , WellKnownObjectMode.Singleton);

            /* Receive client events */ 
            MessageMarshal.TestMessageMarshal.SendMessageEvent += new MessageMarshal.SendMessageHandler(TestMessageMarshal_SendMessageEvent);

            Console.Read();
        }
        static void TestMessageMarshal_SendMessageEvent(string messge)
        {
            Console.WriteLine(messge);
        }
    }
}

3. Create a client

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Threading;

/* code Sakyamuni */ 
namespace TestRemotingClient
{
    class Program
    {
        static void Main(string[] args)
        {
            /* Create a channel */ 
            HttpChannel channel = new HttpChannel();
             /* Register the channel */ 
            ChannelServices.RegisterChannel(channel, false ); 
             /* Register the remoting type of the channel */ 
            RemotingConfiguration.RegisterWellKnownClientType( typeof (MessageMarshal.TestMessageMarshal), " http://localhost:816/test " );  
             /* Create 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

 

Write this for the time being, if you have any questions, please correct me! Continue to update later

 

Author: Sakyamuni Sangha Source: http://www.cnblogs.com/woxpp/p/3992771.html  The copyright of this article belongs to the author and the blog garden. Reprints are welcome, but this statement must be retained without the author's consent, and the The link to the original text is given in an obvious position on the article page.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325301926&siteId=291194637