[英]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.