Skip to content
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

Introduce AddKeyedAwsService to register keyed services #3561

Closed
1 of 2 tasks
OleksiiZuiev opened this issue Nov 28, 2024 · 5 comments
Closed
1 of 2 tasks

Introduce AddKeyedAwsService to register keyed services #3561

OleksiiZuiev opened this issue Nov 28, 2024 · 5 comments
Labels
feature-request A feature should be added or improved. module/sdk-custom p2 This is a standard priority issue pr/needs-review This PR needs a review from a Member. queued

Comments

@OleksiiZuiev
Copy link

OleksiiZuiev commented Nov 28, 2024

Describe the feature

Introduce AddKeyedAwsService which allows register multiple services of the same type with different keys. Then those services can be injected into other components using FromKeyedServices

Use case

I have a service which needs to access S3 buckets in different AWS regions. Different components of the service inject IAmazonS3 instances, but those have to be configured differently.

Proposed solution

It could be

AwsOptions optionsA = GetOptionsA();
AwsOptions optionsB = GetOptionsB();

var services = new ServiceCollection();
services.AddKeyedAwsService<IAmazonS3>("s3A", optionsA);
services.AddKeyedAwsService<IAmazonS3>("s3B", optionsB);

// register dependent components
services.AddTransient<DependentA>();
services.AddTransient<DependentB>();

public class DependentA(
   [FromKeyedServices("s3A")] IAmazonS3 s3Client)
{
}

public class DependentB(
   [FromKeyedServices("s3B")] IAmazonS3 s3Client)
{
}

Such design is aligned with keyed services functionality of Microsoft.Extensions.DependencyInjection:

Other Information

I could implement my own extension methods for my case if Amazon.Extensions.NETCore.Setup.ClientFactory was public.

I ended up with registering clents using

var region = "eu-central-1";
//...
services.AddKeyedSingleton<IAmazonS3>("s3Key", (_, _) => CreateS3Client(region));

private static IAmazonS3 CreateS3Client(string regionName)
    {
        var region = RegionEndpoint.GetBySystemName(regionName);
        var credentials = FallbackCredentialsFactory.GetCredentials();
        return new AmazonS3Client(credentials, region);
    }

but it feels hacky, and I assume can cause problems I don't see now.

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

AWS .NET SDK and/or Package version used

AWSSDK.Extensions.NETCore.Setup 3.7

Targeted .NET Platform

.NET 8.0

Operating System and version

MacOS Ventura

@OleksiiZuiev OleksiiZuiev added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Nov 28, 2024
@ashishdhingra
Copy link
Contributor

ashishdhingra commented Nov 29, 2024

Dependency injection into controllers in ASP.NET Core also gives an example of FromKeyedServices. It applies to targets specified here.

@OleksiiZuiev Feel free to contribute PR. Please target https://github.com/aws/aws-sdk-net/tree/v4-development branch. It's a new development branch for next major version of AWS SDK for .NET (which is in preview right now).

@ashishdhingra ashishdhingra added p2 This is a standard priority issue needs-review queued and removed needs-triage This issue or PR still needs to be triaged. needs-review labels Nov 29, 2024
@OleksiiZuiev
Copy link
Author

@ashishdhingra I have submitted a PR - #3570

@ashishdhingra
Copy link
Contributor

@ashishdhingra I have submitted a PR - #3570
@OleksiiZuiev Thanks for your PR contribution. .NET SDK team should be able to review it soon.

@ashishdhingra ashishdhingra added the pr/needs-review This PR needs a review from a Member. label Dec 9, 2024
@ashishdhingra
Copy link
Contributor

PR #3570 has been merged to v4-development branch. Closing this issue.

Copy link

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved. module/sdk-custom p2 This is a standard priority issue pr/needs-review This PR needs a review from a Member. queued
Projects
None yet
Development

No branches or pull requests

2 participants