Coder Social home page Coder Social logo

standardsocketshttphandler's People

Contributors

onyxmaster avatar talaloni avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

standardsocketshttphandler's Issues

Consider building the NuGet package with debug symbols

Hi.

When searching for the root cause for #13, I was blocked by the NuGet release missing the debug symbols. I would like to suggest adding the <DebugType>embedded</DebugType> to the project file for all configurations. This is easier than doing the full SourceLink stuff, does not add any extra files to the deployment and makes adds only about 40 KB to the built library size, but makes determining the source of exceptions much easier.
For example, compare the stack trace from #13 (where I included the debug symbols) to the following (the one which is produced by using the NuGet build):

System.Net.Http.HttpRequestException: An error occurred while sending the request.
async Task<HttpResponseMessage> System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
async Task<HttpResponseMessage> System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, bool doRequestAuth, CancellationToken cancellationToken)
async Task<HttpResponseMessage> System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, bool doRequestAuth, CancellationToken cancellationToken)
 ---> System.IO.IOException: The server returned an invalid or unrecognized response.
async Task<HttpResponseMessage> System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)

It's much easier to determine where "System.IO.IOException: The server returned an invalid or unrecognized response" is thrown with line numbers, since there are several places in the project where the same exception with the same message is thrown.

HttpRequestMessage in .NET Framework uses _headers not headers

When using .NET Framework 4.6.1 and System.Net.Http 4.3.4 seems to use _headers instead of headers which causes a NullReferenceException since the field cannot be found:

string headersFieldName = RuntimeUtils.IsDotNetFramework() ? "headers" : "_headers";

Here's my stack trace:

at System.Net.Http.HttpRequestMessageExtensions.HasHeaders(HttpRequestMessage request)
   at System.Net.Http.StandardSocketsHttpHandler.ValidateAndNormalizeRequest(HttpRequestMessage request)
   at System.Net.Http.StandardSocketsHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.<SendAsync>d__2.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.<SendAsync>d__2.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<FinishSendAsyncBuffered>d__58.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

ILSpy of System.Net.Http 4.3.4:
image

I'm not familiar with the exact reason you put this in place, but should we check for both fields until we find one instead for backward compatibility?

Great Work!

Great Work!
A good way to fix the HttpClient dns issues

Make it strongly named

Hello,
we couldn't use your nuget package in our solution that is using strong names everywhere, because StandardSocketsHttpHandler doesn't have a strong name.
Could not load file or assembly 'StandardSocketsHttpHandler, Version=2.2.0.5, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)
Can you please make it strongly named?

Getting System.NullReferenceException from HasHeaders

I am getting the following error while using the package in .NET Framework

System.Net.Http.HttpRequestMessageExtensions.HasHeaders(System.Net.Http.HttpRequestMessage)
System.Net.Http.StandardSocketsHttpHandler.ValidateAndNormalizeRequest(System.Net.Http.HttpRequestMessage)
System.Net.Http.StandardSocketsHttpHandler.SendAsync(System.Net.Http.HttpRequestMessage, System.Threading.CancellationToken)
System.Net.Http.DelegatingHandler.SendAsync(System.Net.Http.HttpRequestMessage, System.Threading.CancellationToken)

I am using NET Framework 4.6.1
I have installed System.Net.Http 4.3.4 in my project.

HttpRequestMessage has a _headers property instead of the expected headers property

#region Assembly System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
// C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll
#endregion
image

Getting an exception while using StandardSocketsHttpHandler with grpc.net on .net 4.7.2

Hi! I was trying to use StandardSocketsHttpHandler with grpc.net on .net 4.7.2 however, it is throwing an exception:

