Introduction

I like to have the latest preview bits from the .NET world installed on my computer and this includes the latest previews of Visual Studio. Being able to install Visual Studio versions side by side is a brilliant concept and something I’m really glad Microsoft did. This allows for running the main version of VS (current 2019 16.10.4), with the 2019 preview (16.11.0 preview 3) and the latest 2022 preview build without them fighting each!

The problem with running preview builds of things comes preview build problems. Now this very rarely happens now a days with Visual Studio, or at least for me it does. However when preview builds of .NET 6 started appearing I noticed there was an issue.

Issue

Since installing the latest preview bits for .NET 6 I noticed that there started to be a problem creating new Azure Function through the func CLI.

If I started up a terminal, navigated to a new directory and created a new Azure Functions application it would create as expected (when selecting dotnet worker runtime).

func init

This executed fine. But when I came to create a function inside this application …

func new

I discovered an issue.

C:\Temp\functions> func new
Use the up/down arrow keys to select a template:Function name: TestHttpTrigger
TestHttpTrigger
No templates found matching: 'HttpTrigger'.
To list installed templates, run 'dotnet new --list'.
To search for the templates on NuGet.org, run 'dotnet new HttpTrigger --search'.

I would select a new Http Trigger from the options and give it a name as required. But then after a few seconds it would fail with the error above. At first I thought had I not installed the templates? Was it due to having the v4 preview Azure functions tools installed? So I uninstalled those and went back to the v3. But it still didn’t work.

So what’s the problem?

Well I think it’s down to the new preview version of .NET 6 and the new out of process hosting model etc. and something isn’t liking the new world with the old world and the preview world combined.

There is however an easy solution to this issue.

Solution

We need to help the Azure Functions tooling know where the templates are and to do that we need to tell it which SDK to use. This is exactly the same as how you would pin an ASP.NET Core application to a specific version of the SDK.

First we need to list the installed .NET SDKs on your computer.

dotnet --list-sdks

I get an output similar to this.

2.2.401 [C:\Program Files\dotnet\sdk]
3.1.301 [C:\Program Files\dotnet\sdk]
3.1.406 [C:\Program Files\dotnet\sdk]
5.0.103 [C:\Program Files\dotnet\sdk]
5.0.302 [C:\Program Files\dotnet\sdk]
5.0.400-preview.21328.4 [C:\Program Files\dotnet\sdk]
6.0.100-preview.6.21355.2 [C:\Program Files\dotnet\sdk]

Now this is in relation to a v3 Azure Function. As far as I am aware, at time of writing, these still require the .NET Core 3.x SDK to build. To tell the command line where to look we need to create a global.json file to pin the application to a specific SDK version.

To do this you can use the globaljson template and the dotnet cli command. As you can see from the above I have the 3.1.406 SDK installed so that is the SDK version I am going to specify.

dotnet new globaljson --sdk-version 3.1.406

Once the global.json file has been created you can run func new as normal and create a new C# HttpTrigger.

Note For .NET 5

This problem also causes .NET 5 versions, aka “dotnetIsolated”, to fail when selected through the CLI tooling. Creating a global json for the .NET 5 SDK installed on your machine through the technique above allows you to continue creating functions as well.

Conclusion

It appears there is an issue with the preview build of the CLI and compatibility issues. I’m unsure if there is a GitHub issue for this as I have been unable to find it, so maybe it’s just me, but if you come across this issue then hope it helps.

Let me know on Twitter @WestDiscGolf if you are experiencing the same issue!