Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Business Logic Server as Windows Service (Architectural Prototype)

5.00/5 (4 votes)
23 Jan 2016MIT4 min read 14K   161  
An architectural prototype of a business logic server as Windows Service. Sample service implementation and test utility.

 

Introduction

There is a class of tasks for which it is preferable to have resident in-memory storage. This is often solved via in-process caching but there is another problem: data synchronization in multiple processes (in web clusters or client-server solutions for example).

Examples of such problems:

  • A customizable content of web pages, especially in a case of a tree-based data structure
  • Complex algorithmic tasks with large data amount. Especially when the algorithm is unable to implement efficiently through DBMS operations
  • High loaded subsystems (by simultaneous requests): Data change notifications, Authorization Service

The project implements Authorization Management service as a sample. See CodeProject "Overloaded Server: Quick Fix" article for a description of the subject area and related issues.

This project is a sample of implementation of business logic in a Windows Service application (and a console application for debugging). The approach allows to implement data storage and processing in-memory, build appropriate synchronization root, implement optimal handling of DBMS operations.

Objectives of the project are listed below:

  • Creating a skeleton of a solution to have the ability in future to easily implement required business logic
  • Investigate high-performance approaches of cluster/intranet interprocess communication
  • Supply high-level communication interface with the ability to transport arbitrary object structures (EF Entities POCO here)

Out of scope of the project:

  • Fault tolerance and scalability
  • Authentification, logging
  • DB operations optimization

Background

In the development of this project, and many other tasks is an issue of serialization. I had a deal with standard .NET, NetSerializer, ProtoBuf serializers but still wanted to find an another solution. So I found NFX.Slim. It turned out that it is productive and at the same time, sufficiently universal solution. Moreover, NFX (Server UNISTACK framework) has also optimized replacement of WCF. It names NFX.Glue. I have a very high opinion about current Microsoft frameworks, but it is universal solutions and there is a possibility to build more optimal solution for particular cases. So I chose NFX now, but the project can be converted to WCF/Data Contract Serializer if needed.

Using the Code

The code is structured and commented so look at the source for details. Following are solution projects description and some notes only:

  • README.md - Command line guide
  • AuthListTest - Command line test utility; test*.batch - test series batches
  • AuthService - Console/service dual mode application; service_dbg.batch - service register and run batch
  • AuthService.Client - Service client library; AuthService.Client.laconf - NFX client config file. Usually, code of service client class is autogenerated. But AuthServiceClient was entered manually because gluec utility has some limitations now (related to classes placement which are referenced by contract)
  • AuthService.Contracts - Service contract library. IAuthService (service contract) is the same as ISessionManager (logical service interface). Both are included because the project was developed as refactoring of previous single-process solution with full support of previous functionality for testing.
  • AuthService.Server - Service implementation library; AuthService.Server.laconf - NFX server config file. SessionDataManager - BL implementation; based on a table in memory. DB access via StorageManager - queue of DB requests (as delegates) with a single asynchronous execution thread.
  • SessionData - Data Access Layer library (EF6) from "Overloaded Server: Quick Fix" article.

Testing

Execution time of 500 authorization operations, depending on the number of concurrent threads of execution

BL Service performance tests

  • BLUE (C) - in-process cache approach from "Overloaded Server: Quick Fix".
    Limit: thread count >= 200. Exceeding default database operation time-out.
  • ORANGE (S) - Windows Service approach. Deviations are caused by background process execution (queue of database operations that migrates from previous test run).
    Limit: thread count >= 500. Seems to exceed tcp.socket backlog default value (the maximum length of the pending connections queue).

IMHO, very good Windows Service result, given the additional costs of IPC.

Points of Interest

During the work, I had an impression that the NFX works and works well. There are also a lot of other NFX subsystems. For a subject area of the article, the following may be useful: NFX.BigMemory (large memory heap), NFX.Glue.MPX (multiplexing TCP channel binding).

It was also interesting to build asynchronous database update using a queue of delegates:

C#
// put request: session record create
StorageManager.UpdateAsync((dbx) =>
{
    dbx.Session.Add(sess);
}, "Create Session");
//put request: get created session with user and roles
// (waiting for create because it in front of the requests queue)
sess = StorageManager.Get<Session>((dbx) =>
{
    return dbx.Session
                .Include(s => s.User.Role)
                .Where(s => s.Id == sqlite_guid).First();
}, "Get New Session");

It seems the controller may be useful in other projects as well.

History

  • Jan-2016: v1.1.1.0 Initial release

License

This article, along with any associated source code and files, is licensed under The MIT License