IHostedService可在Azure Functions App中使用?

问题描述 投票:4回答:1

无论我们是否 ,我们可以使用 IHostedService 的Azure Functions App中?

这里是尝试将一个托管服务(特别是后台服务)注册为 "Azure Functions App"。IHostedService:

internal sealed class Startup : FunctionsStartup
{
    public override void Configure(IFunctionsHostBuilder builder)
    {
        builder.Services.AddHostedService<ExampleBackgroundService>();
    }
}

Functions App就会抛出以下异常。

Microsoft.Azure.WebJobs.Script.InvalidHostServicesException: 'The following service registrations did not match the expected services:
  [Invalid] ServiceType: Microsoft.Extensions.Hosting.IHostedService, Lifetime: Singleton, ImplementationType: ExampleBackgroundService'
c# azure azure-functions background-service asp.net-core-hosted-services
1个回答
1
投票

不,目前还不能这样做。关于这一点,有一些讨论 GitHub问题:

这不能很好地与动态扩展基础设施配合。规模控制器不知道在函数执行范围之外运行的任何逻辑,如果它认为应用程序是空闲的,它可能会进行规模调整。客户不会有一个可靠的机制来保持这种运行,除非他们人为地触发函数执行,这肯定会产生混乱和支持案例。

运行时和Functions基础架构的设置不是为了在函数上下文之外的计算使用。允许注册自定义托管服务将暴露出一个能够实现该功能的功能,这将无法与其他基础设施组件(包括欺诈检测,这可能会严重影响客户的应用程序)很好地发挥。

剩下的帖子有更多的细节,值得查看更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.