I have created a AwsS3Service which is used to upload files to AWS S3, and I am creating an AmazonS3Client from 'AWSSDK.S3' nuget package inside the constructor. I am also implementing the IDisposable interface for my custom service, and disposing the AmazonS3Client client inside of it.

My question is, if I register AwsS3Service as a transient service, does AmazonS3Client gets disposed after each time I request my service? If not, could you please suggest a better way to implement this. I don't want the AmazonS3Client to be hanging around in the memory.

IAwsS3Service interface

public interface IAwsS3Service : IDisposable
{
    public Task<bool> UploadFileAsync(IFormFile file, string fileName, string path);
}

AwsS3Service

public class AwsS3Service : IAwsS3Service
{
    private readonly IAmazonS3 _client;
        
    public AwsS3Service()
    {
        _client = new AmazonS3Client(/* secret and access keys are read from appsettings and passed */);
    }

    public async Task<bool> UploadFileAsync(IFormFile file, string fileName, string awsPath)
    {
       // uploading logic..
    }

    public void Dispose()
    {
        _client?.Dispose();
    }
}


startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IAwsS3Service, AwsS3Service>();
}

0

There are 0 best solutions below