March 2020 Ā» .Net 5.0 Preview 1 with Kentico 13 Beta 2 - A Double Phoenix
Mar 22

.Net 5.0 Preview 1 with Kentico 13 Beta 2 - A Double Phoenix

Introduction

If you have been listening to some of my recent podcast episodes, you have been hearing how I have been excited for a bit now about the Kentico 13 Beta 2 release and how it sets up Kentico EMS for the future. You may also have heard me talk about the future of .Net and why it matters to the .Net development community and why .Net Core is better than ASP.Net MVC 5 on the full .Net 4.x Framework.

Well I decided to put my money where my mouth is over the weekend. I was inspired to give the absolute latest bleeding edge scenario of .Net technology a try with the Kentico 13 Beta. I wanted to download Kentico Phoenix, install a clean version of it, and attempt to run .Net 5 Preview 1 as my front end MVC live site connected to Kentico. That's right, to hell with the 18-year-old full .Net Framework. Sorry young .Net Core 3.1, I am even skipping right past you too. I'll take the 6-day old version of .Net 5 (the successor to both). 

Or, as I aptly have termed this scenario, let’s create a Double Phoenix. Get it? .Net 5 is .Net Core reborn just like Kentico 13... Yeah, I know it's cheesy, but it’s a catchy hashtag for sure #DoublePhoenix.

 

How to Go from Nothing to Kentico 13 and .Net 5.0

In order to get things rolling I had to download a few packages and install a few updates on my development machine, as I wasn't really sure how this was going to work. It turns out that this was not too hard.

Below are the steps at a high level of what needs to happen, from there I will dig into a few specifics later in the post. However, this list of links is a good way to collect all the things you need in one place in case you want to follow along and try it out yourself.

  1. Download Kentico 13 Beta 2 on Kentico DevNet (will need a devnet account)
  2. Install a new instance of Kentico 13
  3. Install Visual Studio 2019 16.6 Preview (optional if you prefer VSCode and .Net CLI)
  4. Install the .Net 5.0 SDK
  5. Create a new MVC project that targets netcoreapp5.0
  6. Add in Kentico 13 NuGet Package references
  7. Wire up C# code required to integrate Kentico
  8. Build and Run

 

Download and Install Kentico 13 Beta 2

Grabbing the download from DevNet gets you the Kentico_2020_Beta2.exe installer, related pdf documentation, and license file that you need to get going. This walkthough assumes you have experience with installing Kentico via the installer, if you don't go check out the helps docs on how to install Kentico MVC. The first step is to just perform this basic installation. You can use custom installation as the type of install you want to use now.

One of the new aspects of the Kentico 13 Beta 2 is the fact that MVC as a project type is front and center in the new Kentico 13 installer.

 

Kentico 13 Beta 2 Installer MVC ONLY

 

Note that the Portal Engine option is greyed out and not a choice. I'd wager that this option won't even be in the final full release of Kentico 13.0 as Portal Engine is not supported any longer going forward. From there go ahead and select the site name you want to install and database to work with, you should create a new instance since this is just an experiment.

Once the install completes you should have a runnable admin interface of Kentico 13. The license file from the main download zip is what you will need next. Open that file, copy the license key, and paste that into the Kentico License application inside of the admin.

If you run into an odd message about the database not being able to be connected to, don't forget you may need to give the IIS APPPool username access to the databae catalog if you chose Windows Auth for the database setting (I had to do this step before the admin tool came up). You should be able to get the admin tool up like this if everything worked:

 

Kentico 13 Admin Tool

 

Installing .Net 5.0 SDK Preview 1

Now that we have Kentico 13 ready, it's time to work on our front end MVC live site. Typically this is where you would just go to Visual Studio and create a new MVC project. But since this is all new, that doesn't quite work. First of all, we need the new .Net 5.0 SDK (link at the top of this post). You have a choice, you can install the Visual Studio 2019 16.6 Preview as I mentioned. This preview build of Visual Studio will include the .Net 5.0 SDK and related project templates you need, or if you prefer to save a 15.4 GB download, you can just download the SDK directly and use VS Code and the .Net CLI tool (which is my preference).

 

.Net 5 SDK Preview Download  

 

Once that package is downloaded, go ahead and run through the installer. It's pretty easy, but make sure it finishes all the way. If it throws errors start over and make sure you have it cleanly installed. Also note that .Net 5.0 has greater support for Visual Basic (oh the horror that reminds me of). It should finish up with this screen.

 

.Net 5 SDK Preview Installed

 

This message is nice, but it doesn't always lead you to the exact next step. For that you are going to want to use the .Net CLI tooling (dotnet.exe).

 

Create a Project with .Net CLI Tooling

I am a big fan of the .Net Core CLI, and I am happy to see that .Net 5 is continuing and enhancing the use of this tool. Since the SDK is installed on your machine now you can either A) open up VS Code and use the terminal to do the next step, or B) fire up powershell or windows terminal outside of any editor. I'll go with option B because it makes for better screenshots. Go ahead and make a directory on your machine for where you want to create the new MVC live site project for Kentico. I always create mine inside of C:\ClientProjects for work stuff. So at the command line from that context I went through the following steps.

First, I'm going to create a new directory:

mkdir K13Beta2 

 

I'm going to next verify that the .Net CLI tooling has the correct versions and run times by running the following commands from inside the new directory:

dotnet --version

dotnet --list-sdks

 

Which produces this nice list (yes, I have been working with just a few versions of .Net Core).

 

.Net 5 CLI Tooling

 

If you don't see 5.0.100-preview.1.20155.7, something went wrong, and you should go back and try installing the SDK again.

Now I'm going to use this new tooling to create a new project that we can use with our Kentico 13 install. To do that, I am going to use the following command:

