有人可以请另一只眼睛吗?我试图在显示之前验证斑点图像是否存在。这可能需要1-4秒。 我的JS看起来像这样:
var url = '/api/blob/ValidateBlobExists?id=' + blobImage;
$.getJSON(url,
function(json) {
console.log("success");
})
.done(function () {
console.log("second success");
exists = data;
if (exists) {
console.log("exists'");
$('#imgPhotograph').hide().attr('src', blobImage).fadeIn();
} else {
$('#imgPhotograph').attr('src', '../Images/NoPhotoFound.jpg');
}
});
api看起来像这样..请不要判断..它的vb,因为它必须是。
Public Function ValidateBlobExists(id As String) As JsonResult(Of Boolean)
dim result = CDNHelper.BlobExists(id) 'this could take ~5 seconds
Return Json(Of Boolean)(result)
End Function
底层方法如下:
public static bool BlobExists(string filename)
{
try
{
var sw = new Stopwatch();
sw.Start();
do
{
if (client.AssertBlobExists(filename).Result) // <-- this is a wrapper to query the azure blob
return true;
System.Threading.Thread.Sleep(500); //no reason to hammer the service
} while (sw.ElapsedMilliseconds < 8000);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
return false;
}
问题出在我的控制台输出中,我什至无法获得“成功”。似乎getJson()只是不愿意等待8秒钟过去再继续。任何想法都值得赞赏。
答案 0 :(得分:0)
问题是我的API响应签名未装饰为“异步”。即使我打电话给.result,它也变得混乱了。对我来说,解决方案是按以下方式修改我的代码
Public Async Function ValidateBlobExists(id As String) As Task(Of JsonResult(Of Boolean))
result = await CDNHelper.BlobExists(id, "ioc")
return result
End function
和
public static async Task<bool> BlobExists()
{
return client postasync wrapper
}