在非异步 asp.net 页面中调用异步方法
Call an asynchronous method in a non-asynchronous asp.net page
我的单个 asp.net 应用程序中存在两个不同端点的问题。基本上,问题是其中一个端点不允许在页面中使用异步方法,而另一个端点允许。如果我 运行 应用程序的一个端点会要求我有一个异步 asp.net 页面,但另一个端点会崩溃,反之亦然。
public async Task<AirtableListRecordsResponse> RetrieveRecord()
{
string MyProductID = ProductID;
string baseId = "00000000000xxxx";
string appKey = "00000000000xxxx";
var records = new List<AirtableRecord>();
using (AirtableBase airtableBase = new AirtableBase(appKey, baseId))
{
Task<AirtableListRecordsResponse> task = airtableBase.ListRecords(tableName: "efls", filterByFormula: ProductID);
AirtableListRecordsResponse response = await task;
if (!response.Success)
{
string errorMessage = null;
if (response.AirtableApiError is AirtableApiException)
{
errorMessage = response.AirtableApiError.ErrorMessage;
}
else
{
errorMessage = "Unknown error";
}
// Report errorMessage
}
else
{
records.AddRange(response.Records.ToList());
var record = response.Records;
//offset = response.Offset;
//var record = response.Record;
foreach (var item in record)
{
foreach (var Fields in item.Fields)
{
if (Fields.Key == "pdfUrl")
{
string link = Fields.Value.ToString();
MyLink = Fields.Value.ToString();
}
}
}
// Do something with your retrieved record.
// Such as getting the attachmentList of the record if you
// know the Attachment field name
//var attachmentList = response.Record.GetAttachmentField(YOUR_ATTACHMENT_FIELD_NAME);
}
return response;
}
}
这是请求异步页面的异步方法,另一种包含强大的结构,不能以任何理由更改。有什么办法可以让他们一起工作吗?
顺便说下我用的是airtable.comapi
提前致谢。
使用Wait on Task,可以使用同步方式
Task<AirtableListRecordsResponse> task = Task.Run(() => airtableBase.ListRecords(tableName: "efls", filterByFormula: ProductID));
task.Wait();
AirtableListRecordsResponse response = task.Result;
只有当你不能使用异步方法时才使用它。
如 msdn 博客所述,此方法完全没有死锁 -
https://blogs.msdn.microsoft.com/jpsanders/2017/08/28/asp-net-do-not-use-task-result-in-main-context/
我自己解决了,
我找到的解决方案如下:
当页面使用两个不同的端点并且其中之一要求页面异步时,最好的解决方案是将过程分成两个不同的部分 and/or 页面,其中一个将调用异步方法并在不异步的情况下检索信息和其他作品。
如何在站点之间传递信息?
使用会话变量,有些端点只需要显示简单数据,如本例所示,因此会话变量将在非第 2 页中调用- 异步页面。
这是一个简单但有效的解决方案。
非常感谢大家的回答。
我的单个 asp.net 应用程序中存在两个不同端点的问题。基本上,问题是其中一个端点不允许在页面中使用异步方法,而另一个端点允许。如果我 运行 应用程序的一个端点会要求我有一个异步 asp.net 页面,但另一个端点会崩溃,反之亦然。
public async Task<AirtableListRecordsResponse> RetrieveRecord()
{
string MyProductID = ProductID;
string baseId = "00000000000xxxx";
string appKey = "00000000000xxxx";
var records = new List<AirtableRecord>();
using (AirtableBase airtableBase = new AirtableBase(appKey, baseId))
{
Task<AirtableListRecordsResponse> task = airtableBase.ListRecords(tableName: "efls", filterByFormula: ProductID);
AirtableListRecordsResponse response = await task;
if (!response.Success)
{
string errorMessage = null;
if (response.AirtableApiError is AirtableApiException)
{
errorMessage = response.AirtableApiError.ErrorMessage;
}
else
{
errorMessage = "Unknown error";
}
// Report errorMessage
}
else
{
records.AddRange(response.Records.ToList());
var record = response.Records;
//offset = response.Offset;
//var record = response.Record;
foreach (var item in record)
{
foreach (var Fields in item.Fields)
{
if (Fields.Key == "pdfUrl")
{
string link = Fields.Value.ToString();
MyLink = Fields.Value.ToString();
}
}
}
// Do something with your retrieved record.
// Such as getting the attachmentList of the record if you
// know the Attachment field name
//var attachmentList = response.Record.GetAttachmentField(YOUR_ATTACHMENT_FIELD_NAME);
}
return response;
}
}
这是请求异步页面的异步方法,另一种包含强大的结构,不能以任何理由更改。有什么办法可以让他们一起工作吗?
顺便说下我用的是airtable.comapi
提前致谢。
使用Wait on Task,可以使用同步方式
Task<AirtableListRecordsResponse> task = Task.Run(() => airtableBase.ListRecords(tableName: "efls", filterByFormula: ProductID));
task.Wait();
AirtableListRecordsResponse response = task.Result;
只有当你不能使用异步方法时才使用它。
如 msdn 博客所述,此方法完全没有死锁 - https://blogs.msdn.microsoft.com/jpsanders/2017/08/28/asp-net-do-not-use-task-result-in-main-context/
我自己解决了,
我找到的解决方案如下:
当页面使用两个不同的端点并且其中之一要求页面异步时,最好的解决方案是将过程分成两个不同的部分 and/or 页面,其中一个将调用异步方法并在不异步的情况下检索信息和其他作品。
如何在站点之间传递信息?
使用会话变量,有些端点只需要显示简单数据,如本例所示,因此会话变量将在非第 2 页中调用- 异步页面。
这是一个简单但有效的解决方案。
非常感谢大家的回答。