dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.92k stars 4.64k forks source link

ASP.NET Core - Post Request to API - InnerException {System.Net.Http.WinHttpException (0x80072F78): The server returned an invalid or unrecognized response #26343

Closed rajugurrapu closed 4 years ago

rajugurrapu commented 6 years ago

Request to POST, after 5 minutes the post request is fails with below error.

InnerException  {System.Net.Http.WinHttpException (0x80072F78): The server returned an invalid or unrecognized response

  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.DiagnosticsHandler.<SendAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
karelz commented 6 years ago

I assume you're running .NET Core 2.0. Can you please try it on 2.1? (just released earlier this week)

Do you have a minimal repro you can share with us? Does it happen against any server, or just some? Did you look at the network traffic via Fiddle or Wireshark?

briziomusic commented 6 years ago

I'm facing the same issue migrating a microservice based on netstandard from dotnet core 1 to the latest 2. So the issue can't be closed imho

karelz commented 6 years ago

The stack trace above has WinHttpException, which means it is unlikely running on .NET Core 2.1. 2.1 has brand new networking stack, not using WinHttp. If you can repro on .NET Core 2.1 (with default SocketsHttpHandler), please provide a repro / look at network traffic and answer questions above. That will help us guide it further.

I will close the issue as there is nothing actionable for us now. If something actionable appears, we can reopen.

andrew-kuzovov commented 6 years ago

nothing actionable? I can't update to .NET Core 2.1 because of other issues and I got stuck with this problem

los93sol commented 5 years ago

Running into this same issue, also targetting .NET core 2.1 and using WinHttpHandler so that I can connect to an HTTP2 endpoint. Is there a workaround? If the latest previews of 3.0 implementation of Sockethandler supports HTTP2 now then I will just use that instead.

los93sol commented 5 years ago

In my case it seems like a timing issue, I'm connecting to an API that will stream data to me after a period of time. At that point I am supposed to close the stream and immediately re-open the stream to wait for more data to come. I do this in a while loop with a CancellationToken so on each pass over the loop it re-opens the stream. Adding a short delay seems to resolve it for my case

sathishTkumar commented 5 years ago

+1 The server returned an invalid or unrecognized response.

   at System.Net.Http.HttpConnection.FillAsync()
   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)

screenshot_1

Am using the below .net core version and dependent packages.

 <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
 <ItemGroup>
    <PackageReference Include="DinkToPdf" Version="1.0.8" />
    <PackageReference Include="DotNetCore.NPOI" Version="1.0.2" />
    <PackageReference Include="log4net" Version="2.0.8" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.1.1" />
    <PackageReference Include="Microsoft.ApplicationInsights.Log4NetAppender" Version="2.8.1" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" />
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
    <PackageReference Include="RestSharp" Version="106.3.1" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="2.5.0" />
    <PackageReference Include="Microsoft.Identity.Client" Version="1.1.0-preview" />
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.3.0" />
  </ItemGroup>

and this is my code

   static HttpClient _client;

            var httpRequestMessage = new HttpRequestMessage(new HttpMethod(method), url);

                foreach (var h in headers)
                {
                    httpRequestMessage.Headers.TryAddWithoutValidation(h.Key, h.Value);
                }

                if (!method.Equals("GET", StringComparison.OrdinalIgnoreCase))
                {
                    var httpContent = new StringContent(requestContent, Encoding.UTF8, "application/json");

                    httpRequestMessage.Content = httpContent;
                }

                var response = await _client.SendAsync(httpRequestMessage);

                var responseContent = await response.Content.ReadAsStringAsync();
karelz commented 5 years ago

@sathishTkumar @los93sol if you have a repro that we can run locally, please share it. Otherwise I am afraid there is not much we can do :(

@sathishTkumar why do you use WinHttpHandler? We recommend everyone to use default SocketsHttpHandler (unless they need something not-yet-supported like HTTP/2).

DanBovenzi commented 5 years ago

I am getting this issue when accessing a service on-prem. from an App Service via Hybrid Connection.

Running both API's internally works, but when I try to run this architecture:

External Web API hosted on App Service connects via Hybrid Connection to Internal Web API hosted on-prem. IIS I get the error described above.

Running .NET Core 2.1

karelz commented 5 years ago

I am not sure what "App Service with Hybrid Connection" is. Does it have some fancy VPN setup or something special? Can you debug the environment easily?

DanBovenzi commented 5 years ago

I am not sure what "App Service with Hybrid Connection" is. Does it have some fancy VPN setup or something special? Can you debug the environment easily?

Hybrid Connection is an Azure App Service feature that proxies calls from public to on-prem. networks via a Service Bus connection and an on prem. agent.: https://docs.microsoft.com/en-us/azure/app-service/app-service-hybrid-connections

It's been around a while, but is useful for gaining access to on-prem. services without exposing them to the public internet via firewall inbound rules.

Debugging is tricky because the response is coming from the Hybrid Connection internals after it has proxied the response back to my service.

I've got round the issue by using vnet integration instead - but it would be good to know what the issue is with Hybrid Connection.

tschultz1216 commented 5 years ago

Hello is there any update on this issue @DanBovenzi pointed out that he was only receiving this failure when passing the http message through a hybrid proxy. I am experiencing the exact same behavior when utilizing IBM Cloud Secure Gateway which is the functional equivalent of Azure Hybrid Connection. The source code always succeeds with a direct invocation of the API but when running through the allocated proxy "The server returned an invalid or unrecognized response" is returned as an exception