azure-webjobs-sdk icon indicating copy to clipboard operation
azure-webjobs-sdk copied to clipboard

StackOverFlow Exception from WebJobsBuilderExtensions while running Azure function locally

Open codemaker opened this issue 2 years ago • 2 comments

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>());

Here is my Startup.cs 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();
    }
  }
 } `

Repro steps

Reproduction steps

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

Expected behavior

Azure function shall run without any exceptions

Actual behavior

Stackoverflow exception

Known workarounds

codemaker avatar Oct 12 '22 07:10 codemaker

@codemaker We are able to reproduce the issue and getting the stack overflow exception, we will check with our next level team and update you with the solution. you can share us the function app name as below so that we check the system logs and reason for the exception. Sharing Your Function App name privately

ramya894 avatar Oct 12 '22 09:10 ramya894

@ramya894 We did not deploy the function to the cloud and hence there will be no app name to share.

deepakkumpala avatar Oct 13 '22 03:10 deepakkumpala

@codemaker What happens when you comment out/remove .AddSingleton(serviceProvider => serviceProvider) ?

kshyju avatar Oct 17 '22 00:10 kshyju

@kshyju It works fine when we remove .AddSingleton(serviceProvider => serviceProvider) , So don't we need this line anymore? How did it was working before ?

codemaker avatar Oct 17 '22 02:10 codemaker

Yes, that line is causing your issue. You should not use that. BTW, the other code snippet you have where you are replacing the Configuration is a problematic approach. If you want to add another JSON file as configuration source, the recommended approach is to inherit FunctionsStartup and override the ConfigureAppConfiguration method as shown in this doc.

kshyju avatar Oct 18 '22 15:10 kshyju

Closing as answered.

kshyju avatar Oct 18 '22 15:10 kshyju