简体   繁体   中英

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功能项目模板

发布菜单

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.

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 .

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM