作者:我就是个2丶 | 来源:互联网 | 2022-12-06 13:48
1> Rajmond Burg..:
您可以IActionResult
使用以下任务包装Task
和await
执行.
public async Task post()
{
var authtoken = await _salesForceService.GetTokenForSalesForce();
var respOnse= await _salesForceService.GetOrderCountFromSalesForce(authtoken);
DashboardModel dashboardModel = //fill the object
return Ok(dashboardModel);
}
根据我的理解,至少这是你要求的,如果它的另一个问题让我知道.
编辑1:
这只是我的建议/意见.
就个人而言,我真的不喜欢将代码包装在try-catch中,这样代码就很难阅读和维护.你真的应该考虑在一个地方集中异常处理,你可以有一个基本控制器或只有这样的中间件:
public class ErrorHandlingMiddleware
{
private readonly RequestDelegate _next;
public ErrorHandlingMiddleware(RequestDelegate next)
{
this._next = next;
}
public async Task Invoke(HttpContext context, ILogger logger)
{
try
{
await _next(context);
}
catch (Exception ex)
{
await HandleExceptionAsync(context, ex, logger);
}
}
private static Task HandleExceptionAsync(HttpContext context, Exception exception, ILogger logger)
{
logger.Log(exception);
//do something
return context.Response.WriteAsync(... something ...); //Maybe some JSON message or something
}
}
您只需在Configure
下面的方法中将其注册为中间件:
app.UseMiddleware();