1. Home
  2. Workflow & Automation
  3. Windows Services
  4. Creating the trellispark Generic Windows Service

Creating the trellispark Generic Windows Service

The trellispark generic windows service performs several functions. It processes queued commands, incoming and outgoing mail, and performs scheduled tasks. This article will explain the architecture for the trellispark Generic windows service and how to configure the service in trellispark. To learn how to create your own service, please refer to this article.

The trellispark generic windows service requires the trellispark Rest-Logic project, or the greatideaz.trellispark.Rest.Logic nuget package. The code in this package is required to connect to the database, extract the service data, and perform all service operations.

The next step is to create a worker class for the service. This class prepares the database context and provides the framework for timing the service. This is the most common pattern for trellispark windows services.

First, the class is initialized with a constructor that includes the configuration.

public Worker(IConfiguration configuration)
{
    Configuration = configuration;
}

Second, the class extracts the Application Insights instrumentation key and timer period from the appsettings.config.

 AppInsights.InstrumentationKey = configuration.GetValue<string>("AppInsightsKey");
int TimerPeriod = configuration.GetValue<int>("TimerPeriod");

As long as the class is running, the service will run it’s process based on the timer period. The period is given in milliseconds and trellispark’s default interval is 15 seconds (15000 milliseconds). For each run through, the service will reconnect to the database and create a new instance of whichever class contains the logic for the service. For the trellispark generic service, that class is the ServiceLogic class.

using (ServiceLogic MyLogic = new(myDB, configuration.GetValue<string>("LocalCoreAPI_URL")))
{
    await MyLogic.Process(configuration.GetValue<Guid>("ServiceID"), configuration.GetValue<Guid>("ServiceAccountID"));
}

The Process method in the ServiceLogic class provides a functional framework for connecting the service to the database and extracting the service definition.

(await MyDB.Connect(userData.SessionGUID, userData.UserGUID, "ServiceProcess")

ExecuteSQLInformation mySQL = new()
{
    SessionGUID = userData.SessionGUID,
    UserGUID = userData.UserGUID,
    CurrentInstanceGUID = serviceGUID,
    CommandName = "[dbo].[GI_ConnectServiceAccount]"
};
await MyDB.ExecuteTSQLValues(mySQL);

Next the service reads the service definition. It uses this in each of the additional steps to pull message queues, command queues, and scheduled tasks for processing.

Instance myService = new(MyDB);
InstanceInformation serviceData = new()
{
    SessionGUID = userData.SessionGUID,
    UserGUID = userData.UserGUID,
    InstanceGUID = serviceGUID,
    FormGeneration = false
};
await myService.ReadInstance(serviceData, true);

At this point, the service knows what operations it should perform. It just needs the code to perform those operations. The code is also in the ServiceLogic class with each operation in it’s own method. The operations currently included in the trellispark generic service as well as how to configure those operations will be in the following articles.

After the service has performed it’s operations, it should be disconnected for security and cleanliness reasons.

mySQL = new()
{
     SessionGUID = userData.SessionGUID,
     UserGUID = userData.UserGUID,
     CurrentInstanceGUID = serviceGUID,
     CommandName = "[dbo].[GI_DisconnectServiceAccount]"
 };

 await myDB.ExecuteTSQL(mySQL);

Configuration

To configure the service, open the workspace application and navigate to the “Services” tab. Click the “Add new Service” button on the childlist to create the new service definition.

To configure your service, provide it a human readable name and set the Service Type to “Windows Service”. Copy your Service ID to the ServiceID property in your AppSettings.config file.

Updated on March 6, 2024

Was this article helpful?

Related Articles

Need Support?
Can’t find the answer you’re looking for? Don’t worry we’re here to help!
Contact Support

Leave a Comment