System.AggregateException: One or more errors occurred. ---> Grpc.Core.RpcException: Status(StatusCode="Unavailable", Detail="Error starting gRPC call. HttpRequestException: An error occurred while sending the request. IOException: The server returned an invalid or unrecognized response.", DebugException="System.Net.Http.HttpRequestException: An error occurred while sending the request.") ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.IO.IOException: The server returned an invalid or unrecognized response.
   at System.Net.Http.HttpConnection.<FillAsync>d__91.MoveNext() in C:\Projects\StandardSocketsHttpHandler\StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnection.cs:line 1337
--- 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.Net.Http.HttpConnection.<ReadNextResponseHeaderLineAsync>d__89.MoveNext() in C:\Projects\StandardSocketsHttpHandler\StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnection.cs:line 1289
--- 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.Net.Http.HttpConnection.<SendAsyncCore>d__61.MoveNext() in C:\Projects\StandardSocketsHttpHandler\StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnection.cs:line 531
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.<SendAsyncCore>d__61.MoveNext() in C:\Projects\StandardSocketsHttpHandler\StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnection.cs:line 725
--- 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.Net.Http.HttpConnectionPool.<SendWithNtConnectionAuthAsync>d__40.MoveNext() in C:\Projects\StandardSocketsHttpHandler\StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnectionPool.cs:line 316
--- 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.Net.Http.HttpConnectionPool.<SendWithRetryAsync>d__39.MoveNext() in C:\Projects\StandardSocketsHttpHandler\StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnectionPool.cs:line 296
--- 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.Net.Http.RedirectHandler.<SendAsync>d__4.MoveNext() in C:\Projects\StandardSocketsHttpHandler\StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\RedirectHandler.cs:line 32
--- 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 Grpc.Net.Client.Internal.GrpcCall`2.<RunCall>d__74.MoveNext()
   --- End of inner exception stack trace ---

This is how I am using it:

StandardSocketsHttpHandler socketsHttpHandler = new StandardSocketsHttpHandler()
{
    SslOptions = new SslClientAuthenticationOptions
    {
        EncryptionPolicy = EncryptionPolicy.RequireEncryption,
        ClientCertificates = new X509CertificateCollection(),
        RemoteCertificateValidationCallback = (request, cert, chain, errors) =>
        {
            // Some server cert validation logic
            return true;
        },
    },
};

// Add client cert
socketsHttpHandler.SslOptions.ClientCertificates.Add(clientCert);

channel = GrpcChannel.ForAddress($"https://{options.DestinationIp}:{options.DestinationPort}", new GrpcChannelOptions { HttpHandler = socketsHttpHandler });

The same code works on .net 6 if I replace StandardSocketsHttpHandler with SocketsHttpHandler. Any ideas what might be causing this issue?

Strange Compilation Error When Referencing 3.1.0.1

Continuing from #8

I get the following error when adding this as a reference to my project and compiling:

/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client.Common/AsyncStreamReaderExtensions.cs(8,76): error CS0656: Missing compiler required member 'System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore`1.GetResult' [/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client/EventStore.Client.csproj]
/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client.Common/AsyncStreamReaderExtensions.cs(8,76): error CS0656: Missing compiler required member 'System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore`1.GetStatus' [/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client/EventStore.Client.csproj]
/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client.Common/AsyncStreamReaderExtensions.cs(8,76): error CS0656: Missing compiler required member 'System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore`1.get_Version' [/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client/EventStore.Client.csproj]
/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client.Common/AsyncStreamReaderExtensions.cs(8,76): error CS0656: Missing compiler required member 'System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore`1.OnCompleted' [/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client/EventStore.Client.csproj]
/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client.Common/AsyncStreamReaderExtensions.cs(8,76): error CS0656: Missing compiler required member 'System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore`1.Reset' [/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client/EventStore.Client.csproj]
/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client.Common/AsyncStreamReaderExtensions.cs(8,76): error CS0656: Missing compiler required member 'System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore`1.SetException' [/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client/EventStore.Client.csproj]
/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client.Common/AsyncStreamReaderExtensions.cs(8,76): error CS0656: Missing compiler required member 'System.Threading.Tasks.Sources.ManualResetValueTaskSourceCore`1.SetResult' [/home/jwow/dev/EventStore-Client-Dotnet/src/EventStore.Client/EventStore.Client.csproj]

