我有一个 Web 服务(用 Asp.net Web API 制作),它返回一个大小约为 10MB 的 xml 文件。
该服务已经过 Fiddler 测试,并且正在运行
我正在尝试使用 HttpClient 类下载文件。问题是编译器永远不会超出await client.GetAsync()
方法,即使 API 项目返回HttpResponseMessage
.
这是我的功能
public async Task<XDocument> DownloadXmlAsync(string xmlFileName)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:51734/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
// When the copiler enters this next command, it doesn't get outside anymore
HttpResponseMessage response = await client.GetAsync("api/applications/ApplicationXml?fileName=" + xmlFileName);
response.EnsureSuccessStatusCode();
string stringResponse = await response.Content.ReadAsStringAsync();
XDocument xDoc = new XDocument(stringResponse);
return xDoc;
}
}
我还更新了 web.config 中的 maxRequestLength
<httpRuntime maxRequestLength="15360" />
我做错了什么?
编辑
调用函数
public async Task<ActionResult> Index()
{
var xmlTask = DownloadXmlAsync("1.xml");
// doesn't reach here
var result = xmlTask.Result;
return View();
}