不管我们是否should,我们可以使用IHostedService
在 Azure Functions 应用程序中?
这是尝试将托管服务(特别是后台服务)注册为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'
不,目前这是不可能的。对此有一些讨论GitHub问题 https://github.com/Azure/azure-functions-host/issues/5447#issuecomment-575368316:
这对于动态扩展基础设施来说效果不佳。缩放控制器不知道在函数执行上下文之外运行的任何逻辑,并且如果它认为应用程序空闲,则可以缩小规模。客户不会有可靠的机制来保持其运行,除非他们人为触发函数执行,这肯定会产生混乱和支持案例。
运行时和函数基础设施未设置为在函数上下文之外进行计算。允许注册自定义托管服务将公开启用此功能的功能,该功能无法与其他基础设施组件很好地配合(包括可能严重影响客户应用程序的欺诈检测)
该线程的其余部分有更多详细信息,值得查看以获取更多信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)