skip to Main Content

I have written Azure function which is throwing StackOverFlow Exception in the below code from class WebJobsBuilderExtensions in the namespace ` Microsoft.Azure.WebJobs

 services.TryAddEnumerable(ServiceDescriptor.Singleton<IHostedService, JobHostService>());

enter image description here

Here is my Startup class

[assembly: WebJobsStartup(typeof(Startup))]
namespace FuncApp
{
public class Startup : IWebJobsStartup
{
    public void Configure(IWebJobsBuilder builder)
    {
        var config = new ConfigurationBuilder()
            .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();

        builder.Services
            .AddSingleton<IConfiguration>(config)
            .AddSingleton(serviceProvider => serviceProvider)
            .AddLogging();
    }
}
}

Note: The function was working fine few weeks back, and suddenly stopped working without any changes to the code.

Reproduction steps

  1. Create an azure trigger function using Visual studio 2019 or 2022
  2. Add Startup class and add above code
  3. Run

Boom!

2

Answers


  1. Chosen as BEST ANSWER

    It started working after removing the below line

      .AddSingleton(serviceProvider => serviceProvider)
    

    not sure how it was working before and what the change is under the hood!

    https://github.com/Azure/azure-webjobs-sdk/issues/2926#issuecomment-1280167555


    • The issue is because of the serviceprovider in the startup.cs class.
    • So instead of setting up it like
    .AddSingleton(serviceProvider => serviceProvider)
    

    First create the service provider and then start the singleton service.

    Now to create the service provider first create a service collection.

     var serviceCollection = new ServiceCollection();
    

    Now use buildserviceprovider function to create a service provider.

     var serviceCollection = new ServiceCollection();
    

    complete code :

      var serviceCollection = new ServiceCollection();
    
                var provider1 = serviceCollection.BuildServiceProvider(true);
                builder.Services
                    .AddSingleton<IConfiguration>(config)
                    .AddSingleton(provider1)
                    .AddLogging();
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search