不管怎样,你的fs确实有问题。我在这里发布了可以成功触发的函数的文件和代码:
这是我的函数应用程序的结构:
这是我的 .fsproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<Compile Include="HttpTrigger.fs" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
</ItemGroup>
<ItemGroup>
<Content Include="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</Content>
</ItemGroup>
</Project>
这是我的触发文件 HttpTrigger.fs:
namespace Company.Function
open System
open System.IO
open Microsoft.AspNetCore.Mvc
open Microsoft.Azure.WebJobs
open Microsoft.Azure.WebJobs.Extensions.Http
open Microsoft.AspNetCore.Http
open Newtonsoft.Json
open Microsoft.Extensions.Logging
module HttpTrigger =
// Define a nullable container to deserialize into.
[<AllowNullLiteral>]
type NameContainer() =
member val Name = "" with get, set
// For convenience, it's better to have a central place for the literal.
[<Literal>]
let Name = "name"
[<FunctionName("HttpTrigger")>]
let run ([<HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)>]req: HttpRequest) (log: ILogger) =
async {
log.LogInformation("F# HTTP trigger function processed a request.")
let nameOpt =
if req.Query.ContainsKey(Name) then
Some(req.Query.[Name].[0])
else
None
use stream = new StreamReader(req.Body)
let! reqBody = stream.ReadToEndAsync() |> Async.AwaitTask
let data = JsonConvert.DeserializeObject<NameContainer>(reqBody)
let name =
match nameOpt with
| Some n -> n
| None ->
match data with
| null -> ""
| nc -> nc.Name
let responseMessage =
if (String.IsNullOrWhiteSpace(name)) then
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
else
"Hello, " + name + ". This HTTP triggered function executed successfully."
return OkObjectResult(responseMessage) :> IActionResult
} |> Async.StartAsTask
工作正常,成功的日志:
这是我创建和运行 F# 函数的步骤:
1.在cmd中运行这两个命令:
dotnet new --install Microsoft.Azure.WebJobs.ItemTemplates
dotnet new --install Microsoft.Azure.WebJobs.ProjectTemplates
2.创建函数应用:
dotnet new func --language F# --name FunctionsInFSharp
3.创建触发器:
dotnet new http --language F# --name HttpTrigger
4.添加触发器编译:
<Compile Include="HttpTrigger.fs" />
请将上述代码添加到 .fsproj 文件中的 ItemGroup 中。
5.更改模板,将这些从无更改为内容:
我给出的只是一个简单的例子,说明如何成功触发F#函数,请尝试一下,告诉我是否可以解决。:)