I would like to know if it is possible to remove the await from this method so as not to have an asynchronous method.
public static async Task Run(IMinioClient minio,
string bucketName = "my-bucket-name",
string prefix = null,
bool recursive = true,
bool versions = false)
{
try
{
Console.WriteLine("Running example for API: ListObjectsAsync");
var listArgs = new ListObjectsArgs()
.WithBucket(bucketName)
.WithPrefix(prefix)
.WithRecursive(recursive)
.WithVersions(versions);
await foreach (var item in minio.ListObjectsEnumAsync(listArgs).ConfigureAwait(false))
Console.WriteLine($"Object: {item.Key}");
Console.WriteLine($"Listed all objects in bucket {bucketName}\n");
}
catch (Exception e)
{
Console.WriteLine($"[Bucket] Exception: {e}");
}
}
https://github.com/minio/minio-dotnet/blob/master/Minio.Examples/Cases/ListObjects.cs
Thank you for your help.
Usually I do a .Result to do this but here the await is done on a foreach
You could use the .NET 7 API ToBlockingEnumerable
:
Converts an
IAsyncEnumerable<T>
instance into anIEnumerable<T>
that enumerates elements in a blocking manner.
Usage example:
public static void Run(IMinioClient minio,
string bucketName = "my-bucket-name",
string prefix = null,
bool recursive = true,
bool versions = false)
{
var listArgs = new ListObjectsArgs()
.WithBucket(bucketName)
.WithPrefix(prefix)
.WithRecursive(recursive)
.WithVersions(versions);
foreach (var item in minio.ListObjectsEnumAsync(listArgs).ToBlockingEnumerable())
{
Console.WriteLine($"Object: {item.Key}");
}
}
This solution is susceptible to deadlocks, depending on the implementation of the ListObjectsEnumAsync
method. Blocking on async code is inadvisable in general.