Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a F# Azure Functions (v1 & v2) Project in Visual Studio 2017?

I want to easily create a F# Azure Functions (v2) Project in Visual Studio 2017.

Is there some ZIP file with a Template F# project that I can use and publish using Visual Studio Publish context menu?

I would like that VS has a F# Azure Function Project template, like it has for C#.

C# Azure Function Project Template

Publish Menu

like image 419
Tony Avatar asked Apr 28 '18 17:04

Tony


2 Answers

Azure Functions templates for F# are missing, which means lack of possibility to create F# precompiled projects in Visual Studio and Functions CLI.

There is an open github issue to introduce such support. Even though it's not apparent from this issue, I was told that templates are coming very soon.

For now, you need to:

  • Create a generic F# class library project
  • Reference Functions SDK NuGet package
  • Add a static method for your Function

You could use this sample as a starting point, but be sure to update to latest versions of NuGet packages.

like image 178
Mikhail Shilkov Avatar answered Sep 30 '22 05:09

Mikhail Shilkov


For me, I had to go through converting a C# function project into F#:

  1. Create C# Azure Function Project
  2. rename .csproj to .fsproj
  3. Edit the .fsproj file and make sure the following items are in there:

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.24" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Function1.fs" />
    <Content Include="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </Content>
  </ItemGroup>

Make sure you set host.json and local.settings.json to <Content... instead of <None... so it copies it to the output file.

  1. Make sure you have the Microsoft.NET.Sdk.Functions installed
  2. Your Function1.fs file should look something like that (for an HttpTrigger)

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 Function1 =
    [<FunctionName("Function1")>]
    let Run ([<HttpTrigger(AuthorizationLevel.Function, [|"post"|])>] req: HttpRequest) (log: ILogger) = 
        async {
            return "some result"
        }
        |> Async.StartAsTask

  1. Now you are ready to deploy. Just right click on the project and click Publish...
  2. Select Azure Function App and follow the instructions. Make sure to select Run from pakcage file.

enter image description here

like image 24
Alkasai Avatar answered Sep 30 '22 06:09

Alkasai