DI005

Use `CreateAsyncScope` in Async Methods

CreateScope() used in async flows where async disposal is needed and CreateAsyncScope() is available, including async methods, lambdas, local functions, anonymous methods, and top-level programs that use await. Detection covers regular member access (_scopeFactory.CreateScope()), parameterless IServiceScope CreateScope() methods on concrete IServiceScopeFactory implementations, and conditional-access receivers (_scopeFactory?.CreateScope(), _provider?.CreateScope()) alike.

Warning Default severity · Code fix: Yes

Why it matters

Async disposables (IAsyncDisposable) may not be cleaned up correctly with sync disposal patterns.

If a machine needs a proper shutdown button, pulling the plug is not enough.

README problem example

public async Task RunAsync()
{
    using var scope = _scopeFactory.CreateScope();
    var service = scope.ServiceProvider.GetRequiredService<IMyService>();
    await service.ExecuteAsync();
}

README better pattern

public async Task RunAsync()
{
    await using var scope = _scopeFactory.CreateAsyncScope();
    var service = scope.ServiceProvider.GetRequiredService<IMyService>();
    await service.ExecuteAsync();
}

Yes. Rewrites safe using scope creation/disposal patterns to await using plus CreateAsyncScope(), including explicit IServiceScope declarations that must become var for AsyncServiceScope.

Repo sample extraction

Examples pulled from the sample app

Open full sample file

Sample app warning case

    public async Task Bad_CreateScopeInAsyncMethod()
    {
        // DI005: Use 'CreateAsyncScope' instead of 'CreateScope' in async method
        using var scope = _scopeFactory.CreateScope();
        var service = scope.ServiceProvider.GetRequiredService<IScopedService>();
        await Task.Delay(100);
        service.DoWork();
    }

Sample app safe pattern

    public async Task Good_CreateAsyncScope()
    {
#pragma warning disable DI007 // Intentional use of IServiceProvider within a valid scope
        await using var scope = _scopeFactory.CreateAsyncScope();
        var service = scope.ServiceProvider.GetRequiredService<IScopedService>();
        await Task.Delay(100);
        service.DoWork();
#pragma warning restore DI007
    }

Nearby diagnostics

Other rules in this family

All 37 rules