简体   繁体   English

如何在Visual Studio 2017中创建F#Azure Functions(v1和v2)项目?

[英]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. 我想在Visual Studio 2017中轻松创建F#Azure Functions(v2)项目。

Is there some ZIP file with a Template F# project that I can use and publish using Visual Studio Publish context menu? 是否有一些带有Template F#项目的ZIP文件,我可以使用Visual Studio Publish上下文菜单进行发布?

I would like that VS has a F# Azure Function Project template, like it has for C#. 我希望VS有一个F#Azure Function Project模板,就像它对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. 缺少F#的Azure Functions模板,这意味着无法在Visual Studio和Functions CLI中创建F#预编译项目。

There is an open github issue to introduce such support. 有一个开放的github问题来介绍这种支持。 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 创建一个通用的F#类库项目
  • Reference Functions SDK NuGet package 参考函数SDK NuGet包
  • 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. 您可以使用此示例作为起点,但请务必更新到最新版本的NuGet包。

For me, I had to go through converting a C# function project into F#: 对我来说,我不得不将C#函数项目转换为F#:

  1. Create C# Azure Function Project 创建C#Azure功能项目
  2. rename .csproj to .fsproj .csproj重命名为.fsproj
  3. Edit the .fsproj file and make sure the following items are in there: 编辑.fsproj文件并确保其中包含以下项目:

  <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. 确保将host.jsonlocal.settings.json设置为<Content...而不是<None...因此将其复制到输出文件。

  1. Make sure you have the Microsoft.NET.Sdk.Functions installed 确保安装了Microsoft.NET.Sdk.Functions
  2. Your Function1.fs file should look something like that (for an HttpTrigger ) 你的Function1.fs文件看起来应该是这样的(对于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... 只需右键单击该项目,然后单击Publish...
  2. Select Azure Function App and follow the instructions. 选择Azure Function App并按照说明操作。 Make sure to select Run from pakcage file . 确保选择Run from pakcage file

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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