[英]HttpClient.PostAsync no response & no exceptions
public async static Task<string> GetResponse(string host, string request, HttpClient client)
{
HttpContent content = new StringContent(request, Encoding.UTF8, "application/xml");
HttpResponseMessage message = await client.PostAsync(host, content).ConfigureAwait(false); // issue here
string response = await message.Content.ReadAsStringAsync().ConfigureAwait(false);
return response;
}
同一命名空间内的调用代码 -
public async Task<string> Post(Transaction transaction)
{
string postXml = PostXml.Create(transaction); // serializes object to XML
string response = await GetResponse(host, postXml, client); // client initialized in constructor of this class
return await Deserialize.Response(response);
}
当直接从单元测试调用它们时,上述方法按预期运行。
我有一个接口可以利用不同服务的动态加载 - 它调用Task<string> Post(Transaction transaction)
方法。 从我的主项目调用此方法时 - 相关服务已加载(取决于设置),但我没有收到client.PostAsync(host, content)
的响应。
没有异常或错误。 我试图用断点调试问题,但一无所获。 它命中PostAsync
并继续只是转义代码的 rest。
注意 1:一切都是异步的,并且没有任何线程阻塞代码,例如.Wait()
或.Result
注意 2:我也尝试通过删除 ConfigureAwait 来运行它。 在这两种情况下,单元测试都能完美运行。 当我在我的主项目中引用这个项目时,不知何故它不起作用。 我还是不明白,因为我仍然接触到那部分代码。 除此之外,程序仍然运行而不会阻塞 UI 线程,但没有响应。
更新
为了获得host
,我正在读取配置 json 文件。 读取此文件导致了我面临的问题,当我将主机硬编码为变量时,一切正常。 最初对该文件的读取是异步的,但为了调试问题我将其更改为同步读取 - 这仍然没有解决问题。
注意 - 使用 App.Config 效果很好。
我正在使用 newtonsoft 读取自定义 json 设置文件。 我不太喜欢从 app.config 读取配置,因为它的 XML 结构和访问自定义部分的复杂方式。
public static ServerConfig GetServerSettings()
{
string json = File.ReadAllText("appSettings.json");
return JsonConvert.DeserializeObject<ServerConfig>(json);
}
ServerConfig 具有属性,即我要访问的string host
和string company
(即哪个公司的数据)。
在 WPF/WinForm 和(旧的)ASP.NET 中,您必须使用 CongigureAwait( true )。
简单来说ConfigureAwait
:
更多信息MSDN 杂志
public async static Task<string> GetResponse(string host, string request, HttpClient client)
{
HttpContent content = new StringContent(request, Encoding.UTF8, "application/xml");
HttpResponseMessage message = await client.PostAsync(host, content).ConfigureAwait(false); // issue here
string response = await message.Content.ReadAsStringAsync().ConfigureAwait(false);
return response;
}
同一命名空间内的调用代码 -
public async Task<string> Post(Transaction transaction)
{
string postXml = PostXml.Create(transaction); // serializes object to XML
string response = await GetResponse(host, postXml, client); // client initialized in constructor of this class
return await Deserialize.Response(response);
}
上述方法在直接从单元测试中调用时按预期运行。
我有一个接口来利用不同服务的动态加载 - 它调用Task<string> Post(Transaction transaction)
方法。 从我的主项目调用此方法时 - 已加载相关服务(取决于设置),但我没有收到来自client.PostAsync(host, content)
的响应。
没有异常或错误。 我试图用断点调试问题,但一无所获。 它命中PostAsync
并继续只是转义代码的 rest。
注意 1:一切都是异步的,并且没有任何线程阻塞代码,例如.Wait()
或.Result
任何地方
注意 2:我也尝试通过删除 ConfigureAwait 来运行它。 在这两种情况下,单元测试都能完美运行。 当我在我的主项目中引用这个项目时,它以某种方式不起作用。 我仍然不明白,因为我仍然到达那部分代码。 除此之外,程序仍然在不阻塞 UI 线程的情况下运行,但没有响应。
更新
为了得到host
,我正在阅读配置 json 文件。 读取此文件导致了我面临的问题,当我将主机硬编码为变量时,一切正常。 最初该文件的读取是异步的,但为了调试问题,我将其更改为同步读取 - 这仍然没有解决问题。
注意 - 使用 App.Config 效果很好。
我正在使用 newtonsoft 读取自定义 json 设置文件。 由于它的 XML 结构和访问自定义部分的复杂方式,我不是从 app.config 读取配置的忠实粉丝。
public static ServerConfig GetServerSettings()
{
string json = File.ReadAllText("appSettings.json");
return JsonConvert.DeserializeObject<ServerConfig>(json);
}
ServerConfig 具有我要访问的string host
和string company
(即哪个公司的数据)的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.