Skip to content

Support Keyed Services in MVC #50145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

benjaminpetit
Copy link
Member

@benjaminpetit benjaminpetit commented Aug 17, 2023

Description

This PR adds support to resolve keyed services from dependency injection in MVC.

Fixes #49634

Customer Impact

Support for keyed services was added to the runtime in .NET 8 Preview 7. This change allows customers to leverage keyed services in MVC and contributes to a more complete user experience.

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Low risk, because the change is additive and doesn't affect pre-existing codepaths around parameter binding.

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

@benjaminpetit benjaminpetit added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Aug 17, 2023
@benjaminpetit benjaminpetit requested a review from a team as a code owner August 17, 2023 15:37
!context.Metadata.ModelType.IsValueType &&
context.Metadata.NullabilityState == NullabilityState.Unknown);

var attribute = context.Metadata.Identity.ParameterInfo?.GetCustomAttribute<FromKeyedServicesAttribute>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to get the key when constructing the BindingInfo, but it will require adding some properties in several places.

@danmoseley
Copy link
Member

OK, please mark servicing-consider when ready and email tactics for approval for rc1.

@benjaminpetit
Copy link
Member Author

I opened an issue to track the api change needed here: #49634

@benjaminpetit benjaminpetit requested a review from halter73 August 17, 2023 20:39
[HttpGet("GetBoth")]
public ActionResult<string> GetBoth(
[FromKeyedServices("ok_service")] ICustomService s1,
[FromKeyedServices("not_ok_service")] ICustomService s2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any tests for optional and required services when the service is missing?

// Keyed services
if (attributes.Any(a => typeof(IFromServiceMetadata).IsAssignableFrom(a.GetType())))
{
if (attributes.OfType<FromKeyedServicesAttribute>().FirstOrDefault() is not null)
Copy link
Member

@halter73 halter73 Aug 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not combine these into a single conditional?

And why not support both and treat it the same as if the [FromServices] wasn't there? Regardless, we should add a test with both [FromServices] and [FromKeyedServices].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And why not support both and treat it the same as if the [FromServices] wasn't there? Regardless, we should add a test with both [FromServices] and [FromKeyedServices].

This came up as a topic of discussion in the minimal API PR (see here). Overall, I think throwing an exception that informs users that the two attributes are mutually exclusive is a clearer experience then figuring out what sort of implicit behavior we have here.

@benjaminpetit
Copy link
Member Author

Build is failing because

error : Detected modification to baseline API files. PublicAPI.Shipped.txt files should only be updated after a major release. See /docs/APIBaselines.md for more information

But I didn't changed PublicAPI.Shipped.txt, only PublicAPI.Unshipped.txt. Am I missing something?

@@ -1 +1,3 @@
#nullable enable
Microsoft.AspNetCore.Mvc.ModelBinding.BindingInfo.ServiceKey.get -> object?
Microsoft.AspNetCore.Mvc.ModelBinding.BindingInfo.ServiceKey.set -> void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjaminpetit here's the modification to the API file that the build is complaining about. Normally we don't change these files in release branches, but since this is the RC1 branch it's fine. Ping me or a manager when this is approved & CI failures are down to just this, and we can force-merge it for you (note I'll be OOF for a week after about 3:30 PST today)

@wtgodbe
Copy link
Member

wtgodbe commented Aug 18, 2023

But I didn't changed PublicAPI.Shipped.txt, only PublicAPI.Unshipped.txt. Am I missing something?

#50145

@adityamandaleeka
Copy link
Member

adityamandaleeka commented Aug 19, 2023

Approved over email. Looks like the remaining error is about an extra using directive. Once that's fixed I guess we can merge it @benjaminpetit

@adityamandaleeka adityamandaleeka added the Servicing-approved Shiproom has approved the issue label Aug 19, 2023
@ghost
Copy link

ghost commented Aug 19, 2023

Hi @benjaminpetit. This PR was just approved to be included in the upcoming servicing release. Somebody from the @dotnet/aspnet-build team will get it merged when the branches are open. Until then, please make sure all the CI checks pass and the PR is reviewed.

@benjaminpetit benjaminpetit force-pushed the feature/keyed-di-rebase branch from dd131fc to b85e637 Compare August 21, 2023 09:11
@benjaminpetit benjaminpetit force-pushed the feature/keyed-di-rebase branch from b85e637 to 397abeb Compare August 21, 2023 10:18
@benjaminpetit
Copy link
Member Author

Ready to be merged, the only failure is because I added an API.

@adityamandaleeka adityamandaleeka merged commit 81fe483 into dotnet:release/8.0-rc1 Aug 21, 2023
@ghost ghost added this to the 8.0-rc1 milestone Aug 21, 2023
@BrennanConroy BrennanConroy added the blog-candidate Consider mentioning this in the release blog post label Aug 21, 2023
@ghost
Copy link

ghost commented Aug 21, 2023

@benjaminpetit, this change will be considered for inclusion in the blog post for the release it'll ship in. Nice work!

Please ensure that the original comment in this thread contains a clear explanation of what the change does, why it's important (what problem does it solve?), and, if relevant, include things like code samples and/or performance numbers.

This content may not be exactly what goes into the blog post, but it will help the team putting together the announcement.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates blog-candidate Consider mentioning this in the release blog post Servicing-approved Shiproom has approved the issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants