23

Since Microsoft has released .NET Core 2.0 for Azure Functions a few days ago, I'm trying to understand how to create a new Functions project in VS2017 targeting .NET Core.

I've tried many configurations and still ended up with .NET 4.7.

Did anyone manage to create a new Function targeting .NET Core?

Thanks.

NOP-MOV
  • 792
  • 2
  • 8
  • 28

3 Answers3

14

This is supported with the 1.0.5 release of the Microsoft.NET.Sdk.Functions package.

In your Azure Functions Project, do the following:

  • Update the Microsoft.NET.Sdk.Functions package version to 1.0.5
  • Right click on your project, click the Edit <projectname>.csproj option and modify the TargetFramework element value to netstandard2.0

This will should generate .NET Standard 2.0 assemblies with all the artifacts created by the Azure Functions tooling.

Johnathon Sullinger
  • 7,097
  • 5
  • 37
  • 102
Fabio Cavalcante
  • 12,328
  • 3
  • 35
  • 43
  • Tried to create a default HTTP Trigger, couldn't compile because "GetQueryNameValuePairs" was missing from request object. – NOP-MOV Oct 07 '17 at 19:19
  • You're just missing a reference to the required WebApi package that exposes that extension method (`Microsoft.AspNet.WebApi.Core`). Add the package reference and you'll be done. – Fabio Cavalcante Oct 08 '17 at 02:07
  • Did that, now compiler ambiguous about all HttpRequest methods. Will wait for 1.0.6 :-) or a RTM VS2017 with that. Thanks. – NOP-MOV Oct 08 '17 at 12:33
  • HttpRequest? Or HttpRequestMessage? Is the use of the extension method required in your scenario? Or are you just trying to get the 1.0 template to work? You can indeed change to use HttpRequest as that will be the preferred way in 2.0. – Fabio Cavalcante Oct 08 '17 at 18:51
  • For reference, this is what the default 2.0 template for HTTP Trigger will look like: https://github.com/Azure/azure-webjobs-sdk-templates/blob/6ae2e9feada6be9c3c5bd12307e46ed78bf3af5e/Functions.Templates/Templates/HttpTrigger-CSharp/HttpTriggerCSharp.cs . You'll also get a function with that new template if you use the CLI `func new` command. – Fabio Cavalcante Oct 08 '17 at 20:15
14

As of today I have been able to target .Net Standard 2.0 in a "reasonably" intuitive way and without editing any .csproj files.

You need a reasonably recent version of Visual Studio. I'm using Visual Studio Professional 15.5.3 (although I would guess that community would work).

You need to have the Azure development workload installed. This will install an extension called Azure Functions and Web Jobs Tools.

So far so plain vanilla. There were 2 additional bits that were to me not at all intuitive but ended up being very easy to do - easy when you know how!

  1. You need to make sure that the Azure Functions and Web Jobs Tool is 15.0.31114.0 or greater - that's when they added .net core 2.0 support (see https://github.com/Azure/Azure-Functions/blob/master/VS-AzureTools-ReleaseNotes.md). You can update this using Tools/Extensions and Updates, or see https://marketplace.visualstudio.com/items?itemName=VisualStudioWebandAzureTools.AzureFunctionsandWebJobsTools

  2. Even when you've done that, Visual Studio is a tiny bit weird about letting you create Azure Functions that target .net 2.0. When you go File/New Project, nothing has changed in the list of available project types, and if you select Azure Functions, the list of Frameworks just shows .NET Framework *, no .NET Standard, no .NET Core.
    enter image description here

But if at this point you persevere and select Azure Functions, you then get a new dialog I hadn't seen before, which allows you to select Azure Functions v2 Preview (.NET Core).Azure Functions v2 Preview (.NET Core)

Now, when I then look at the project properties, it turns out it is targeting .NET Standard 2.0, which in turn seems to contain Microsfot.NETCore.Platforms (1.1.0). So is this .net core 2.0 or not? Not quite sure but its' good enough for my purposes so now going to tuck into some coding.

Cheers!

enter image description here

ubienewbie
  • 1,771
  • 17
  • 31
  • Good info about required version 15.0.31114.0 of Azure Functions and Web Jobs Tool. – Kristoffer Jälén Jan 11 '18 at 13:07
  • 3
    It doesn't work. when I tried to reference a .net core 2 project to my Azure Function an error comes up: Error Project 'X' targets 'netcoreapp2.0'. It cannot be referenced by a project that targets '.NETStandard,Version=v2.0'. – Amir Chatrbahr Jan 24 '18 at 22:21
  • With the current version of Azure Functions and Web Job Tools (15.9.02046), Azure Functions v2 is no longer preview. You can manually change your `.csproj` file to update `TargetFramework` to `netstandard2.1` and then update `AzureFunctionsVersion` to `v2`.You also no longer need the reference to `Microsoft.CSharp` so this item can be removed. – Doug Lampe Feb 21 '19 at 19:57
5

For now, it's a manual process. You need to create .NET Standard 2.0 library, add function.json manually and run it with core tools.

the package Microsoft.NET.Sdk.Functions does not yet support .NET Standard 2.0 class libraries. This is the package that identifies a project as Functions project to Visual Studio and generates function.json from attributes during build.

You can still author C# class libraries that target .NET Standard 2.0, but you must use a manually-authored function.json. The templates for C# class libraries aren’t yet available in the Core Tools, but you can get a sample from GitHub.

from Develop Azure Functions on any platform

Update: 1.0.5 version of SDK should now support it, as mentioned by Fabio.

Community
  • 1
  • 1
Mikhail Shilkov
  • 34,128
  • 3
  • 68
  • 107
  • This has changed since the blog was originally posted and this is now supported with 1.0.5. The post will be updated to reflect that soon. – Fabio Cavalcante Oct 08 '17 at 20:17