Thursday, February 6, 2020

ASP.NET Core Dependency Injection with AWS

This is my second post regarding the AWS Hope toy like my first post.

The AWSSDK.Extensions.NETCore.Setup NuGet package also integrates with a new dependency injection system in ASP.NET Core. The ConfigureServices method in Startup is where the MVC services are added. If the application is using Entity Framework, this is also where that is initialized.
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); }
The AWSSDK.Extensions.NETCore.Setup NuGet package adds new extension methods to IServiceCollection that you can use to add AWS services to the dependency injection. The following code shows how to add the AWS options that are read from IConfiguration to add Amazon S3 and DynamoDB to our list of services.
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); services.AddDefaultAWSOptions(Configuration.GetAWSOptions()); services.AddAWSService<IAmazonS3>(); services.AddAWSService<IAmazonDynamoDB>(); }
Now, if your MVC controllers use either IAmazonS3 or IAmazonDynamoDB as parameters in their constructors, the dependency injection system passes in those services.
public class HomeController : Controller { IAmazonS3 S3Client { get; set; } public HomeController(IAmazonS3 s3Client) { this.S3Client = s3Client; } ... }

Thank you Hope it will help will help you.



No comments:

Post a Comment