ASP.NET MVC应用程序中有三个层。从第一层开始,我在第二层调用方法,方法调用第三层,我调用Web服务。下面是代码。层(2和3)都在解决方案中添加为Class Library
。
namespace Web.Controllers // Layer 1
{
using Web.Services;
// other usings...
public class Controller1
{
[HttpPost]
public JsonResult Search(SomeObject request)
{
Service service = new Service();
var result = service.Search(request).Result;
}
}
}
namespace Web.Service // Layer 2
{
using Web.Library;
// other usings...
public class Service
{
public async Task<SomeType> SearchFlights(SomeObject requestModel)
{
SomeObjectReturn result = new SomeObjectReturn();
Library library = new Library();
var result = await library.Search(requestModel);
return result;
}
}
}
namespace Web.Library // Layer 3
{
public class Library
{
public async Task<SomeObjectReturn> Search(SomeObject request)
{
// here I call Sabre service to get the result...
SomeObjectReturn obj = new SomeObjectReturn();
RestClient restClient = RestClientFactory.Create();
IActivity activity = new InstaFlightsActivity(restClient, requestModel);
Sabre.Library.Workflow.Workflow workflow = new Sabre.Library.Workflow.Workflow(activity);
SharedContext sharedContext = await workflow.RunAsync();
// map sharedContext to SomeObjectReturn
return obj;
}
}
}
现在我不知道为什么await workflow.RunAsync
上存在死锁。我还在.ConfigureAwait(false)
上尝试了workflow.RunAsync
。但无论如何都会产生僵局。我不知道代码有什么问题。
顺便说一下,我在Controller
中进行了如下更改,我得到了结果。
public async Task<JsonResult> Search(SomeObject request) {...
而不是上面。
答案 0 :(得分:1)
Result
的调用会产生死锁。一旦你有一个异步API,你应该使用async / await,否则可能会出现死锁。因此,如果您更改下面的控制器代码,问题就会消失。
[HttpPost]
public async Task<JsonResult> Search(SomeObject request)
{
Service service = new Service();
var result = await service.Search(request);
}