如何从MVC控制器操作调用asyc方法
本文关键字:调用 asyc 方法 操作 控制器 MVC | 更新日期: 2023-09-27 18:29:51
我想调用一个异步方法,该方法在Controller中的操作方法中返回一个列表。但是Action方法不是异步方法。我如何修改它,以便在操作中调用异步方法?
public ActionResult AdvisorsMapCompleted()
{
List<AdvisorMapInfo> infos = await AdvisorsMapBL.getAdvisorsAsync();
//task.Wait();
}
public static async Task<List<AdvisorMapInfo>> getAdvisorsAsync()
{
var auth = new AuthenticationClient();
//Authenticate with Salesforce
var url = IsSandboxUser.Equals("true", StringComparison.CurrentCultureIgnoreCase)
? "https://test.salesforce.com/services/oauth2/token"
: "https://login.salesforce.com/services/oauth2/token";
await auth.UsernamePasswordAsync(ConsumerKey, ConsumerSecret, Username, Password, url);
var client = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion);
const string qry = "SELECT Name,Primary_Contact__c,Asset_Range_Lower__c,Asset_Range_Upper__c,BillingAddress FROM Account WHERE (Account_Type__c='Advisor' or Account_Type__c='provider')";
var accts = new List<AdvisorMapInfo>();
var results = await client.QueryAsync<AdvisorMapInfo>(qry);
var totalSize = results.totalSize;
accts.AddRange(results.records);
var nextRecordsUrl = results.nextRecordsUrl;
if (!String.IsNullOrEmpty(nextRecordsUrl))
{
while (true)
{
var continuationResults = await client.QueryContinuationAsync<AdvisorMapInfo>(nextRecordsUrl);
totalSize = continuationResults.totalSize;
accts.AddRange(continuationResults.records);
if (string.IsNullOrEmpty(continuationResults.nextRecordsUrl)) break;
nextRecordsUrl = continuationResults.nextRecordsUrl;
}
}
return accts;
}
尝试
public async Task<ActionResult> AdvisorsMapCompleted()
{
List<AdvisorMapInfo> infos = await AdvisorsMapBL.getAdvisorsAsync();
//task.Wait();
}
如果您将控制器操作方法的签名更改为上面的,那么就可以了。