If you think creating a project for Azure Functions in Visual Studio 2019, it is easy, I have a 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"
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...
You can't! There is no option for that. My problem is that I have to create a solution for Azure Functions in 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.
Press Next.
Select where you want to create your project and then press Next.
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.
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#.
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!
You find this code on my Github.