Brief introduction
Microsoft recently unveiled a new platform for building integrated applications--microsoft. NET Framework allows developers to quickly build and deploy WEB services and applications using any programming language. The Microsoft Intermediate Language (MSIL) and Just-in-time (JIT) compilers enable this language-independent framework to be implemented.
There is also a new programming language C # (read "C sharp") that is available at the same time as the. NET Framework. C # is a simple, novel, object-oriented, and type-safe programming language. Leverage the. NET Framework and C # (except Microsoft?) Visual Basic? and Managed C + +), can users write powerful Microsoft Windows? and WEB applications and services. This article provides a solution that focuses on the. NET framework and C # rather than the programming language. A description of the C # language can be found in "Introduction and Overview of C #" (English).
Recent article "MSMQ: Scalable, high-availability load-balancing Solutions" describes a solution for high availability Message Queuing (MSMQ) scalable load-balancing solution architecture. This solution involves a development scenario that uses the Windows service as a smart messaging router. Such a solution was previously only Microsoft Visual C + +? Can be implemented by programmers, and the. NET framework has changed that. You can see this from the solution below.
. NET Framework Applications
The solution described here is a Windows service that handles several message queues, each of which is handled by multiple threads (receiving and processing messages). Handlers use the looping technique or application-specific values (message AppSpecific properties) to route messages from the destination queue list and use message properties to invoke the component method. (This is also the case with the sample process.) In the latter case, the requirement for the component is that it can implement the given interface IWebMessage. To handle errors, the application needs to send messages that cannot be processed to the error queue.
The structure of the messaging application is similar to that of the previous Active Template Library (ATL) applications, where the main difference is in the encapsulation of the code used to manage the service and the use of the. NET Framework components. To create a Windows service, the. NET framework user simply needs to create a class that inherits from the ServiceBase (from the System.servicecontrol assembly). This is no surprise, because the. NET framework is object-oriented.
Application Architecture
The main class in the application is ServiceControl, which is inherited from ServiceBase. Thus, it must implement the OnStart and OnStop methods, as well as the optional OnPause and OnContinue methods. In fact, classes are constructed in static method Main:
using System;
using System.ServiceProcess;
public class ServiceControl: ServiceBase
{
// 创建服务对象的主入口点
public static void Main()
{
ServiceBase.Run(new ServiceControl());
}
// 定义服务参数的构造对象
public ServiceControl()
{
CanPauseAndContinue = true;
ServiceName = "MSDNMessageService";
AutoLog = false;
}
protected override void OnStart(string[] args) {...}
protected override void OnStop() {...}
protected override void OnPause() {...}
protected override void OnContinue() {...}
}
The ServiceControl class creates a series of CWorker objects, that is, an instance of the CWorker class is created for each message queue that needs to be processed. The CWorker class creates a series of CWorkerThread objects in turn, depending on the number of threads required to process the queue in the definition. A processing thread created by the CWorkerThread class will perform the actual service work.
The primary purpose of using the CWorker and CWorkerThread classes is to confirm the service control Start, Stop, Pause, and Continue commands. Because these processes must be non-blocking, the command operation will eventually execute on the background processing thread.
CWorkerThread is an abstract class that is inherited by Cworkerthreadappspecific, Cworkerthreadroundrobin, and cworkerthreadassembly. These classes process messages in different ways. The first two classes handle messages by sending a message to another queue, which differs in determining how the queue path is received, and the last class uses message properties to invoke the component method.
Error handling within the. NET Framework is based on the base class Exception. When an error is raised or caught by the system, these errors must be classes derived from Exception. The Cworkerthreadexception class is an implementation that extends the base class by attaching additional attributes that define whether the service should continue to run.
Finally, the application consists of two structures. These value types define Run-time parameters for worker processes or threads to simplify the structure of CWorker and CWorkerThread objects. The use of value type structures, rather than reference type classes, ensures that these run-time parameters maintain values (rather than references).