Creating .NET Core 2.0 Azure Functions

If you want to create an Azure Function in .NET Core 2.0 rather than a .NET Framework 4.6.1 function you’ll first need to install the “Azure development” workload into Visual Studio if you haven’t done so already as part of the initial installation.

You can then create a new Azure Function from the standard create new dialog.

Although the creation dialog appears to suggest this will use .NET Framework 4.6.1 the next screen will allow you to select .NET Core, though currently this is still in preview. If you don’t see this screen you may need to update your “Azure Functions and Web Jobs Tools” extension through “Tools” > “Extensions and Updates…”.

Once you’ve created your function Visual Studio will scaffold the project and create an initial method called “Function1”.

Currently NuGet also shows a warning for the package “Microsoft.AspNet.WebApi.Client” as the version included is targeting .NET Framework 4.6.1 rather than the desired .NETStandard 2.0, this error is safe to ignore and the reference will hopefully be updated when the extension moves out of preview.

As the .NETStandard 2.0 version of Azure Functions is still in preview it’s not that well documented and there’s a few things that differ from the .NET Framework 4.6.1 version.

HttpRequestMessage vs HttpRequest

Most guides to Azure Functions take the incoming request as a HttpRequestMessage, in .NETStandard 2.0 this should be HttpRequext. HttpRequestMessage is still accepted but in the preview version at least seems to suffer from dependency issues. The default function created when you create a new project provides an example of how this should be accessed.


    public static class GetSasToken
    {
        [FunctionName("GetSasToken")]
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequest req, TraceWriter log)
        {
			// Stuff here...
			
            string requestBody = new StreamReader(req.Body).ReadToEnd();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
			
			// Moe stuff here...
        }

Named query parameters can also be accessed like so.


string name = req.Query["name"];

Application Settings

In the .NET Framework 4.6.1 version you could access your local app settings as well as those set in Azure using ConnectionManager, this no longer works in .NETStandard 2.0 and you need to use dependency injection as with other projects.


[FunctionName("GetSasToken")]
public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]HttpRequest req, TraceWriter log, ExecutionContext context)
{
	var config = new ConfigurationBuilder()
		.SetBasePath(context.FunctionAppDirectory)
		.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
		.AddEnvironmentVariables()
		.Build();

	var storageAccount = CloudStorageAccount.Parse(config.GetConnectionString("AzureStorageConnection"));
}

The code for this function that the above samples come from can be found here.


Leave a Reply

Your email address will not be published. Required fields are marked *