Is it bad practice to mix async and sync call in same ASP.NET core API call?
For example, in following code method CropBlackBroderOfAnImageAsync
is an async method.
On the other hand SaveImageForProcessing(file, sourceFolderPath);
is a sync method.
The reason I am calling SaveImageForProcessing
synchronously is
that I want use the result of it to execute the code in CropBlackBroderOfAnImageAsync
.
public async Task<(string sourceFolderPath, string destinationFolderPath)> CropBlackBorderOfAnImage(IFormFile file)
{
var extension = Path.GetExtension(file.FileName);
var newFileName = Guid.NewGuid().ToString();//Create a new Name for the file due to security reasons.
var fileNameSource = newFileName + extension;
var sourceFolderPath = Path.Combine(Directory.GetCurrentDirectory(), "Images\\Source", fileNameSource);
var fileNameDestination = newFileName + "Result" + extension;
var destinationFolderPath = Path.Combine(Directory.GetCurrentDirectory(), "Images\\Destination", fileNameDestination);
SaveImageForProcessing(file, sourceFolderPath);
await _imageCropBlackBroderService.CropBlackBroderOfAnImageAsync(sourceFolderPath, destinationFolderPath);
return (sourceFolderPath, destinationFolderPath);
}
private void SaveImageForProcessing(IFormFile file, string path)
{
using (var bits = new FileStream(path, FileMode.Create))
{
file.CopyTo(bits);
}
}
Path.Combine
which is great, but then you hardcode the path delimiter\\
. TheImages
andSource
names should be their own distinct parameters. \$\endgroup\$