Click here to Skip to main content
15,887,449 members
Articles / WCF
Tip/Trick

A Batch process using RESTful WCF Service

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Jun 2013CPOL2 min read 11.9K   252   6  
This tip contains a solution for a Batch process which invokes a RESTful WCF Service.

Sample Image

Introduction

A Batch program takes a set of data files as input, processes the data, and produces a set of output data files. The input data is usually a collection of .txt or .xml, a typical batch program monitors a directory, starts processing on ‘arrival’ of the files, and generates the output or rejected files. There are situations when the Batch process has to make web service calls to third party applications for checking the incoming input files. Consider a telecommunication provider of local, national, or international calls. A provider like AT&T often uses the equipment, wiring, and transmission of other carriers like Verizon, Sprint, and SBC to provide service to the customer. When a new number is provisioned, the provider has to check with other carriers for preparing and equipping a network to allow it to provide (new) services to its users. This process requires invoking web service calls.

Background

In this article, the batch program is processing the area code. The name of the input file indicates an area code, i.e., 101.txt represents the area code 101. The application makes a web service call if this area code is valid for the network. If it is not it rejects the file with a rejection message, “Not served by partner carrier”. The batch process uses the FileSystemWatcher object. The main console batch process spawns a new thread and the FileSystemWatcher monitors the source folder for input files.

Using the code

If there are new files in the folder, it starts processing. Else it waits for the file creation event. If there are incoming flies, the event is fired and the batch process is signaled that it can start processing again. The batched query requests are sent to the WCF service which is translated into REST-based operations. The web method returns a Response object which represents responses to individual queries in the batch. Based on the response, the files are either rejected or are processed.

C#
string[] lstr_arryFilesInDirectory = Directory.GetFiles(_strSourceDir);
// Begin watching.
if (lstr_arryFilesInDirectory.Length > 0)
{
    watcher.EnableRaisingEvents = false;
    ProcessFilesFound(lstr_arryFilesInDirectory);
}
else
{
    watcher.EnableRaisingEvents = true;
    _objSignal.WaitOne();
}

The article download contains two components: BatchProcess and WcfValidationService. You will need to create the Source, Target, and Reject folder (the location is hardcoded in the config file). There are also sample input files which are available in the source folder of the BatchProcess project. To run the application, start the WcfValidationService and then the console based BatchProcess. After that copy the input .txt files to the source folder. A successful operation will move these .txt files to the Target and Reject folders, respectively.

Points of Interest

The application uses LINQ and the Repository pattern in the web service.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I am passionate on coding and application development specially in the .NET C# windows and web platform.

Comments and Discussions

 
-- There are no messages in this forum --