Azure Function App VS 2019 .NET 3.0 中的错误 - 找不到方法:'IFunctionsHostBuilder.get_Services()'

Error in Azure Function App VS 2019 .NET 3.0 - Method not found: 'IFunctionsHostBuilder.get_Services()'

Azure Functions/EFSQLSERVER .NET CORE 3.0 问题:

重现:

导致错误,如以下代码片段所示。有人遇到过这个问题吗?

Azure Functions Core Tools (2.7.1633 Commit hash: 45c7d86a3bbc9ed0a80a8f4199aa7ea80ccfb24e)
Function Runtime Version: 2.0.12673.0
[10/4/2019 6:13:14 PM] Building host: startup suppressed:False, configuration suppressed: False
[10/4/2019 6:13:14 PM] Loading startup extension 'Startup'
[10/4/2019 6:13:14 PM] Loaded extension 'Startup' (1.0.0.0)
[10/4/2019 6:13:14 PM] Loading startup extension 'DurableTask'
[10/4/2019 6:13:14 PM] Loaded extension 'DurableTask' (1.0.0.0)
[10/4/2019 6:13:14 PM] A host error has occurred
[10/4/2019 6:13:14 PM] FunctionApp5: Method not found: 'Microsoft.Extensions.DependencyInjection.IServiceCollection Microsoft.Azure.Functions.Extensions.DependencyInjection.IFunctionsHostBuilder.get_Services()'.
Value cannot be null.
Parameter name: provider

我的 nuget 包来自 csproj 文件。

<ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.DurableTask" Version="1.8.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
  </ItemGroup>
using Microsoft.Azure.Functions.Extensions.DependencyInjection;


[assembly: FunctionsStartup(typeof(FunctionApp5.Startup))]
namespace FunctionApp5
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            **var x = builder.Services;**

        }
    }
}

目前,ASP.NET Core 3.0 当前不可用于 Azure 应用服务,请查看此 Microsoft doc

与 Core 3.0 完全兼容的 Azure Functions 3.0 将于 10 月推出,查看 here。不过现在还没有发布。

由此 issue,您可以发现 Azure Function 2.0 目前无法与任何 Microsoft.Extensions.* 3.* 包一起使用,并且无法与 .Net Core 3.0 服务共享代码。

有关 Azure Fuction 3.0 的更多信息,请查看此 discussion

您现在可以使用 .net core 3.0 创建 azure 函数。将 Microsoft.NET.Sdk.Functions 更新为 1.0.30-beta2 并将 AzureFunctionsVersion 设置为 v3-preview

阅读有关使用 .NET Core 3.0 开发 Azure Functions 的更多信息here

您现在可以使用 DI IFunctionsHostBuilder

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;

[assembly: FunctionsStartup(typeof(BI_Geo.AzureFunctions.Startup))]
namespace BI_Geo.AzureFunctions
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddScoped<IProcess, Process>();
        }
    }
}

在 Azure Functions 3.0 作为稳定版本可用之前,最简单的方法似乎是将 Microsoft.Extensions.Http 包降级到 2.2.0:

它帮助我解决了同样的问题,因为目前似乎没有其他解决方法。即使是可用的测试包也不能在构建服务器上运行。

此处有更多详细信息:Azure functions dependency injection - error when referencing class library (Github)。

这拯救了我的一天

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
  </PropertyGroup>

要添加其他注意事项,以下是我们的问题:

  • 我们的函数应用引用了域项目
  • 我们的域项目引用了 "Microsoft.Extensions.Localization 版本 5.0.1,后者又引用了 5.x 版本的 Microsoft.Extensions.DependencyInjection,这与我们的 3.x 运行时不兼容.一旦我们降级到参考 Microsoft.Extensions.Localization 版本 3.1.10Microsoft.Extensions.DependencyInjection 参考也随之降级并且一切正常。