我正在面对 System.Threading.Tasks.TaskCanceledException 从另一个webService(简称"MainWebService")异步调用web服务(比如"Backend WebService") .
要创建表单的PDF,MainWebServie应调用Backend WebService方法CreatePDF(formId) .
Main WebService有一个api Called ProcessForms() ,它包含一个逗号分隔的formIds,可以作为参数传递,其文档(PDF)应该被创建 .
另一个名为BackgroundWebService的webService . 它包含一个名为 CreatePDF() 的方法,其中可以使用单独的formId来创建PDF .
由于最多可以有20个FormId,可以用逗号分隔并作为Main Webservice方法ProcessForms()的参数传递以创建每个的PDF,我想从Main WebService异步调用backendWebService,这样用户就不会必须等待在前端创建所有PDF,并且该过程可以以“Fire and Forget”方式进行 .
它在大多数情况下工作正常,但最近我注意到周末发生了几乎一次 System.Threading.Tasks.TaskCanceledException 导致一些记录在过程中间终止 .
如果我在某处做错了,请纠正我:
//Method of MainWebService where comma separated formIds can be passed whose PDF should be created
public string ProcessForms(string suppliedFormIds)
{
//Get the Comma separated GUID to process
List<string> formIdList = suppliedFormIds.Split(',').Select(Convert.ToString).ToList();
//Loop through each record and process the record asynchronously (Fire and Forget)
for (int i = 0; i <= formIdList.Count; i++)
{
var queueId = formIdList[i].ToString();
**this.ProcessFormRecord**(Guid.Parse(queueId));
}
return "Success";
}
// method that calls the backendWebService Asynchronously to create the PDF
public **async void** ProcessFormRecord(Guid formId)
{
string message = string.Empty;
HttpResponseMessage httpResponse = null;
string webserviceUrl = "https://abcd.com/API/MyWebservice.svc";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(webserviceUrl);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpRequestenter code hereMessage request = new HttpRequestMessage(HttpMethod.Post, webserviceUrl + "/CreatePDF");
string content = "{\"formId\":\"" + formId.ToString() + "\"}";
request.Content = new StringContent(content, Encoding.UTF8, "application/json");
httpResponse = **await client.PostAsync**(request.RequestUri, request.Content);
}
}