This is a beginners question. I have an API that is supposed to return a list of "MyContent". When I call the API from my test device it takes too long to load the byte array. It takes around 7 seconds on my iphone and 5 on the MVC app that I use for testing. I know that it is the byte array that is slowing everything down.
Below is my code. Is there anything I can do to my code that will speed things up? Other applications on my test device loads super fast so it should not be the bandwidth?
public class MyContent
{
public byte[] Content;
public string Name;
}
[Route("/GetPicturesFromBlobStorage")]
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> GetPicturesFromBlobStorageAsync([FromBody] List<string> filenames)
{
var files = new List<MyContent>();
// here I have a loop that fills the "files" property.
var str = JsonConvert.SerializeObject(files);
return Ok(str);
}
This is the Xamarin.iOS code:
var responseIng = await client.PostAsJsonAsync(url, files); // THIS IS THE LINE THAT TAKES FOREVER.
String urlContentsIng = await responseIng.Content.ReadAsStringAsync();
UPDATE:
I changed so that I do not use json:
public async Task<FileContentResult> GetPicturesFromBlobStorageAsync([FromForm] string filename)
{
var stopWatch5 = Stopwatch.StartNew();
AzureStorageConfig config = new AzureStorageConfig();
config.AccountName = "xxx";
config.AccountKey = "xx"
config.stagecontainer = "xx";
var sasToken =
_azureStorageBlobOptionsTokenGenerator.GenerateSasToken(
config.stagecontainer);
var storageCredentials =
new StorageCredentials(
sasToken);
var cloudStorageAccount =
new CloudStorageAccount(storageCredentials, config.AccountName, null, true);
var cloudBlobClient =
cloudStorageAccount.CreateCloudBlobClient();
var cloudBlobContainer =
cloudBlobClient.GetContainerReference(config.stagecontainer);
var blobName = filename;
var cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(blobName);
var stream = new MemoryStream();
await cloudBlockBlob.DownloadToStreamAsync(stream);
byte[] bytes = stream.ToArray();
var time = stopWatch5.Elapsed.TotalSeconds;
return File(bytes, "image/jpeg", filename);
}
And this is my client call to the API
var bag = new System.Collections.Concurrent.ConcurrentBag<object>();
var tasks = files.Select(async file =>
{
var multiFormCat = new MultipartFormDataContent();
multiFormCat.Add(new StringContent(file), "filename");
var responseIng = await client.PostAsync(url, multiFormCat);
var content = await responseIng.Content.ReadAsByteArrayAsync();
});
await Task.WhenAll(tasks);
It still takes around 4-5 seconds. Is there anything more I can do to improve performance?