The set-up of Console Applications in Xperience by Kentico

16/08/2024
Model.AuthorName
Milan Sustek


๐Ÿš€ Streamlined Content Migration

Redesigns, system upgrades and other significant changes are a natural part of any business's lifecycle. To make the process of content migration that comes along with it streamlined, we use the Console Applications.

These powerful tools:

  • Automate complex processes
  • Handle large volumes of data efficiently
  • Provide robust error-handling mechanisms

In short, Console Applications make our work on any of our client’s continuous projects much simpler and more effective, saving us time and saving our clients money.
 

๐Ÿ› ๏ธ Extensive navigation through the Kentico SDK

The official documentation has an article on leveraging the Kentico SDK within external applications. It covers both web and console applications. As of this moment, you might run into an error after completing all steps within featured documentation. In this article, I will expand on the provided materials and give you a clear solution to ensure your integration process is as smooth as possible.

Step 1: Create a New Console Application

Let's start by creating a new console application. You can do this easily by running the following command:
dotnet new console -n KenticoConsoleApplication

Step 2: Set the Database Connection String

Next, you'll need to set the database connection string. It's a straightforward process, simply create a new file named appsettings.json and paste the connection string from your web project into it. Your appsettings.json file should look like this:
{
  "ConnectionStrings": {
    "CMSConnectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=DancingGoat14;Integrated Security=True;Persist Security Info=False;Connect Timeout=60;Encrypt=False;Current Language=English;"
  }
}

Step 3: Install the Kentico Xperience NuGet Package

Once you've set up the connection string, the next step is to install the Kentico Xperience NuGet package. This package provides the necessary libraries to work with Kentico in your console application.

You can install it by running the following command:
dotnet add package Kentico.Xperience.Core

This will add the Xperience libraries to your project, enabling you to interact with your DXP data programmatically.

Step 4: Initialize the Xperience Application

After installing the Kentico Xperience NuGet package, the final step is to initialize the Xperience application within your console project. This ensures that all the necessary components are properly configured before you start interacting with the CMS data.

To do this, add the following code to your Program.cs file:
using CMS.DataEngine;

CMSApplication.Init();

Console.WriteLine("Xperience by Kentico successfully initialized!");

This is all that should be required according to the documentation. But attempting to run the console application may result in an error:
CMS.DataEngine.ApplicationInitException: Cannot access the database specified by the 'CMSConnectionString' connection string. Please install the database externally and set a correct connection string.

To quickly investigate what might be wrong, we can check the current connection string, use:
Console.WriteLine($"CMS connection string: {ConnectionHelper.ConnectionString}");

Unfortunately, another exception arises:
 CMS.Core.ServiceResolutionException: Resolution of 'CMS.Core.IConnectionStringService' failed with the following error: IoC container cannot be used for service resolution because it was not initialized yet.

To address this, we can first pre-initialize the Xperience application by calling:
CMSApplication.PreInit(initializeContainer: true);

Exception is gone but ConnectionHelper.ConnectionString property returns empty string ๐Ÿ™, despite being specified in appsettings.json, it’s likely due to the IConfiguration not being set up correctly. To resolve this, add 'Microsoft.Extensions.Configuration.Json' Nuget package
dotnet add Microsoft.Extensions.Configuration.Json

And configure IConfiguration manually with:
Service.Use<IConfiguration>(new ConfigurationManager().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json"));

This final step makes Console Applications ready for your Kentico project. I hope I helped you save your time and efforts.
 
As of 5. 9. 2024 the documentation for Console Applications integration has been extended and the hotfix streamlining the setup has been released! Thank you Kentico for the provided hotfix ๐Ÿ˜Š
 

You can find the full solution on Github: https://github.com/BiQ-Bluesoft/xperience-by-kentico-console-application