在 Visual Studio 中调试时出现底层连接已关闭错误
The underlying connection was closed error when debugging in Visual Studio
我们正在使用访问 Web API REST Web 服务的 Web API 客户端。
调试时调用此方法时出现以下错误:
An error occured while sending the request (Inner Exception: The underlying conection was closed: An unexpected error occured on a send)
这只会在 Visual Studio 2015 年进行调试时发生。如果 运行 在没有调试的情况下调试代码,则不会发生。
我们正在使用.NET 4.6.1。
为什么会这样?为什么不调试时运行,调试时崩溃。您知道我们如何解决这个问题以及我们可以检查什么吗?
我们使用以下代码进行网络服务调用:
public async Task<string> GetToken(string username, string password)
{
using (var client = new HttpClient())
{
InitializeHttpClient(client);
HttpContent requestContent = new StringContent("grant_type=password&username=" + username + "&password=" + password, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await client.PostAsync("token", requestContent);
if (response == null || response.StatusCode == HttpStatusCode.BadRequest)
return null;
if (response.StatusCode == HttpStatusCode.NotFound
|| response.StatusCode == HttpStatusCode.RequestTimeout
|| response.StatusCode == HttpStatusCode.BadGateway
|| response.StatusCode == HttpStatusCode.ServiceUnavailable)
{
throw new Exception("No Connection to Web Service");
}
var bearerData = response.Content.ReadAsStringAsync().Result;
return JObject.Parse(bearerData)["access_token"].ToString();
}
}
private void InitializeHttpClient(HttpClient client)
{
client.BaseAddress = new Uri(_webServiceAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("User-Agent", "Test Client");
}
private void InitializeHttpClient(HttpClient client, string token)
{
InitializeHttpClient(client);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
}
这似乎是由 Windows 更新 KB4041083 引起的。
我们卸载了这个更新,然后一切又恢复正常了。
我们正在使用访问 Web API REST Web 服务的 Web API 客户端。
调试时调用此方法时出现以下错误:
An error occured while sending the request (Inner Exception: The underlying conection was closed: An unexpected error occured on a send)
这只会在 Visual Studio 2015 年进行调试时发生。如果 运行 在没有调试的情况下调试代码,则不会发生。 我们正在使用.NET 4.6.1。
为什么会这样?为什么不调试时运行,调试时崩溃。您知道我们如何解决这个问题以及我们可以检查什么吗?
我们使用以下代码进行网络服务调用:
public async Task<string> GetToken(string username, string password)
{
using (var client = new HttpClient())
{
InitializeHttpClient(client);
HttpContent requestContent = new StringContent("grant_type=password&username=" + username + "&password=" + password, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await client.PostAsync("token", requestContent);
if (response == null || response.StatusCode == HttpStatusCode.BadRequest)
return null;
if (response.StatusCode == HttpStatusCode.NotFound
|| response.StatusCode == HttpStatusCode.RequestTimeout
|| response.StatusCode == HttpStatusCode.BadGateway
|| response.StatusCode == HttpStatusCode.ServiceUnavailable)
{
throw new Exception("No Connection to Web Service");
}
var bearerData = response.Content.ReadAsStringAsync().Result;
return JObject.Parse(bearerData)["access_token"].ToString();
}
}
private void InitializeHttpClient(HttpClient client)
{
client.BaseAddress = new Uri(_webServiceAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Add("User-Agent", "Test Client");
}
private void InitializeHttpClient(HttpClient client, string token)
{
InitializeHttpClient(client);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
}
这似乎是由 Windows 更新 KB4041083 引起的。 我们卸载了这个更新,然后一切又恢复正常了。