7

Orchard Core .NET5 and C# 9

 3 years ago
source link: https://www.davidhayden.me/blog/orchard-core-net5-and-csharp9
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Orchard Core .NET5 and C# 9

When .NET5 and C# 9 was released I eagerly began developing ASP.NET Core web applications and web API's using the new features. The C# 9 Record Types are especially handy as well as features in EF Core 5 like better handling of many-to-many relationships. As an Orchard Core developer I eagerly awaited for .NET5 and C# 9 support in Orchard Core as well. It took longer than I had hoped, but in the latest pre-releases of Orchard Core we can now develop Orchard Core websites using .NET5 and C# 9 as well as .NET Core 3.1. In fact by default, all new Orchard Core websites now target .NET5!

Orchard Core Code Generation Tools

Assuming you've installed the latest pre-release version of the Orchard Core code generation tools, you can create an Orchard Core website using the .NET CLI with the following command.

$ dotnet new occms -n Web
The template "Orchard Core Cms Web App" was created successfully.

If you're new to Orchard Core, I have an Orchard Core tutorial that discusses the code generations tools to create an Orchard Core website.

This creates an Orchard Core website using project templates and the default parameters. There are parameters that can be passed as part of the command to specify target frameworks, etc., but as we will see in a moment, .NET5 is the default framework used by Orchard Core.

Orchard Core Targets .NET5 by Default

Once the Orchard Core website is generated using the code generation tools, open the project file using Visual Studio Code or your favorite editor or IDE of choice. Although I tend to use JetBrains Rider, let's open it in Visual Studio Code from the shell.

$ cd Web
$ code Web.csproj

Alternatively, you can just display the contents of the project file from the shell without using Visual Studio Code, JetBrains Rider, Visual Studio, etc.

$ cd Web
$ cat Web.csproj

Here is a snippet of the Orchard Core website's project file showing that Orchard Core is now targeting .NET5 by default.

<propertygroup>
  <targetframework>net5.0</targetframework>
  ...
</propertygroup>

This is pretty exciting as we can now take advantage of all the new features in .NET5 and C# 9 when developing Orchard Core websites.

Orchard Core and .NET Core 3.1

Although I've been developing all my new Orchard Core websites using .NET5 and have migrated a number of existing Orchard Core websites to .NET5, you may need to target .NET Core 3.1. As mentioned earlier, you can pass parameters to the Orchard Core code generation tools to specify the target framework for the Orchard Core website. By default, it will target .NET5 and C# 9, but if you wish to develop a new Orchard Code website using .NET Core 3.1, just pass it as a parameter.

$ dotnet new occms -fm netcoreapp3.1 -n Web
The template "Orchard Core Cms Web App" was created successfully.

Display the Orchard Core website's project file.

$ cd Web
$ cat Web.csproj

The Orchard Core website now targets .NET Core 3.1.

<propertygroup>
  <targetframework>netcoreapp3.1</targetframework>
  ...
</propertygroup>

As you probably suspect, you can just as easily change the target framework yourself by modifying the project file.

Conclusion

That's it! If you're developing a new Orchard Core website and wish to target .NET5 and C# 9, make sure you install the .NET5 SDK as well as the latest version of the Orchard Core source code or code generation tools. By default, new Orchard Core websites will target .NET5. However, if you're working on an existing Orchard Core website, you will have to manually update the project files and any source code to take advantage of the .NET5 and C# 9 features.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK