question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for AddScoped to be per function calls

See original GitHub issue

With ASP.Net Core, using the DI with AddScoped is understood that the lifetime is per request. With WebJobs 3.0, AddScoped behaves the same way as AddSingleton, which does not play well when using a DbContext. AddScoped should be per “request”, basically per function call. At least, provide some entry points to start/stop the scope properly. Right now, the JobActivator does not allow to do such a thing because it is created with the IHostedService, which has a lifetime of the app. The GetService() cannot be used with scoping either.

I have searched everywhere how to actually hook up the scoping properly, and either all the executors are sealed or internal. The only way to make this work is to create a scope in each of my function calls which defeat the purpose of dependency injection…

I could create a scope in the CustomJobActivator, but it is not disposed properly.

Expected behavior

AddScoped<IMyService, MyService>() should be per request when injected in the Function class. Or, injecting my IMyService in the method parameters could work as well.

Actual behavior

Everything is treated as Singleton scope.

Known workarounds

public class ContinuousMethods 
{
    private readonly IServiceProvider serviceProvider;
    private readonly IMyService myService;

    public ContinuousMethods(
        IServiceProvider serviceProvider
       // IMyService myService // I wish it were Scoped
    )
    {
        this.serviceProvider = serviceProvider;
        //this.myService = myService;
    }

    public async Task SendEmail(
        [QueueTrigger("%AzureStorage:Queue:SendEmail%")] int emailId,
        ILogger logger
      //  IMyService myService // Why not...? But we don't have a binder for this right now.
    )
    {
        // Ugly workaround that I have to insert in all my functions.
        using (var scope = serviceProvider.CreateScope())
        using (var myService = scope.ServiceProvider.GetService<IMyService>())
        {
            await myService.SendEmailAsync(emailId);
        }
    }
}

Related information

WebJobs 3.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:19
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
piotrzbyszynskicommented, Jul 25, 2019

Any news on that? This issue is still present in 3.0.10 @fabiocav

4reactions
jsgoupilcommented, Apr 12, 2019

To anybody interested, I have opened a bug, since people who have answers about this matter don’t seem to be following this bug.

https://github.com/Azure/azure-webjobs-sdk/issues/2173

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebJobs 3.0 Dependency Injection AddScoped per ...
The goal is to do have scoped dependency per function. At the moment, this is the only work around found to fix this...
Read more >
Dependency injection in ASP.NET Core
The AddScoped method registers the service with a scoped lifetime, ... Each requested dependency in turn requests its own dependencies.
Read more >
Understanding AddTransient Vs AddScoped ...
In this article, you will learn about AddTransient Vs AddScoped Vs AddSingleton In ASP.NET Core.
Read more >
How To Setup Dependency Injection With Azure Functions ⚡
Azure Functions supports Dependency Injection pattern. ... in the Startup class, invoke the AddLogging method as shown below.
Read more >
Dependency Injection in .NET 6 - Service Lifetimes
Dependencies declared with the transient service lifetime will have a ... We declare a dependency as scoped using the AddScoped<T> method:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found