<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">Blog posts by Bien Le</title><link href="https://world.optimizely.com/blogs/bien-le/" /><updated>2020-08-31T06:28:34.0000000Z</updated><id>https://world.optimizely.com/blogs/bien-le/</id><generator uri="https://world.optimizely.com/blogs/bien-le/" version="2.0">Optimizely World</generator><entry><title>Adding Web Job to an existing CMS Web App</title><link href="https://world.optimizely.com/blogs/bien-le/dates/2020/8/adding-web-job-to-an-existing-cms-web-app/" /><id>https://world.optimizely.com/blogs/bien-le/dates/2020/8/adding-web-job-to-an-existing-cms-web-app/</id><published>2020-08-31T06:28:34.0000000Z</published><updated>2020-08-31T06:28:34.0000000Z</updated><author><name>bien.le@episerver.com</name></author><category term="Blog post" /><summary type="html">&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;First, add a new project to the solution which has the existing CMS web app and choose Azure WebJob template.&lt;/p&gt;
&lt;p&gt;&lt;img src="/link/d87f7379b24042c0baefdeb8f11b864b.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="/link/1978e514056c47998d89ccafeda3cb15.aspx" /&gt;&lt;/p&gt;
&lt;div&gt;Once the project is created, replace the &lt;strong&gt;Function.cs&lt;/strong&gt; file with the content below:&lt;/div&gt;
&lt;pre class="language-csharp"&gt;&lt;code&gt;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"]}");
                        }
                    }
                }
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;div&gt;And replace the &lt;strong&gt;Program.cs&lt;/strong&gt; file with this content:&lt;/div&gt;
&lt;pre class="language-csharp"&gt;&lt;code&gt;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 () =&amp;gt;
            {
                await Functions.GetLatestActivityLogs(Console.Out).ConfigureAwait(false);
            }).GetAwaiter().GetResult();
        }
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then add database connection string to the &lt;strong&gt;App.config&lt;/strong&gt; file&lt;/p&gt;
&lt;pre class="language-markup"&gt;&lt;code&gt;&amp;lt;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" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;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&lt;/p&gt;
&lt;p&gt;&lt;img src="/link/023717eee7b04b649a5e9d6c0e9b84e7.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="/link/6622fd8fa38b479f8bd0e0c649adad03.aspx" alt="imagebkyx.png" /&gt;&lt;/p&gt;
&lt;p&gt;A file named &lt;strong&gt;webjobs-list.json&lt;/strong&gt; added to the web app project&lt;/p&gt;
&lt;pre class="language-markup"&gt;&lt;code&gt;{
  "$schema": "http://schemastore.org/schemas/json/webjobs-list.json",
  "WebJobs": [
    {
      "filePath": "../GetLatestActivityLogs/GetLatestActivityLogs.csproj"
    }
  ]
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In which the &lt;em&gt;filePath&lt;/em&gt; points to path of the WebJob project. And a file added to the project itself, &lt;strong&gt;webjob-publish-settings.json&lt;/strong&gt;, in which defines WebJob name and run mode.&lt;/p&gt;
&lt;pre class="language-xml"&gt;&lt;code&gt;{
  "$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
  "webJobName": "GetLatestActivityLogs",
  "runMode": "Continuous"
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;p&gt;&lt;img src="/link/e5d6033848a0428a8c572b6a72054d78.aspx" /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="/link/15a312021c5c484b82e58614b18c14db.aspx" /&gt;&lt;/p&gt;
&lt;div&gt;Uncheck the option [&lt;em&gt;&lt;strong&gt;Exclude files from the App_Data folder&lt;/strong&gt;&lt;/em&gt;], and then publish the web app as usual.&lt;/div&gt;
&lt;p&gt;&lt;img src="/link/f97c0b29df9b4ac890ed341750715cd6.aspx" /&gt;&lt;/p&gt;
&lt;div&gt;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.&lt;/div&gt;
&lt;p&gt;&lt;img src="/link/31f8ca3acb214de2b9662d9172053f14.aspx" /&gt;&lt;/p&gt;
&lt;div&gt;Run the WebJob and you can check Logs to see the result, it should look like this:&lt;/div&gt;
&lt;p&gt;&lt;img src="/link/c8886c2bbfaa47d18863c534c6d4c87e.aspx" /&gt;&lt;/p&gt;</summary></entry></feed>