dotnet new mvc -o MVCLiveSite

 

This command is going to use the MVC template that ships with .Net 5. It will automatically target the netcoreapp5.0 framework for us, and create all the standard MVC convention and files that we need to get started. It results in this:

 

.Net 5 MVC Live Site

 

 

Add the Kentico 13 NuGet Packages

Now the first tricky part. We need to add a package reference to our new .csproj file to tell our project that we want to use the new Kentico 13 NuGet packages. The fun part is that the packages are not listed on Nuget.org (yet) because they are not fully public. We only have them locally (in the same folder as the Kentico 13 installation). The Kentico 13 .Net Standard NuGet packages are in the \NuGet Packages subfolder of the K13 folder. I copied those out and into my root folder so they exist at:

C:\ClientProjects\K13Beta2\NuGet Packages

 

Kentico 13 NuGet Packages

 

To easily add the packages from this local file we need to edit the .csproj file, so pop that open in VS Code and add in the following snippet as a new PropertyGroup:

  <PropertyGroup>

    <RestoreSources>$(RestoreSources);../NuGet Packages;https://api.nuget.org/v3/index.json</RestoreSources>

  </PropertyGroup>

 

This new RestoreSource tells us that the first source is our local directory, and after that hit up the normal NuGet rep location. Save that change and now hope back to your terminal window. Now we can simply use the standard add package command in the CLI.

cd MVCLiveSite

dotnet add package Kentico.Libraries

 

The add package command will use the newly edited .csproj file and add all of the NuGet packages in one fell swoop because they are dependent. Now at that point you are probably going to see a bunch of warnings about how the packages that we just brought in are all for the wrong version. This is true, we actually don't want anything to do with .Net Framework versions of these dlls. However, since we are using the .Net Standard aspect of things, these warnings do not matter. In fact I recommend adding in a compiler exclusion to ignore the warnings. Simply add this into your .csproj file.

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

    <NoWarn>NU1701</NoWarn>

  </PropertyGroup>

 

The fully edited .csproj file will end up looking like this:

 

Kentico 13 MVC Live Site CSPROJ

 

Wire up the C# Code

Now that we have Kentico 13 beta 2 installed, a new .Net 5 MVC project created, the next step is to wire up some Kentico API calls. But before we can do that, we need to add Kentico into the middlewear pipeline of the .Net 5 codebase. This is pretty much the exact same as using Kentico in .Net Core. So we can follow along with the directions given to us by Kentico in the Asp.Net Core Support.pdf file inside of the Documentation folder of the beta2 download zip (but not all of it).

The key here is to follow steps 5 and 6 of that documentation. We need to add in the connection string to the Kentico database inside of appsettings.json and also modify the Startup.cs file in our project.

Appsettings.json should look like this (grab the same connectionstring value from your Kentico 13 admin tool project's web.config).

 

Kentico 13 Appsettings

 

We need to modify the Startup.cs just as the documentation states. However, it doesn't mention that we need to add the correct Using statement at the top of the file.

using CMS.AspNetCore.Platform;

 

This makes it possible to add in sevices.AddKentico();

 

Kentico 13 Startup.cs

 

And later on in that file add in the two other documented lines of code:

 

Kentico 13 Startup.cs.2

 

That should be all the setup that we need to integrate Kentico 13 and .Net 5. At this point you should be able to build and run your project without error. Again you can do this from the terminal via:

dotnet build

dotnet run

 

Kentico 13 Build Run

 

This should get you a new website running that is connected to Kentico 13.

 

.Net 5 MVC Site

 

Prove it works with Kentico APIs

It's one thing to see the standard .Net 5 MVC template work, it's another thing to replace that message on the home page with content that is sourced from Kentico. So let's do that next. Since we went with a custom install of Kentico 13 that means we start with a blank slate (the right way to do things in the real world). That means we will most likely need at least one page type. Let's jump to the Kentico admin tool and create a new Page Type to replace the home page message with something that comes from Kentico. I logged in chose new Page Type from the Page Type app.

 

Kentico 13 Admin Page Types App

 

Part of Kentico 13's new features are that Page Types are MVC aware now. So we have a choice of how this Page Type is going to be used. For today, I am going to choose URL only (Page Builder is not yet supported in .Net Core in Beta 2 so it doesn't really matter). From there I am going to add two fields, Title and Summary just like any other field in Kentico, and add this page type as a child type of CMS.Root. Once that is completed I can navigate to the Pages app and create a new home page like so:

 

Kentico 13 Admin Pages App

 

Now that we have some content to work with, we can edit the HomeController.cs file with some standard Kentico C# code.

 

Kentico 13 HomeController.cs

 

Double Phoenix Lives

Once the Kentico 13 new page type, home page content, and code is complete, it's time to build and run the application again. Again I would hop into your terminal window in VSCode or powershell and run the following:

dotnet build

dotnet run

 

And the result should look like:

 

.Net 5 MVC Site with Kentico 13

 

 

Conclusion

I'm happy to see that this is all possible with the Kentico 13 Beta 2 release. I feel that it validates all the work that the Kentico team is doing to future proof the Kentico EMS platform. Honestly this was not too hard to setup / create. I can't wait till the end of the year when both technologies are released and production ready. It's going to open some amazing possibilities.

Don't get me wrong though, there is still a ton of work to do to get to that point. Some of the latest features don't even work yet in the .Net Standard libraries yet, like automatic Routing does in MVC 5 running full .Net Framework. So please remember this scenario is not ready for prime time! Do not use this approach in a production project! It's just more of an experiment.

I hope you found this blog post interesting, please let me know in the comments or on Twitter. Also feel free to give #DoublePhoenix a retweet!

 

You might also be interested in

[X]

Be the first to know about my latest Kentico Tips & Tricks!

Do not show again