背景
目前正在创建一个web,它需要与我创建的另一个web对话。这个有问题的web已经过时了,我唯一修改的mad是启用内核,并在其中添加了两个类。
问题
目前,这个web一直在执行到我的集成点,但是当它到达HttpResponseMessage hrm = await hc.GetAsync(requestUrl);
时,它就停止了,不会给我一个错误。我试着在这个函数上添加一个try catch,但是它也忽略了这个tried。下面的代码将显示我如何构建这个web。
码
控制器
[Route("api/WorkingPaperServices/CreateWorkingPaper")]
public IHttpActionResult CreateWorkingPaper()
{
try
{
log4net.Config.XmlConfigurator.Configure();
bool complete = WorkingPaperCreator.StartWPCreation().Result;
return Ok("Finished");
}
catch(Exception ex)
{
log4net.LogManager.GetLogger("EmailLogger").Error(JsonConvert.SerializeObject(ex));
return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Not Laoded"));
}
}
WorkingPaperCreator - class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
namespace CWAppServices.Services
{
public class WorkingPaperCreator
{
public static async Task<bool> StartWPCreation()
{
try
{
var testing = await IntergrationPoints.GetClientInformation();
return true;
}
catch(Exception ex)
{
return false;
}
}
}
}
IntergrationPoints - class (我的问题所在)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
namespace CWAppServices.Services
{
public class IntergrationPoints
{
public static async Task<bool> GetClientInformation()
{
List<string> uniqueID = new List<string>();
try
{
string requestUrl = "http://appdev.tenant.com/caseware32/api/DataServices/GetAllClients";
var action = new HttpMethod("GET");
HttpClient hc = new HttpClient();
HttpResponseMessage hrm = await hc.GetAsync(requestUrl);
if (hrm.IsSuccessStatusCode)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
// TODO Log
return false;
}
}
}
}
发布于 2017-03-30 12:58:06
这个阻塞调用导致问题:bool complete = WorkingPaperCreator.StartWPCreation().Result;
。你会死锁。为了避免这种情况,可以使用aync\await无处不在(也使您的控制器异步)或向所有等待调用添加.ConfigureAwait(false)
。
https://stackoverflow.com/questions/43118461
复制相似问题