What is remoting? In short, we can regard it as a distributed processing method. From the perspective of Microsoft products, it can be said that remoting is an upgrade of DCOM, which improves many functions and is well integrated into the. NET platform. Microsoft. NET remoting provides a framework that allows an object to interact with another object through an application domain. This is exactly why remoting is used. Why? In Windows, applications are separated into separate processes. This process forms a boundary around the application code and data. If the inter-process communication (RPC) mechanism is not used, the Code executed 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 and communicate with another application domain, that is, cross the border.
. NET provides two methods for remote calls: remoting and WebService. remoting is more suitable for small and medium LAN applications than WebService, but not for enterprise applications.
Remoting has the following features:
Advantages:
1. Fast TCP channel remoting
2. Although it is remote, It is very close to the local call object.
3. The object state can be maintained.
4. No application restrictions. It can be console, winform, IIS, or Windows Services that host remote objects.
Disadvantages:
1. There are platform restrictions because it is not a standard application
2. If you leave IIS, you need to have your own security mechanism.
A remoting demo is provided below:
Call object:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace RemoteObjects{ public class RemoteObject : System.MarshalByRefObject { public RemoteObject() { Console.WriteLine("New Referance Added!"); } public int GetRdm(int a, int b) { Random rdm = new Random(); try { return rdm.Next(a, b); } catch { return 0; } } }}
Server:
using System;using System.Runtime;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;using RemoteObjects;namespace RemoteServer{ class Program { static void Main(string[] args) { Console.WriteLine("Remoting server start..."); Console.Write("Regist Channel..."); TcpServerChannel channel = new TcpServerChannel(8201); ChannelServices.RegisterChannel(channel, true); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), "RemoteObject", WellKnownObjectMode.SingleCall); Console.WriteLine("OK"); Console.ReadKey(true); GC.Collect(); GC.WaitForPendingFinalizers(); } }}
Client:
using System;using System.Runtime;using System.Runtime.Remoting;using System.Runtime.Remoting.Channels;using System.Runtime.Remoting.Channels.Tcp;using RemoteObjects;namespace RemoteClient{ class Program { static void Main(string[] args) { ChannelServices.RegisterChannel(new TcpClientChannel(), true); RemoteObject remoteobj = (RemoteObject)Activator.GetObject(typeof(RemoteObject), "tcp://localhost:8201/RemoteObject"); Console.WriteLine("[1, 100) => " + remoteobj.GetRdm(1, 100).ToString()); Console.ReadLine(); Console.ReadKey(true); } }}