Tim*_*imo 10 c# azure background-service azure-functions asp.net-core-hosted-services
无论是否应该,我们都可以IHostedService在 Azure Functions 应用程序中使用吗?
这是尝试将托管服务(特别是后台服务)注册为IHostedService:
internal sealed class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHostedService<ExampleBackgroundService>();
}
}
Run Code Online (Sandbox Code Playgroud)
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'
Run Code Online (Sandbox Code Playgroud)
Don*_*nut 11
不,目前这是不可能的。关于这个GitHub 问题有一些讨论:
这对于动态扩展基础设施来说效果不佳。缩放控制器不知道在函数执行上下文之外运行的任何逻辑,并且如果它认为应用程序空闲,则可以缩小规模。客户不会有可靠的机制来保持其运行,除非他们人为触发函数执行,这肯定会产生混乱和支持案例。
运行时和函数基础设施未设置为在函数上下文之外进行计算。允许注册自定义托管服务将公开启用该服务的功能,该功能无法与其他基础设施组件很好地配合(包括可能严重影响客户应用程序的欺诈检测)
该线程的其余部分有更多详细信息,值得查看以获取更多信息。