Sample background service

Background services in SOA are continuously running services, which may or may not expose some service-callable interface. A background service is usually considered as a technical category of service, and is usually categorized depending on the function of a service. Our sample service could be categorized as an interaction or a data access service, which is a background running service acting as a DB Monitor, which periodically checks the database for a document's validity. Upon detecting the expiry documents, it calls another service to notify the change. We will see the sample code of the notification service (DocumentValidity) called by the background service in the next session.

This kind of service needs to be hosted in a process depending on the operating system. If it's Windows, the service can be a console app, an app without UI scheduled by Windows schedule, or an always running Windows service. If it's Linux, it would probably be configured as a service, or as an executable in Init.d or as a cron job. The skeleton code of our background service, which is hosted in a .NET Core console app, would look like the following piece of code:

    public class DBMonitor : IDisposable 
{
private SOAContext soaContext;
public DBMonitor()
{
soaContext = new SOAContext();
}

public void Initialize()
{
//Does the initialization, configuration, schedules,
database, check ups..
soaContext.Initialize();
}

public void Work()
{
//Perform all the mandated tasks as per schedule
//Periodically watch DB tables for documents validity
//Detect the change
//Notify the change in respective document that it is
expiring soon -- calls DocumentValidity notification service
}

//IDisposable Interface
}

The simple code in the host process would be something like this:

    public static void Main(string[] args) 
{
var monitor = new DBMonitor();
monitor.Initialize();
monitor.Work();
}
Note that we can use the same code and host it, for example, in the windows service as it is.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.107.89