Create a distributed application architecture based on Microsoft .NET Remoting

xiaoxiao2021-03-06  124

Microsoft.com Home | Site Map

Search in Microsoft.com:

Community Home | Community Heroes | Chinese News Group | Community Integral System | Microsoft Headquarters

Community Home

Microsoft Technology Community

Chinese news group

Community integral system

Community hero

MVP technology column

Microsoft expert special lecture

Community original article

Bo Hall

Community partner

More resources

User site

IT Professional Community

Developer community

Product technology community

Windows

Office

development tools

server

MOBILE

Create a distributed application architecture based on Microsoft .NET Remoting

Summary

This article describes how to use .NET Remoting technology to build a typical distributed multipoint broadcast application architecture. Requires readers have some basic concepts of .NET Remoting technology, if you don't understand this, please refer to: Microsoft .NET Remoting Technical Overview.

table of Contents

Introduction Network Physical Structure Diagram Architecture Design Summary Reference Reference Author

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

{

/ / Define the parameter class for the 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 BroadcastHandrandler (Object Sender, BroadcasteventArgs Submitargs);

Public Class InfoCenter: MarshalbyrefObject

{

Public infocenter ()

{

Console.writeLine ("Infocenter Created.");

}

Public override Object InitializelifetimeService ()

{

Return NULL;

}

Public Event BroadcastEventHandler Broadcast;

Public void Broadcasting (String Message)

{

BroadcastEventArgs E = New BroadcastEventArgs (MESSAGE);

IF (Broadcaster! = NULL)

{

Broadcast (this, e); // issued an event

Console.Writeline ("Broadcast: E.MESSAGE);

}

}

}

}

Important:

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):

Wellknown

Mode = "singleton"

TYPE = "Distribution_framework.infocenter, InfoCenter"

Objecturi = "Broadcast"

/>

Ref = "http"

Port = "8080"

/>

The TV broadcast station has been set up, let's take a look at how "TV" is doing (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 ();

// Subscribe to information

InfoCenter.Broadcaster = New BroadcastHandler (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 receive information

}

Public static void main ()

{

Receiver 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, BroadcasteventArgs Args)

{

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):

Wellknown

TYPE = "Distribution_framework.infocenter, InfoCenter"

URL = "http:// localhost: 8080 / bigcast"

/>

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 announce = new announce ();

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):

Wellknown

TYPE = "Distribution_framework.infocenter, InfoCenter"

URL = "http:// localhost: 8080 / bigcast"

/>

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 #'s delegate to improve the OBServer mode. references

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

Author

Lu Yan

November 1, 2002

Copyright Notice

Another article published on this website must guarantee a original article; if it is not a original article, the copyright dispute brings to the author assumes himself. Microsoft is obliged to review the article published on this site, but you need to consider the risk of use of these articles. This site only provides a user exchange technology information and learning gardens, and does not take any risks for technical articles from community users.

Personal Information Center |

Contact us |

Subscribe to Microsoft Community Mail

© 2004 Microsoft Corporation. All rights reserved.

Keep all rights |

Trademark |

Privacy statement