Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IServiceScopeFactory.CreateScope() => NullReferenceException #83

Open
max-ieremenko opened this issue Sep 10, 2020 · 1 comment
Open
Labels
Requires Investigation 👮 Requires further verification

Comments

@max-ieremenko
Copy link

max-ieremenko commented Sep 10, 2020

Hi, i am trying to implement this suggestion "Do not capture services injected into the controllers on background" from microsoft docs.
It works with default dependecy injection mechanism, but when i switch to UnityServiceProvider the code throws exception:

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    private readonly IServiceScopeFactory _serviceScopeFactory;

    public TestController(IServiceScopeFactory serviceScopeFactory)
    {
        _serviceScopeFactory = serviceScopeFactory;
    }

    [HttpGet]
    public string Get()
    {
        Task.Run(DoSomething);

        return "hello";
    }

    private async Task DoSomething()
    {
        await Task.Delay(1000);

        // CreateScope, exception thrown: 'System.NullReferenceException' in Unity.Microsoft.DependencyInjection.dll
        using (var scope = _serviceScopeFactory.CreateScope())
        {
        }
    }
}

The unity container behind Unity.Microsoft.DependencyInjection.ServiceProvider (_serviceScopeFactory) is already disposed, because request is finished.
Is it a bug or how this example should look like?

@ENikS
Copy link
Contributor

ENikS commented Sep 11, 2020

Might be related to this issue

@ENikS ENikS added the Requires Investigation 👮 Requires further verification label Sep 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Requires Investigation 👮 Requires further verification
Projects
None yet
Development

No branches or pull requests

2 participants