Web API REST WebサービスにアクセスするWeb APIクライアントを使用しています。デバッグ時にこれを呼び出すときに私たちは、次のエラーを取得しているVisual Studioでのデバッグ時に基底の接続が閉じられたエラー
:
An error occured while sending the request (Inner Exception: The underlying conection was closed: An unexpected error occured on a send)
のVisual Studioの2015年debbuggingせずにコードを実行しているときには発生しませんをデバッグするときにのみ発生します。 .NET 4.6.1を使用しています。
どうしてですか?デバッグ時にデバッグ時にクラッシュする理由は何ですか?あなたはこの問題をどのように解決することができ、私たちは何を確認することができるか考えていますか?
我々は、Webサービスの呼び出しを行うために、次のコードを使用:あなたはあなたが正しく設定を構成している以下の
を確認することがよう
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);
}
これは、同じプロジェクトをデバッグするときに別のマシンで発生しますか? – DiskJunky
この場合、コードにブレークポイントを入れますか?または、デバッガで実行しますが、どこにでもブレークしませんか?それは常に起こるか、まれにしか起こりませんか? – Evk
私はWindows 10とVisual Studio 2017の2台目のPCを持っています。すべてがコアで実行されています。私は正常にアプリケーションをデバッグすることができます。 この問題が発生したPCは、Windows 7でVisual Studio 2015で実行されています。 – Alexander