IHostedService usable in Azure Functions App?
Asked Answered
C

1

13

Regardless of whether we should, can we use IHostedService in an Azure Functions App?

Here is an attempt to register a hosted service (background service, specifically) as IHostedService:

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

The Functions App then throws the following exception:

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'
Chucklehead answered 28/1, 2020 at 10:53 Comment(1)
I ran into the same problem while trying to add the HealthCheck services to azure functions!Antioch
E
12

No, this is not currently possible. There is some discussion on this GitHub issue:

This would not play well with the dynamic scaling infrastructure. The scale controller is not aware of any logic running outside of the context of a function execution, and may scale in if it believes an application is idle. Customers would not have a reliable mechanism to keep that running unless they're artificially triggering function executions, and this would certainly generate confusion and support cases.

The runtime and Functions infrastructure is not setup for compute use outside of the context of a function. Allowing the registration of custom hosted services would expose a feature that enables that, which would not play well with other infrastructure components (including fraud detection which could severely impact a customer's application)

The rest of the thread has more details and is worth checking out for more information.

Expansile answered 4/5, 2020 at 14:57 Comment(4)
Thanks @Donut! Follow-up question: I recall that the Microsoft docs concerning implementing a logger give explicit instruction to (cleverly) log to the hard drive, and then periodically move log entries to the log server in bulk. (Unfortunately I cannot seem to find this page anymore.) Is there a suggested alternative for this in Azure Functions?Chucklehead
@Chucklehead I'm not aware of any alternatives, but that's not to suggest that there isn't one. Could try asking as a separate question or looking through the issues on the Azure/azure-functions-host repo.Expansile
Come to think of it, the first point ("The scale controller is not aware of any logic [...]") seems to be remedied by cancellation tokens. Any background process should always honor cancellation (or risk being terminated mid-process). The scale controller could simply trigger shutdown, and give the instance 5 seconds before it kills it.Chucklehead
Come to think of it, most of what we have been discussing here applies particularly to a BackgroundService (the abstract class that helps you use IHostedService for background work). For many IHostedServices, which perform no continuous background work, the discussion is a non-issue. The Function may simply spin up and down, and an IHostedService will respond accordingly.Chucklehead

© 2022 - 2024 — McMap. All rights reserved.