Skip to content

Support keyed DI services in data portal parameter injection#4823

Merged
rockfordlhotka merged 4 commits intomainfrom
copilot/add-keyed-di-support
Feb 14, 2026
Merged

Support keyed DI services in data portal parameter injection#4823
rockfordlhotka merged 4 commits intomainfrom
copilot/add-keyed-di-support

Conversation

Copy link
Contributor

Copilot AI commented Feb 14, 2026

Description

Adds support for keyed service injection in data portal operations (.NET 8.0+). Developers can now specify a service key on the [Inject] attribute to retrieve keyed services registered with AddKeyed* methods.

Changes

  • InjectAttribute: Added Key property (type object?) to specify service key
  • ServiceProviderMethodInfo: Added ServiceKeys array to track keys per parameter, populated during PrepForInvocation()
  • ServiceProviderMethodCaller: Updated CallMethodTryAsync() to resolve keyed services:
    • Optional: Cast to IKeyedServiceProvider and call GetKeyedService(Type, object?)
    • Required: Use extension method GetRequiredKeyedService(Type, object?)
    • Keyed service logic gated by #if NET8_0_OR_GREATER
  • Tests: Added 6 tests covering keyed service scenarios with/without criteria, optional/required services

Usage

// Register keyed services
services.AddKeyedTransient<IEmailService, SendGridService>("sendgrid");
services.AddKeyedTransient<IEmailService, SmtpService>("smtp");

// Inject by key
[Create]
private void Create([Inject(Key = "sendgrid")] IEmailService emailService)
{
    // Gets SendGridService instance
}

[Fetch]
private void Fetch(int id, [Inject(Key = "smtp")] IEmailService emailService)
{
    // Gets SmtpService instance
}

// Optional keyed service
[Update]
private void Update([Inject(Key = "optional", AllowNull = true)] IService? service)
{
    // Returns null if key not registered
}

Fully backward compatible - existing [Inject] without keys work unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>Support keyed DI services in data portal parameter injection</issue_title>
<issue_description>It is now possible (in .NET 9 and higher) to register a DI service with the AddAsKeyed extension, and to then select a keyed service during injection with the use of the FromKeyedServices attribute.

We should look into supporting the FromKeyedServices attribute alongside the Inject attribute used for method parameter injection in the data portal. The goal being that a developer can use the Inject attribute on a data portal operation parameter like today, plus be able to indicate that the parameter should come from a keyed service by name.
</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] Support keyed DI services in data portal parameter injection Support keyed DI services in data portal parameter injection Feb 14, 2026
Copilot AI requested a review from rockfordlhotka February 14, 2026 23:43
@rockfordlhotka rockfordlhotka marked this pull request as ready for review February 14, 2026 23:44
@rockfordlhotka rockfordlhotka merged commit 89283c9 into main Feb 14, 2026
3 checks passed
@rockfordlhotka rockfordlhotka deleted the copilot/add-keyed-di-support branch February 14, 2026 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support keyed DI services in data portal parameter injection

2 participants