<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><language>en</language><title>Blog posts by Bien Le</title> <link>https://world.optimizely.com/blogs/bien-le/</link><description></description><ttl>60</ttl><generator>Optimizely World</generator><item> <title>Adding Web Job to an existing CMS Web App</title>            <link>https://world.optimizely.com/blogs/bien-le/dates/2020/8/adding-web-job-to-an-existing-cms-web-app/</link>            <description>&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=&quot;/link/d87f7379b24042c0baefdeb8f11b864b.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/link/1978e514056c47998d89ccafeda3cb15.aspx&quot; /&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=&quot;language-csharp&quot;&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($&quot;+++ Getting the latest activity logs +++&quot;);
 
            using (var dbConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;EPiServerDB&quot;].ConnectionString))
            {
                await dbConnection.OpenAsync().ConfigureAwait(false);
                var selectCommandText = &quot;select top 15 AL.LogData, AL.ChangeDate, AL.ChangedBy from tblActivityLog as AL order by AL.ChangeDate desc&quot;;
                using (var command = new SqlCommand(selectCommandText, dbConnection))
                {
                    using (var reader = await command.ExecuteReaderAsync(CommandBehavior.CloseConnection).ConfigureAwait(false))
                    {
                        while (reader.Read())
                        {
                            log.WriteLine($&quot;Data :{reader[&quot;LogData&quot;]} - Changed on: {reader[&quot;ChangeDate&quot;]} - Changed by: {reader[&quot;ChangedBy&quot;]}&quot;);
                        }
                    }
                }
            }
        }
    }
}
&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=&quot;language-csharp&quot;&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=&quot;language-markup&quot;&gt;&lt;code&gt;&amp;lt;add name=&quot;EPiServerDB&quot;
         connectionString=&quot;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;&quot;
         providerName=&quot;System.Data.SqlClient&quot; /&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&#39;s add the project as Azure WebJob to the WebApp&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/link/023717eee7b04b649a5e9d6c0e9b84e7.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/link/6622fd8fa38b479f8bd0e0c649adad03.aspx&quot; alt=&quot;imagebkyx.png&quot; /&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=&quot;language-markup&quot;&gt;&lt;code&gt;{
  &quot;$schema&quot;: &quot;http://schemastore.org/schemas/json/webjobs-list.json&quot;,
  &quot;WebJobs&quot;: [
    {
      &quot;filePath&quot;: &quot;../GetLatestActivityLogs/GetLatestActivityLogs.csproj&quot;
    }
  ]
}
&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=&quot;language-xml&quot;&gt;&lt;code&gt;{
  &quot;$schema&quot;: &quot;http://schemastore.org/schemas/json/webjob-publish-settings.json&quot;,
  &quot;webJobName&quot;: &quot;GetLatestActivityLogs&quot;,
  &quot;runMode&quot;: &quot;Continuous&quot;
}
&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=&quot;/link/e5d6033848a0428a8c572b6a72054d78.aspx&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/link/15a312021c5c484b82e58614b18c14db.aspx&quot; /&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=&quot;/link/f97c0b29df9b4ac890ed341750715cd6.aspx&quot; /&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=&quot;/link/31f8ca3acb214de2b9662d9172053f14.aspx&quot; /&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=&quot;/link/c8886c2bbfaa47d18863c534c6d4c87e.aspx&quot; /&gt;&lt;/p&gt;</description>            <guid>https://world.optimizely.com/blogs/bien-le/dates/2020/8/adding-web-job-to-an-existing-cms-web-app/</guid>            <pubDate>Mon, 31 Aug 2020 06:28:34 GMT</pubDate>           <category>Blog post</category></item></channel>
</rss>