Create a project for Azure Function in F# and Visual Studio 2019

Microsoft F# Fsharp

If you think creating a project for Azure Functions in Visual Studio 2019, it is easy, I have the bad news for you!

Easy scenario. You want to create a simple Azure Function project with F# using Visual Studio 2019. Easy, right? First, launch Visual Studio 2019 and select “Create a new project

Visual Studio 2019 Start page
Visual Studio 2019 Start page

Then, you can create new project with the wizard. For example, if you select from the dropdown “All languages” F# and from “All platforms” Azure, surprise surprise…

Visual Studio 2019 - Create a new project
Visual Studio 2019 – Create a new project

You can’t! There is no option for that. My problem is that I have to create a solution for Azure Functions in F#.

Visual Studio 2019 - Create a new project for Azure Function and F#
Visual Studio 2019 – Create a new project for Azure Function and F#

Depressing, I found a workaround that I want to share with you. Hopefully, you can give me a better solution.

Create a new solution in C# for Azure Functions.

Visual Studio 2019 and Azure Functions in C#
Visual Studio 2019 and Azure Functions in C#

Press Next.

Visual Studio 2019: configure your new project
Visual Studio 2019: configure your new project

Select where you want to create your project and then press Next.

Visual Studio 2019: create a new Azure Functions Application
Visual Studio 2019: create a new Azure Functions Application

Http Trigger is ok for this example; we have time to complicate the function later. Press Create and then you have you C# project in a solution! Run the project to be sure it is working fine.

Azure Function up and running
Azure Function up and running

Done! Oh, wait, F#?

This is the funny part. Follow my simple instructions:

  • close Visual Studio
  • rename .csproj to .fsproj
  • open the solution file and replace .csproj to .fsproj
  • edit the .fsproj file and make sure the following items are in there for Azure Functions v3:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
    <RootNamespace>vspan_aBillity</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Open this solution with Visual Studio again. Now, the solution is in F#. You can add a new Azure Function in F#.

Visual Studio 2019: add new item
Visual Studio 2019: add new item

Replace the content of the new file with this one:

namespace FunctionApp1

open System
open Microsoft.Azure.WebJobs
open Microsoft.Azure.WebJobs.Host
open System;
open System.IO;
open System.Threading.Tasks;
open Microsoft.AspNetCore.Mvc;
open Microsoft.Azure.WebJobs;
open Microsoft.Azure.WebJobs.Extensions.Http;
open Microsoft.AspNetCore.Http;
open Microsoft.Extensions.Logging;

module GetOrganizations =
    [<FunctionName("GetOrganizations")>]
    let Run ([<HttpTrigger(AuthorizationLevel.Function, [|"get"|])>] req: HttpRequest) (log: ILogger) = 
        async {
            return "some result"
        }
        |> Async.StartAsTask

Play the project. The function is working and then you can browse your function!

Azure Function in F# is working
Azure Function in F# is working
Call the function with your browser
Call the function with your browser

You find this code on my Github. Also, if you want to know more about what Visual Studio can do for you, I recommend to read my article about digital transformation.