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
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
- Create an azure trigger function using Visual studio 2019 or 2022
- Add
Startup
class and add above code - Run
Boom!
2
Answers
It started working after removing the below line
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
serviceprovider
in thestartup.cs
class.First create the service provider and then start the singleton service.
Now to create the service provider first create a service collection.
Now use
buildserviceprovider
function to create a service provider.complete code :