我有一个带有以下控制器的asp.net core 2.0 Web应用程序(使用Kestrel运行):
public IActionResult GetUpdateList(string apiCode, int softwareId, [FromBody] List<SoftwareFile> updateFiles)
{
try
{
var stream = SoftwareUpdateFilesHandler.GetUpdateZipFileStream(updateFiles, softwareId);
return File(stream.BaseStream, "application/octet-stream", "UpdateFile");
}
catch (System.Exception ex)
{
return NotFound(ex.ToString());
}
}
以及我客户端上的代码:
public async static Task<byte[]> GetUpdateAsync(string apiCode, int softwareId, List<SoftwareFile> updatefiles)
{
try
{
StringContent content = null;
if (updatefiles != null && updatefiles.Count > 0)
{
content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(updatefiles));
content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
}
string address = $"{baseAddress}GetUpdate?softwareId={softwareId}";
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("POST"), address);
request.Content = content;
var response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
var responseContent = await response.Content.ReadAsByteArrayAsync();
return responseContent;
}
catch(Exception ex)
{
return null;
}
}
但是,当代码到达httpClient.SendAsync
时,代码将挂起,几秒钟后客户端应用程序崩溃,我得到了这个异常:The underlying connection was closed: The connection was closed unexpectedly
当我在本地测试代码时,所有这些工作都很好,但是当我发布代码并尝试调用GetUpdateList
时,我得到了异常。
奇怪的是,我无法在catch块中处理异常。不知何故,catch块无法捕获此异常,我可以在应用程序崩溃时看到异常。