Microsoft.CodeAnalysis.FxCopAnalyzers
v2.9.5
Example Dependency Injected Service:
public interface ICategoryService
{
Task<IEnumerable<MyItems>> GetMyItemsAsync(FilterValues filters);
}
internal sealed class CategoryService : ICategoryService
{
#region Internal State
private readonly ICategoryRepository categoryRepository;
#endregion
public CategoryService(ICategoryRepository categoryRepository)
{
this.categoryRepository = categoryRepository ?? throw new ArgumentNullException(nameof(categoryRepository));
}
#region ICategoryService Implementation
public Task<IEnumerable<MyItems>> GetMyItemsAsync(FilterValues filters)
=> categoryRepository.GetMyItemsAsync(filters);
#endregion
}
public static class InfrastructureDIConfig
{
public static void AddInfrastructureServices(this IServiceCollection services)
{
services.TryAddScoped<ICategoryRepository, CategoryRepository>();
services.TryAddScoped<ICategoryService, CategoryService>();
}
}
CA1812 should not fire when the internal class has been registered for DI.
CA1812 falsely fires and suggests CategoryService should be removed.
_Migrated this issue from:
https://github.com/MicrosoftDocs/visualstudio-docs/issues/4028
as the somewhat unhelpful response I received there makes me think there isn't currently a way to suppress the CA1812 warning for only classes used via DI and not the whole assembly._
I'm running into this all the time in a similar scenario. I have a generic factory where I register types (much like you do in a DI container) and I have to suppress the bogus warning on many internal types due to this.
To add to this, the reason I think this is 100% a false positive is that the type name in question _appears in the source code_ as part of a type expression, so clearly it _is_ used.
Bump.
I don't mind a false warning if I can suppress it. But I can't easily suppress this one, except for the whole assembly as @ideoclickVanessa says.
https://github.com/dotnet/roslyn-analyzers/issues/3210#issuecomment-647495727
It's because of annoyances like this that I'm sorely tempted to give up on FxCopAnalyzers altogether. I've learnt useful lessons from it, but these days it's more hindrance and annoyance than help.
BTW you can definitely suppress this code analysis warning for just the 1 affected line, but VS doesn't offer that for the reasons mentioned in https://github.com/dotnet/roslyn-analyzers/issues/3210#issuecomment-647540527.
Here's what I do:
```c#
internal class BlazorHybridJSRuntime : JSRuntime
...
```
I confirm that with version 5.0.1 and 6.0.0-preview1.20609.1 it is still not possible to suppress the issue.
@mavasani I am right to assume that the Suppress or Configure issues menu for a rule is handled on roslyn's side? I think that's the real issue behind this ticket.
See https://github.com/dotnet/roslyn-analyzers/issues/3210#issuecomment-747455459
@Evangelink Lets close this as duplicate of the Roslyn feature request that you cited in https://github.com/dotnet/roslyn-analyzers/issues/3210#issuecomment-747455459
Most helpful comment
To add to this, the reason I think this is 100% a false positive is that the type name in question _appears in the source code_ as part of a type expression, so clearly it _is_ used.