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

Open
1 of 2 tasks
OleksiiZuiev opened this issue Nov 28, 2024 · 1 comment
Open
1 of 2 tasks

Introduce AddKeyedAwsService to register keyed services #3561

OleksiiZuiev opened this issue Nov 28, 2024 · 1 comment
Labels
feature-request A feature should be added or improved. module/sdk-custom needs-review p2 This is a standard priority issue

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

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

@ashishdhingra ashishdhingra added p2 This is a standard priority issue needs-review and removed needs-triage This issue or PR still needs to be triaged. labels Nov 29, 2024
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 needs-review p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

2 participants