Source code of AsyncStreamReaderExtensions.cs

Status of 3.1?

I came across your library and it's just what we're looking for, specifically unencrypted http/2 support in our unit tests. I see it on your 3.1 branch but it's not available on nuget.

A call to SSPI failed, see inner exception. The message received was unexpected or badly formatted

Hello.
First of all, thank you very much for making this good library, it helps me a lot to improve the performance of my program.
I have a problem with all the services that exist in net framework to communicate with URLs, I was looking for a place to raise them.
There are some sites that have not provided the correct certificate, and when we try to get them through httpClient, WebClient or anything else, the program encounters an error, such as http://www.irmp3.ir or http://www.resanejavan.com
I recently used your library to maybe fix this problem, but it still persists. I said to share the problem with you, so that maybe you could save me from this problem.
The code I have written so far is:
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Threading;
using System.Net.Sockets;

namespace httpCertificateTesting
{
internal class Program
{
static void Main( string[] args )
{
try
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Tls13 | SecurityProtocolType.SystemDefault;
ServicePointManager.ServerCertificateValidationCallback = (message, cert, chain, errors) => { Console.WriteLine($"message: {message}\ncert: {cert}\nchain: {chain}\nerrors: {errors}"); return true; };
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var handler = new StandardSocketsHttpHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Manual;
handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { Console.WriteLine($"message: {message}\ncert: {cert}\nchain: {chain}\nerrors: {errors}"); return true; };
HttpClient client = new HttpClient( handler, false );
var r = client.GetStringAsync( "http://www.resanejavan.com/" ).Result;
Console.WriteLine( r.Length );
File.WriteAllText( "page.txt", r);
}
catch ( Exception ex )
{ Console.WriteLine( ex.ToString() ); }
Console.ReadLine();
}
}
}`

This code works well on Windows 10, but it has a problem on Windows Server 2012 r2 and gives the following error:
System.AggregateException: One or more errors occurred. ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The message received was unexpected or badly formatted
--- End of inner exception stack trace ---
at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
at System.Net.Security.SslStreamExtensions.<>c.b__0_0(IAsyncResult iar) in StandardSocketsHttpHandler\Net\Security\Extensions\SslStreamExtensions.cs:line 18
at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.ConnectHelper.d__5.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\ConnectHelper.cs:line 183
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.d__5.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\ConnectHelper.cs:line 194
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpConnectionPool.<CreateConnectionAsync>d__44.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnectionPool.cs:line 394 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.HttpConnectionPool.d__49.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnectionPool.cs:line 523
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpConnectionPool.<SendWithRetryAsync>d__39.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnectionPool.cs:line 284 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.RedirectHandler.d__4.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\RedirectHandler.cs:line 67
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task1.GetResultCore(Boolean waitCompletionNotification) at System.Threading.Tasks.Task1.get_Result()
at httpCertificateTesting.Program.Main(String[] args)
---> (Inner Exception #0) System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The message received was unexpected or badly formatted
--- End of inner exception stack trace ---
at System.Net.Security.SslState.InternalEndProcessAuthentication(LazyAsyncResult lazyResult)
at System.Net.Security.SslState.EndProcessAuthentication(IAsyncResult result)
at System.Net.Security.SslStream.EndAuthenticateAsClient(IAsyncResult asyncResult)
at System.Net.Security.SslStreamExtensions.<>c.b__0_0(IAsyncResult iar) in StandardSocketsHttpHandler\Net\Security\Extensions\SslStreamExtensions.cs:line 18
at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.ConnectHelper.d__5.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\ConnectHelper.cs:line 183
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.d__5.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\ConnectHelper.cs:line 194
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpConnectionPool.<CreateConnectionAsync>d__44.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnectionPool.cs:line 394 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.HttpConnectionPool.d__49.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnectionPool.cs:line 523
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult() at System.Net.Http.HttpConnectionPool.<SendWithRetryAsync>d__39.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\HttpConnectionPool.cs:line 284 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()
at System.Net.Http.RedirectHandler.d__4.MoveNext() in StandardSocketsHttpHandler\Net\Http\SocketsHttpHandler\RedirectHandler.cs:line 67<---

I would be grateful if you could check the problem and help me to solve it

Reflection fails on .NET Native, resulting in runtime exception

When running on .NET Native (In my case, a UWP Release build with the .NET Native toolchain enabled) reflection on the core .net binaries is not allowed, so the following code always results in headersField being null, this ends up throwing an exception at runtime when it is used.

FieldInfo headersField = typeof(HttpRequestMessage).GetField(headersFieldName, BindingFlags.Instance | BindingFlags.NonPublic);
if (headersField == null && isDotNetFramework)
{
// Fallback for .NET Framework 4.6.1
headersFieldName = "_headers";
headersField = typeof(HttpRequestMessage).GetField(headersFieldName, BindingFlags.Instance | BindingFlags.NonPublic);
}

As far as I can tell, the HasHeaders method is used as an optimization to prevent allocation of the the header objects when just trying to check if they exist, so it seems like it would be safe to take the small perf hit when running on .NET native to enable use of this library.

ConnectCallback not currently supported

It's not explicitly mentioned in the documentation that ConnectCallback is not supported.

https://docs.microsoft.com/en-us/dotnet/api/system.net.http.socketshttphandler.connectcallback

My goal was to hopefully use this to force IPv4 connections on pre-.NET 5, as described in this issue comment:

dotnet/runtime#44686 (comment)

I don't see a way to replicate this callback with StandardSocketsHttpHandler as it stands currently? Or am I mistaken?

If you don't intend to implement this, that's fine, just the documentation would need updating in that case.

Make StandardSocketsHttpHandler not sealed?

Hi.

Any chance to make the handler not sealed?

I'm trying to build a custom HttpClientFactory-like type to manage the lifetime of HttpClient and the underlying handler. This involves overriding Dispose() which cannot be done as StandardSocketsHttpHandler is now...

The connection taken from pool which was shut down by the server leads to IOException

Hi.
First, I would like to thank you for creating and maintaining this library. The default .NET Framework HttpClient implementation based on HttpWebRequest is really lacking in terms of stability (especially timeout/cancellation handling), and your library is a great fix for that.

Unfortunately, since we started using it, we encountered the following issue. With some servers which shut down the pooled connections from their own side, the request that follows fails with HttpRequestException wrapping an IOException:

System.Net.Http.HttpRequestException: An error occurred while sending the request.
async Task<HttpResponseMessage> System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) in StandardSocketsHttpHandler/StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/HttpConnection.cs:line 724
async Task<HttpResponseMessage> System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, bool doRequestAuth, CancellationToken cancellationToken) in StandardSocketsHttpHandler/StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:line 316
async Task<HttpResponseMessage> System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, bool doRequestAuth, CancellationToken cancellationToken) in StandardSocketsHttpHandler/StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs:line 296
...
---> System.IO.IOException: The server returned an invalid or unrecognized response.
async Task<HttpResponseMessage> System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) in StandardSocketsHttpHandler/StandardSocketsHttpHandler/Net/Http/SocketsHttpHandler/HttpConnection.cs:line 516

I believe that this happens due to the ConsumeReadAheadTask reading zero bytes. In the original SocketsHttpHandler this was addressed. While I understand that you're probably not going to integrate all the "new" changes to SocketsHttpHandler, this one routinely breaks conversations with remote servers and probably is a good candidate for a backport.

I guess we can work around the issue by making the connection pool lifetime smaller for this specific service, but I don't think that will protect us from the problem in all cases.

Thanks for taking a look.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.