Implementing a Distributed Service


The MessagingFramework object binds two different application elements through messaging services. On the application side, the client can be a business service, a Windows form, or a Web form. On the distributed service side, the client is likely to be a Windows service that performs a specialized, and probably lengthy, operation.

The following sections implement an analytical service. This service might include functionality that is notified of incoming messages and that performs a lengthy analysis to help determine trends in the issues filed. Because this analytical process is likely to run a long time, it makes sense to offload that functionality to a separate process running on a separate server. For the purpose of this example, messaging is the form of communication. Remoting is another viable option, but for the sake of this example, assume that the incoming issues are mission critical and need to be persisted in case of hardware failure.

Adding the Project

The first step in implementing AnalysisService is to create a new Windows Service project and add it to the IssueTracker solution. Figure 4-6 illustrates the process of creating a new service within the Add New Project dialog box.

click to expand
Figure 4-6: Creating a transactional message queue

Implementing the Service

Once you have named the project and clicked OK, Visual Studio .NET creates the project and adds it to the solution. To access the messaging services, you need to add the references to the System.Messaging and the local SystemFrameworks namespaces. The source code also needs to include the appropriate using statements. Listing 4-18 outlines additional methods that you need to add to the service code.

Listing 4-18: AnalysisService Implementation
start example
 protected override void OnStart(string[] args) {     Thread listeningThread = new Thread( new ThreadStart(MessageListener) );     listeningThread.Start();     return; } public static void MessageListener() {     Issue objIssue = null;     MessagingFramework messagingServices = new MessagingFramework();     messagingServices.ProcessName = "AnalysisService";     messagingServices.CreateQueues();     while( true )     {         objIssue = (Issue)messagingServices.ReceiveBusinessObject();         //perform analysis on issue object         Thread.Sleep(1000);     }     return; } 
end example
 

In this code, you fill in the OnStart method to start a new thread of execution. This new thread will be responsible for executing the lengthy analytical processing. The processing itself is implemented in the MessageListener method. This method uses the MessagingFramework object to create the application message queues and continuously listen for new incoming messages. When a message is received, the analytical services go to work and perform their function. After processing, the thread is suspended briefly to yield processing time to other services.




Developing. NET Enterprise Applications
Developing .NET Enterprise Applications
ISBN: 1590590465
EAN: 2147483647
Year: 2005
Pages: 119

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net