3

I have an azure function whose startup looks like this

internal class Startup : FunctionsStartup
    {
        public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
        {

        }

        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.SomeExtensionMethod();// This will add few services to the service collection.
        }
    }

Consider multiple functions under a function app with this as the startup. Let one of the registered services in the builder.Services.SomeExtensionMethod() be IBootstrapService. Now I want to run a method in IBootstrapService right after the startup executes. Is there a way I can do this, since azure functions doesn't support IHostedService. I cannot go with isolated function apps because I have a lot of in-process functions which I need to migrate.

1 Answers1

0

Check my below findings helps to fix your issue

I can see there is an existing ticket in the GitHub regarding IHostedService implementation in Azure Functions .NET Core.

  • As .NET Core supports IHostedService NuGet Package, it will be supported in Azure Functions In-proces Projects.
  • It can be implemented in builder.services configuration method and starting the function host and then other services when running is the good approach.

SO #1 - At that time Custom IHostedService implementation is not supported due to some issues and it is fixed later.

Also, I can see a similar issue regarding implementation support of IHostedService in Azure Functions.NET Core on MS q&a Forum 933834 where @MughundhanRaveendran has provided the solution.

Pravallika KV
  • 2,415
  • 2
  • 2
  • 7
  • I tried it. Whenever I use IHostedService, I get this error in visual studio - ```A host error has occurred during startup operation '45ab4e7f-1ef2-448b-bd40-99567b322bf3'. func: Invalid host services. Microsoft.Azure.WebJobs.Script.WebHost: The following service registrations did not match the expected services: [Invalid] ServiceType: Microsoft.Extensions.Hosting.IHostedService, Lifetime: Singleton, ImplementationType: k8senricherissue.TestHostedService, k8senricherissue, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.``` In Kubernetes I don't get any error but the app crashes. – Sankar Mantripragada Feb 24 '23 at 08:16
  • Also the one mentioned in the MS q&a forum is about isolated func apps and not in-process ones. (Since it is mentioning creation of our own host instance which is not the case in in-process function apps) – Sankar Mantripragada Feb 27 '23 at 15:29
  • Could you provide your function code snippets how you configured? – Pravallika KV Feb 27 '23 at 16:18
  • You can find my function app here: https://github.com/mavsankar/k8senricherissuerepro – Sankar Mantripragada Feb 28 '23 at 08:40