Skip to content

MVC1004 should not warn for [FromServices] parameters #63888

@ascott18

Description

@ascott18

Is there an existing issue for this?

  • 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

    public class HomeController : Controller
    {
        [HttpGet]
        public async Task<ActionResult> 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    analyzerIndicates an issue which is related to analyzer experiencearea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions