Reputation: 34038
I have created a webapi get method to return images with a blob storage URL.
But I need to handle exceptions as well, how should I do it in this case? I get an error in the last line of the catch
[HttpGet]
public async Task<HttpResponseMessage> GetProfileImage(string url)
{
var telemetry = new TelemetryClient();
try
{
//Initalize configuration settings
var accountName = ConfigurationManager.AppSettings["storage:account:name"];
var accountKey = ConfigurationManager.AppSettings["storage:account:key"];
var profilepicturecontainername = ConfigurationManager.AppSettings["storage:account:profilepicscontainername"];
//Instance objects needed to store the files
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer imagesContainer = blobClient.GetContainerReference(profilepicturecontainername);
//Gets blob reference
var cloudBlob = await blobClient.GetBlobReferenceFromServerAsync(new Uri(url));
//Check if it exists
var cloudBlobExists = await cloudBlob.ExistsAsync();
//Opens memory stream
using (MemoryStream ms = new MemoryStream())
{
cloudBlob.DownloadToStream(ms);
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new ByteArrayContent(ms.ToArray());
response.Content.LoadIntoBufferAsync(b.Length).Wait();
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return response;
}
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
Upvotes: 0
Views: 5328
Reputation: 840
try this
[HttpGet]
public async Task<IActionResult> GetProfileImage(string url)
{
var telemetry = new TelemetryClient();
try
{
//Initalize configuration settings
var accountName = ConfigurationManager.AppSettings["storage:account:name"];
var accountKey = ConfigurationManager.AppSettings["storage:account:key"];
var profilepicturecontainername = ConfigurationManager.AppSettings["storage:account:profilepicscontainername"];
//Instance objects needed to store the files
var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer imagesContainer = blobClient.GetContainerReference(profilepicturecontainername);
//Gets blob reference
var cloudBlob = await blobClient.GetBlobReferenceFromServerAsync(new Uri(url));
//Check if it exists
var cloudBlobExists = await cloudBlob.ExistsAsync();
//Opens memory stream
using (MemoryStream ms = new MemoryStream())
{
cloudBlob.DownloadToStream(ms);
return File(ms, "application/octet-stream");
}
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
Upvotes: 1