2

We are using a Web API Client that accesses a Web API REST Web Service.

We are getting the following error when calling this when Debugging:

An error occured while sending the request (Inner Exception: The underlying conection was closed: An unexpected error occured on a send)

This only happens when Debugging in Visual Studio 2015. It does not happen when running the Code without debbugging. We are using .NET 4.6.1.

Why does this happen? Why does it run when not Debugging and Crash when Debugging. Do you have any ideas how we can solve this Problem and what we can check?

We use the following Code to make the web Service calls:

    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);

    }
Alexander
  • 1,021
  • 6
  • 20
  • 38
  • Does this happen on a different machine when debugging the same project? – DiskJunky Oct 09 '17 at 15:07
  • Do you put any breakpoints in the code when this happens? Or you just run with debugger but not break anywhere? Does it happens always or just occasionally? – Evk Oct 09 '17 at 15:10
  • I have a second PC with Windows 10 and Visual Studio 2017. Everything runs corectly there. I can debug the application normally. The PC on which this Problem occurs is running on Windows 7 with Visual Studio 2015. – Alexander Oct 09 '17 at 15:10
  • We have tried it with and without breakpoints. It always crashes at the following line: var response = await client.PostAsync("token", requestContent); – Alexander Oct 09 '17 at 15:11
  • Use a proxy such as [Fiddler](http://www.telerik.com/fiddler) to examine the request and response. – Alan Larimer Oct 09 '17 at 15:20
  • https://stackoverflow.com/questions/29886223/c-sharp-system-net-webexception-the-underlying-connection-was-closed-an-unexpe, you could troubleshooting this issue using the steps provided in this thread. I think we need to collect much more information. – Jack Zhai Oct 10 '17 at 06:47
  • When using fiddler to look at what has been sent to the server by the client it Looks like the Client is using TLS/1.0 instead of 1.2. This could cause that the connection is denyed. On the PC on which everything works fine this is using TLS/1.2. Why does the Web API Client use TLS/1.0 even though we are using .NET Framework 4.6.1? Is it possible to set which Version of TLS should be used in Visual Studio? – Alexander Oct 10 '17 at 08:28

1 Answers1

2

This seems to be caused by Windows Update KB4041083. We deinstalled this update and then everything worked normal again.

Alexander
  • 1,021
  • 6
  • 20
  • 38
  • Glad to know that it has been resolved, you could mark it as the answer, so it could help other community members who get the same issue as yours. Thanks for your sharing. – Jack Zhai Oct 13 '17 at 01:57