Skip to content

MinioClient Injection Logic with ServiceLifetime Can Be Confusing #1268

@gxrsprite

Description

@gxrsprite

I’ve been reviewing the code for dependency injection of MinioClient in the minio-dotnet SDK, and I noticed some potential confusion in the way the client is being injected based on the ServiceLifetime provided.

In the code:
https://github.com/minio/minio-dotnet/blob/master/Minio/ServiceCollectionExtensions.cs

var client = minioClientFactory.CreateClient();
client.Config.ServiceProvider = services.BuildServiceProvider();
switch (lifetime)
{
    case ServiceLifetime.Singleton:
	services.TryAddSingleton(_ => client);
	break;
    case ServiceLifetime.Scoped:
	services.TryAddScoped(_ => client);
	break;
    case ServiceLifetime.Transient:
	services.TryAddTransient(_ => client);
	break;
}

The issue is that the client is being created first and then injected as a singleton, scoped, or transient instance based on the lifetime provided. However, it seems like the code is always injecting the same instance of MinioClient, which makes the ServiceLifetime setting effectively irrelevant.

whether it's recommended to use MinioClient as a Singleton or as a Transient/Scoped instance in a typical application?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions