[英]How get async method value from non async method
我有一個名為GetDetails();
的async
方法; 從 API 獲取數據。
public async Task<string> GetDetails(string token, int tenantId, string fromDate,string searchText)
{
try
{
string serviceUrl = "http://localhost/Testapi/api/details/requestDetails/Details";
//API callimg code going here....
var response = await client.PostAsync(serviceUrl, content);
var result = await response.Content.ReadAsStringAsync();
client.Dispose();
return result;
}
}
我需要獲取上述async
方法數據。 所以我試着按如下方式做,
public string GetAllDetails(string token, int tenantId, string fromDate,string searchText)
{
var dataResult = GetDetails(token,tenantId,fromDate,searchText);
return dataResult;
}
但是我不能從非異步方法調用GetDetails
異步方法。 還有其他方法嗎? 我可以將GetAllDetails()
方法設為異步,因為它從 web 方法調用如下。
[WebMethod(EnableSession = true)]
public string GetDetails(string fromDate,string searchText)
{
try
{
SessionState session = new SessionState(Session);
DetialsConfiguration dc = new DetialsConfiguration();
string details = dc.GetAllDetails(session.JwtToken, session.ClientId,fromDate,searchText);
return details;
}
catch (Exception ex)
{
Logger.LogErrorEvent(ex);
throw;
}
}
如何獲取GetDetails()
API 響應數據到我的 web 方法? 請幫我做這個?
如何從非異步方法獲取異步方法值
你不會......除非你有一個非常具體的用例。 相反,您讓異步和等待模式傳播
public async Task<string> GetAllDetails(string token, int tenantId, string fromDate,string searchText)
{
var dataResult = await GetDetails(token,tenantId,fromDate,searchText);
return dataResult;
}
...
[WebMethod(EnableSession = true)]
public async Task<string> GetDetails(string fromDate,string searchText)
{
try
{
SessionState session = new SessionState(Session);
DetialsConfiguration dc = new DetialsConfiguration();
string details = await dc.GetAllDetails(session.JwtToken, session.ClientId,fromDate,searchText);
return details;
}
catch (Exception ex)
{
Logger.LogErrorEvent(ex);
throw;
}
}
另請注意,任何async
方法都應具有異步后綴,例如GetAllDetailsAsync
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.