Create a distributed application architecture based on Microsoft .NET Remoting

zhaozj2021-02-16  90

Lu Yan

introduction

I often see someone in the forum: How do I use .NET Remoting how to send an event to the client from the server? Indeed, there will be such questions in the initial contact .Net Remoting, because most of the articles and books are introduced to .NET Remoting, only the concepts, objects, activation, and survival cycles, etc., talking about how to do When remote communication, only tell the reader how to activate a server object from the client and pass the parameter to call its method. So many readers don't know how to broadcast information from the server, and even many people think that the web server cannot actively send information to the browser. -> Server Reply Response "This simple communication mode, so that both ends of the server and Client objects are designed to place both ends of Server and Client objects when the server broadcast information is required. It is timely trouble.

In fact, .NET Remoting remote processing fully supports event-driven programming and uses synchronous and asynchronous callback functions. In .NET Remoting, you can easily adopt event registration remote callback functions, and easily use this mechanism to broadcast server-side information to the client. The following will be described in detail to the reader to describe the writing method of this distributed multipoint broadcast application architecture.

Network physical structure diagram

The above is a schematic diagram of the network structure, which is a typical distributed application architecture because this network structure is very common, and can be used in many ways such as securities trading, futures market, video conferencing, and distance teaching.

Architecture design

For the conceptualization of abstract concepts, let's first understand the workflow of TV broadcasts, and then combine our program to understand the main ideas of architecture design.

Television broadcasting process is involved in the following four main agencies:

1. The program production department. Responsible for making TV programs.

2. Broadcast. Responsible for arranging the broadcast mode of the program provided by the program production department.

3. TV tower. Responsible for converting the TV signal to radio waves to send out.

4. TV. Responsible for accepting radio signals and converts to a visible image.

In our program, it is also composed of four main objects, and their names and uses are:

1. Announce: Information Send Objects. Responsible for sending raw information, equivalent to the TV program production department.

2. Infocenter: Information Center. Responsible for managing information broadcast mechanisms. Equivalent to the broadcast room.

3. Server: Server. Manage delivery channels to send broadcast data streams. Equivalent to the TV tower.

4. Receiver: Acceptor. Accept the broadcast data stream to convert the information format we can understand. Equivalent to the TV.

The object structure is shown below:

programming

First let's take a look at the writing method of the information center infocenter object (Infocenter.cs):

using System; using System.Runtime.Remoting; namespace Distribution_Framework {// class parameter defines broadcast event [Serializable] public class BroadcastEventArgs: EventArgs {private string msg = null; public BroadcastEventArgs (string message) {msg = message;} public string Message {get {return msg;}}} public delegate void BroadcastEventHandler (object sender, BroadcastEventArgs submitArgs); public class InfoCenter: MarshalByRefObject {public InfoCenter () {Console.WriteLine ( "InfoCenter created.");} public override object InitializeLifetimeService ( ) {return null;} public event BroadcastEventHandler Broadcaster; public void Broadcasting (string message) {BroadcastEventArgs e = new BroadcastEventArgs (message); if (Broadcaster = null) {Broadcaster (this, e);! // emit events Console.WriteLine ("Broadcast:" E.Message);}}}}

Public Delegate Void BroadcastHandrandler (Object Sender, BroadcasteventArgs Submitargs);

Define an event handle and its parameter format.

Public Event BroadcastEventHandler Broadcast;

Note that this sentence defines a public event and requires a remote object that accepts broadcast information can be registered with the INFOCENTEVENTHANDLER in accordance with this event. This mechanism is a bit similar to a wired charge TV. If you need to watch TV shows, please register first.

Next, look at the implementation of the Server object (Server.cs):

using System; using System.Runtime.Remoting; namespace Distribution_Framework {class Server {public static void Main (string [] Args) {RemotingConfiguration.Configure ( "Server.exe.config"); Console.WriteLine ( "Server is running, Press ENTER key to exit. "); Console.readline ();}}}

Oh, is it as simple as the TV tower? Basically, there is nothing else except "antenna". :)

Here is its configuration file (server.exe.config):

TV broadcast station has been set up, let's take a look at "How to do" TV " (Receiver.cs):

