使用依赖项注入时未注册持久的 Azure 函数绑定类型

Durable Azure function binding types not registered when using dependency injection

我创建了一个基于 visual studio "Add new function" 到项目的标准持久函数。这开箱即用。

然后我按照这里的步骤:https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection添加依赖注入。添加 Microsoft.Extensions.http。

这破坏了功能,我得到错误:

[03-Mar-20 14:58:18] The 'test' function is in error: The binding type(s) 'orchestrationTrigger' are not registered. Please ensure the type is correct and the binding extension is installed.
[03-Mar-20 14:58:18] The 'test_Hello' function is in error: The binding type(s) 'activityTrigger' are not registered. Please ensure the type is correct and the binding extension is installed.
[03-Mar-20 14:58:18] The 'test_HttpStart' function is in error: Microsoft.Azure.WebJobs.Host: Error indexing method 'test_HttpStart'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'starter' to type IDurableOrchestrationClient. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

在此状态下 startup.cs 不会 运行,这在我以前创建的函数中从未发生过,但我可以通过添加 extensions.json 和以下内容来解决此问题:

{
  "extensions": [
    {
      "name": "Startup",
      "typeName": "FunctionApp1.Startup, FunctionApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
    }
  ]
}

这使 startup.cs 运行 中的 Configure 方法成为可能,但我仍然遇到相同的错误。

startup.cs

public override void Configure(IFunctionsHostBuilder builder)
{
    builder.Services.AddHttpClient();
}

Function2.cs

public class 函数 2 { [函数名("test")] public 异步任务> RunOrchestrator( [OrchestrationTrigger] IDurableOrchestrationContext 上下文) { var outputs = new List();

        // Replace "hello" with the name of your Durable Activity Function.
        outputs.Add(await context.CallActivityAsync<string>("test_Hello", "Tokyo"));
        outputs.Add(await context.CallActivityAsync<string>("test_Hello", "Seattle"));
        outputs.Add(await context.CallActivityAsync<string>("test_Hello", "London"));

        // returns ["Hello Tokyo!", "Hello Seattle!", "Hello London!"]
        return outputs;
    }

    [FunctionName("test_Hello")]
    public string SayHello([ActivityTrigger] string name, ILogger log)
    {
        log.LogInformation($"Saying hello to {name}.");
        return $"Hello {name}!";
    }

    [FunctionName("test_HttpStart")]
    public async Task<HttpResponseMessage> HttpStart(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")]HttpRequestMessage req,
        [DurableClient]IDurableOrchestrationClient starter,
        ILogger log)
    {
        // Function input comes from the request content.
        string instanceId = await starter.StartNewAsync("test", null);

        log.LogInformation($"Started orchestration with ID = '{instanceId}'.");

        return starter.CreateCheckStatusResponse(req, instanceId);
    }
}

我使用的是.net core 3.1 Microsoft.Azure.Functions.Extensions1.0.0

Microsoft.Azure.WebJobs.Extensions.DurableTask 2.1.1

Microsoft.Extensions.Http 3.1.2

Microsoft.NET.sdk.Functions 3.0.4

暂时,尝试将 Microsoft.NET.sdk.Functions 3.0.4 降级到以前的版本。该团队进行了一些性能改进,但我看到有人提出了同样的问题(DI 问题)