SaaS CMS has officially launched! Learn more now.

Bien Le
Bien Le  -  DXP
Aug 31, 2020
  4472
(1 votes)

Adding Web Job to an existing CMS Web App

This article will show you how to add a WebJob to an existing CMS web app project. The WebJob will access the CMS database and print out the last 15 activity log entries.

First, add a new project to the solution which has the existing CMS web app and choose Azure WebJob template.

Once the project is created, replace the Function.cs file with the content below:
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Threading.Tasks;
 
namespace GetLatestActivityLogs
{
    public class Functions
    {
        public static async Task GetLatestActivityLogs(TextWriter log)
        {
            log.WriteLine($"+++ Getting the latest activity logs +++");
 
            using (var dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["EPiServerDB"].ConnectionString))
            {
                await dbConnection.OpenAsync().ConfigureAwait(false);
                var selectCommandText = "select top 15 AL.LogData, AL.ChangeDate, AL.ChangedBy from tblActivityLog as AL order by AL.ChangeDate desc";
                using (var command = new SqlCommand(selectCommandText, dbConnection))
                {
                    using (var reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection).ConfigureAwait(false))
                    {
                        while (reader.Read())
                        {
                            log.WriteLine($"Data :{reader["LogData"]} - Changed on: {reader["ChangeDate"]} - Changed by: {reader["ChangedBy"]}");
                        }
                    }
                }
            }
        }
    }
}
And replace the Program.cs file with this content:
using System;
using System.Threading.Tasks;
 
namespace GetLatestActivityLogs
{
    // To learn more about Microsoft Azure WebJobs SDK, please see https://go.microsoft.com/fwlink/?LinkID=320976
    class Program
    {
        // Please set the following connection strings in app.config for this WebJob to run:
        // AzureWebJobsDashboard and AzureWebJobsStorage
        static void Main()
        {
            Task.Run(async () =>
            {
                await Functions.GetLatestActivityLogs(Console.Out).ConfigureAwait(false);
            }).GetAwaiter().GetResult();
        }
    }
}

Then add database connection string to the App.config file

<add name="EPiServerDB"
         connectionString="Server=tcp:{dxp_environment_name}.database.windows.net,1433;Initial Catalog=epicms;Persist Security Info=False;User ID={db_user_id};Password={db_password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
         providerName="System.Data.SqlClient" />

The WebJob project is now ready to add to be deloyed along with the Web App, to do that let's add the project as Azure WebJob to the WebApp

imagebkyx.png

A file named webjobs-list.json added to the web app project

{
  "$schema": "http://schemastore.org/schemas/json/webjobs-list.json",
  "WebJobs": [
    {
      "filePath": "../GetLatestActivityLogs/GetLatestActivityLogs.csproj"
    }
  ]
}

In which the filePath points to path of the WebJob project. And a file added to the project itself, webjob-publish-settings.json, in which defines WebJob name and run mode.

{
  "$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
  "webJobName": "GetLatestActivityLogs",
  "runMode": "Continuous"
}

The solution is now ready publish to DXP environment. Select the web app project and choose publish, and then choose [Azure App Service (Windows)] to deploy the site to:

Uncheck the option [Exclude files from the App_Data folder], and then publish the web app as usual.

Once publish has finished, go to App Service Editor tool in Azure Portal. You can see the Web Jobs and its dependencies are in [app_data] folder.

Run the WebJob and you can check Logs to see the result, it should look like this:

Aug 31, 2020

Comments

Please login to comment.
Latest blogs
Optimizely SaaS CMS Concepts and Terminologies

Whether you're a new user of Optimizely CMS or a veteran who have been through the evolution of it, the SaaS CMS is bringing some new concepts and...

Patrick Lam | Jul 15, 2024

How to have a link plugin with extra link id attribute in TinyMce

Introduce Optimizely CMS Editing is using TinyMce for editing rich-text content. We need to use this control a lot in CMS site for kind of WYSWYG...

Binh Nguyen Thi | Jul 13, 2024

Create your first demo site with Optimizely SaaS/Visual Builder

Hello everyone, We are very excited about the launch of our SaaS CMS and the new Visual Builder that comes with it. Since it is the first time you'...

Patrick Lam | Jul 11, 2024

Integrate a CMP workflow step with CMS

As you might know Optimizely has an integration where you can create and edit pages in the CMS directly from the CMP. One of the benefits of this i...

Marcus Hoffmann | Jul 10, 2024

GetNextSegment with empty Remaining causing fuzzes

Optimizely CMS offers you to create partial routers. This concept allows you display content differently depending on the routed content in the URL...

David Drouin-Prince | Jul 8, 2024 | Syndicated blog

Product Listing Page - using Graph

Optimizely Graph makes it possible to query your data in an advanced way, by using GraphQL. Querying data, using facets and search phrases, is very...

Jonas Bergqvist | Jul 5, 2024