You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
CA2000 does not trigger when creating an instance of Microsoft.Extensions.DependencyInjection.ServiceProvider, even though ServiceProvider implements IDisposable.
Steps To Reproduce
Create new csproj file with the following content:
Create new .editorconfig file in the same directory as the csproj file.
Configure CA2000 as warning in .editorconfig:
[*]
dotnet_diagnostic.CA2000.severity = warning
Add the following class in a new file:
public class MyClass
{
public void MyMethod()
{
var serviceCollection = new ServiceCollection();
var serviceProvider = serviceCollection.BuildServiceProvider();
}
}
Expected behavior
CA2000 suggests to add the missing using-statement, i.e.:
using var serviceProvider = serviceCollection.BuildServiceProvider();
Actual behavior
CA2000 does not trigger.
The text was updated successfully, but these errors were encountered:
I figured the cause of this issue is the same as mentioned here: #5330 (comment)
Basically, the instance of ServiceProvider is created in a referenced assembly, and CA2000 does not recognize the ownership of the IDisposable as being transferred to our code.
I see the following options as ways to solve this:
Enhance IsDisposableCreationSpecialCase so it also considers methods starting with "build".
Create option for assuming that ownership of any IDisposable is transferred when it is returned from a method. This can potentially give a lot of noise for consumers. See Allow end-users to configure dispose ownership transfer #1587 for related options.
Analyzer
Diagnostic ID: CA2000:
Dispose objects before losing scope
Analyzer source
SDK: Built-in CA analyzers in .NET 5 SDK or later
Version: SDK 6.0.400
Describe the bug
CA2000 does not trigger when creating an instance of
Microsoft.Extensions.DependencyInjection.ServiceProvider
, even thoughServiceProvider
implementsIDisposable
.Steps To Reproduce
Create new
csproj
file with the following content:Create new
.editorconfig
file in the same directory as thecsproj
file.Configure CA2000 as warning in
.editorconfig
:Add the following class in a new file:
Expected behavior
CA2000 suggests to add the missing using-statement, i.e.:
Actual behavior
CA2000 does not trigger.
The text was updated successfully, but these errors were encountered: