.NET Remoting A communication framework across application domains or across networks

.NET Remoting is a cross-application domain or cross-network communication framework that enables client applications to access and invoke objects in server-side applications. .NET Remoting is based on the classes and interfaces provided by the .NET Framework’s System.Runtime.Remoting namespace and its subnamespaces. Before learning .NET Remoting, we need to understand some basic concepts and components, as follows:

  • Remote objects: Remote objects refer to objects running in the application domain on the server side, which can be accessed and invoked by client applications through proxy objects. Remote objects must derive from the MarshalByRefObject class so that they can pass references across application domain boundaries. Remote objects can be singletons, or create a new instance for each request.

  • Channel: The channel refers to the medium for transmitting messages between the client and the server. They are responsible for serializing and deserializing the messages and sending them to the destination address. .NET Remoting supports multiple types of channels, such as HTTP channel, TCP channel, IPC channel, etc., which use different protocols and formats to transmit messages. Channels can be registered and configured through configuration files or code.

  • Message: The message refers to the data unit exchanged between the client and the server, and they contain information such as the identity of the remote object, the method name to be called, and the parameters passed. Messages can be either synchronous or asynchronous. Messages can be represented in SOAP format or in binary format.

  • Formatters: Formatters are components responsible for converting messages into a specific format, and they work with channels to facilitate the transmission of messages across networks. .NET Remoting supports two types of formatters, SOAP formatters and binary formatters. The SOAP formatter uses XML to represent messages, which has the advantages of readability and cross-platform, but also the disadvantages of performance and size. Binary formatters use binary data to represent messages, which has performance and size advantages, but also readability and cross-platform disadvantages.

  • Proxy objects: Proxy objects refer to objects that represent remote objects in the client application. They are responsible for converting the client’s method calls on the remote objects into messages and sending them to the server through channels. There are two types of proxy objects: transparent proxy and real proxy. The transparent proxy is automatically generated by the CLR. It implements the interface implemented by the remote object and rewrites all methods so that the client can call the remote object as if it were a local object. The real proxy is customized by the developer. It inherits from the RealProxy class and rewrites the Invoke method so that the client can control the remote object more flexibly.

  • Receiver: Receivers refer to components that intercept messages and process them on the client or server side, and they can implement some additional functions, such as security, logging, caching, etc. There are two types of receivers: client receivers and server receivers. Client sinks are further divided into envoy sinks and context sinks, which are respectively located between the transparent proxy and the real proxy, and between the real proxy and the channel. The server-side sink is divided into service environment sink (context sink) and object

  • Receiver: Receivers refer to components that intercept messages and process them on the client or server side, and they can implement some additional functions, such as security, logging, caching, etc. There are two types of receivers: client receivers and server receivers. Client sinks are further divided into envoy sinks and context sinks, which are respectively located between the transparent proxy and the real proxy, and between the real proxy and the channel. The server-side sink is further divided into a service environment sink (context sink) and an object environment sink (object sink), which are respectively located between the channel and the remote object, and between the remote object and the method call.

  • Activator: Activators are components responsible for creating or obtaining remote objects on the server side. They can implement different activation strategies, such as singleton mode, factory mode, etc. There are two types of activators: client-side activators and server-side activators. A client-side activator is a proxy object used by a client application to create or obtain a remote object using the Activator class or the RemotingConfiguration class. Server-side activators are used by server-side applications to register or publish remote objects using the RemotingConfiguration class or configuration file.

How to use .NET Remoting:

  • Introduce necessary namespaces, such as System.Runtime.Remoting and System.Runtime.Remoting.Channels, on the server side and client side.

  • Create a remote object class on the server side, inherit from System.MarshalByRefObject, and define public methods and properties.

  • Create a channel object on the server side, specify the listening port and transport protocol (TCP or HTTP), and register it in the ChannelServices class.

  • On the server side, use the RegisterWellKnownServiceType method of the RemotingConfiguration class to register a remote object, specifying the object type, URI, and activation mode (single call or singleton).

  • Create a channel object on the client, specify the connection port and transport protocol (TCP or HTTP), and register it in the ChannelServices class.

  • On the client side, use the GetObject method of the Activator class to obtain the proxy of the remote object, and specify the type and URI of the object.

  • The methods and properties of the remote object are invoked on the client side through the proxy object.

//server-side code
 using System;
using System. Runtime. Remoting;
using System.Runtime.Remoting.Channels;
 using System.Runtime.Remoting.Channels.Tcp;
//Define the remote object class, inherited from MarshalByRefObject
public class RemoteObject : MarshalByRefObject { public RemoteObject() { }
public string SayHello()
{
    return "Hello, this is a remote object.";
}
}
//Define the server-side application class
class ServerApp { static void Main(string[] args) { //Create a TCP channel and listen to port 8085 TcpChannel channel = new TcpChannel(8085); //Register the channel to the ChannelServices class ChannelServices.RegisterChannel(channel); //Register Remote object, specify type, URI and activation mode (singleton) RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject), “RemoteHello”, WellKnownObjectMode.Singleton); //Output information on the console, waiting for the client to connect Console.WriteLine(“Server is running. Press ENTER to exit.”); Console. ReadLine(); } }
//client code
 using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp;
//Define the client application class
 class ClientApp { static void Main(string[] args) {
//Create a TCP channel and connect to port 8085 of the server
 TcpChannel channel = new TcpChannel(0);
//Register the channel to the ChannelServices class
 ChannelServices. RegisterChannel(channel);
/ / Get the proxy of the remote object, specify the type and URI
RemoteObject remoteObject = (RemoteObject) Activator. GetObject(typeof(RemoteObject), “tcp://localhost:8085/RemoteHello”);
 //Call the method of the remote object through the proxy object and output the return value
 Console.WriteLine(remoteObject.SayHello()); } }

Summary This is an introduction to the basic concepts and components of the .NET Remoting cross-application domain or cross-network communication framework, which can be used to understand how .NET Remoting works and how to use it.