using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; namespace Distribution_Framework {class Receiver: MarshalByRefObject {InfoCenter infoCenter; public Receiver () {} public override object InitializeLifetimeService () {return null;} public void Run () {RemotingConfiguration.Configure ( "Receiver.exe.config"); infoCenter = new InfoCenter (); // subscription information infoCenter.Broadcaster = new BroadcastEventHandler (this.BroadcastReceiver) ; Console.WriteLine ( "Ready to Recieve Message ..."); Console.ReadLine (); // unsubscribe infoCenter.Broadcaster - = new BroadcastEventHandler (this.BroadcastReceiver);} public void BroadcastReceiver (object sender, BroadcastEventArgs args) {Console.Writeline ("received:" args.Message); // Print receiving information} public static void main () {received receiver = new receiver (); recEiver.run ();}}}

Important:

InfoCenter.Broadcaster = New BroadcastHandler (this.broadcastReceiver);

Remember the BroadCaster event we defined in InfoCenter? Here is how to register a remote callback function to it, which is equivalent to the registration to watch TV shows. If you don't want to see it, you will remember to register it, infocenter.broadcaster - = New BroadcastRecaster (this.BroadcastReceiver); otherwise, you will have more money. :) Public void BroadcastReceiver (Object Sender, Broadcast "{console.writeline (" received: " args.message);

This is a function remotely called by the server.

It is also important to note that this customer object must also inherit from MarshalByrefObject, because it is very simple because it is also a remote object that needs to be serialized relative to the server.

Here is its configuration file (Receiver.exe.config):

< Channel Ref = "http" port = "0" />

Note that port = "0" means automatically selecting a most suitable port by a computer.

We now have a TV set, and someone will send the show to play, the following is the code of the "TV program production department":

using System; using System.Timers; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; namespace Distribution_Framework {class Announcer {InfoCenter infoCenter; public static void Main (string [] Args) {Announcer announcer = new Announcer (); announcer.Run (); Console.WriteLine ( "The announcer has been started."); Console.ReadLine ();} public void Run () {RemotingConfiguration.Configure ( "Announcer.exe.config"); infoCenter = new InfoCenter (); Timer timer = new Timer (1000); timer.Elapsed = new System.Timers.ElapsedEventHandler (this.timer_Elapsed); timer.Enabled = true;} private void timer_Elapsed (object sender, System.Timers.ElapsedEventArgs e) {string msg = "The Time is:" DateTime.Now.ToString (); Console.WriteLine ( "Send Message:" msg); infoCenter.Broadcasting (msg Here, we define a timer, send a computer local time string information to the server every other second, and we finally have a program that can be received, although it is simple. Also, please note that we can receive a lot of TV channels, the sender does not specify that there is only one, in fact, we can add many senders, but pay attention to the synchronization problem between threads.

Here is its configuration file (announcer.exe.config):

< Channel Ref = "http" port = "0" />

The readers of the eye show, this is the same as the Receiver's configuration file, in fact, they are all the client, the configuration file is of course the same.

Finally, we compile them into executables with the following commands:

CSC / T: library infocenter.cs csc /r:infocenter.dll server.cs csc /r: Infocenter.dll receiver.cs csc /r :infocenter.dll Announcer.cs

Please run Server.exe first, then run announcer.exe, and finally you can open several Receiver to observe their work. Note that if you need to demonstrate this program to others on a different computer, change localhost in the configuration file into the corresponding computer name or an IP address without recompilation.

to sum up

Using .NET programming is always so simple and fun. In the above example, you can't see anything about "remote". It is very close to ordinary program calls, in fact, if there is no RemotingConfigure.configure ( Call, you don't need to make any modifications, you can compile and run the entire application in a single application domain.

This article focuses on design patterns based on .NET Remoting distributed remote application architecture, part of its design idea comes from Observer mode in Design Mode, if you use events and delegates to implement OBServer mode concept ambiguous Please refer to 文:

Use C # delegate to improve the OBServer mode.

references

MSDN-.NET Framework Developer Guide: .NET Remote Processing

转载请注明原文地址:https://www.9cbs.com/read-14089.html

New Post(0)