dotnet / dotnet/aspnetcore

MVC1004 should not warn for [FromServices] parameters

Open
#63,888 0 comments 0 reactions 0 assignees View on GitHub
analyzer area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

MVC1004 is warning for parameters that are injected from services.

Image

Image

### Expected Behavior

MVC1004 does not warn for any BindingSource that is `isFromRequest: false`, or at least not `BindingSource.Services`.

It should probably also not warn for getter-only properties that will never be bound.

### Steps To Reproduce

```c#
public class HomeController : Controller
{
[HttpGet]
public async Task Get([FromServices] SiteSettingsService settings) { /* stuff */ }
^^^^^^^^ MVC1004
}

public class SiteSettings { }
public class SiteSettingsService(AppDbContext db)
{
private SiteSettings _settings;
public SiteSettings Settings => _settings ??= db.SiteSettings.First();

// ... other unrelated stuff
}
```

### Exceptions (if any)

_No response_

### .NET Version

10.0.100-rc.1.25451.107

### Anything else?

Tried TargetFramework both net8.0 and net10.0 (10.0.100-rc.1.25451.107)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.