aspnet / BasicMiddleware

[Archived] Basic middleware components for ASP.NET Core. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
169 stars 84 forks source link

IPEndPointParser exception #356

Closed bmpcon closed 5 years ago

bmpcon commented 6 years ago

Hello,

when deploying a vanilla ASP.NET Core (2.1.1) WebApi application to our enterprise server (Windows 2016) we get the following exception when doing a request from localhost:

Application started. Press Ctrl+C to shut down.
fail: Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HLG41UURV0D2", Request id "0HLG41UURV0D2:00000001": An unhandled exception was thrown by the application.
System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.Globalization.CultureData.GetNFIValues(NumberFormatInfo nfi)
   at System.Globalization.NumberFormatInfo..ctor(CultureData cultureData)
   at System.Globalization.CultureInfo.get_NumberFormat()
   at System.Globalization.NumberFormatInfo.get_CurrentInfo()
   at System.Int32.TryParse(String s, Int32& result)
   at Microsoft.AspNetCore.HttpOverrides.Internal.IPEndPointParser.TryParse(String addressWithPort, IPEndPoint& endpoint)
   at Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersMiddleware.ApplyForwarders(HttpContext context)
   at Microsoft.AspNetCore.HttpOverrides.ForwardedHeadersMiddleware.Invoke(HttpContext context)
  at Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.Hosting.Internal.HostingApplication.ProcessRequestAsync(Context context)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

As a workaround we were able to solve the problem with changing the thread culture to en-us:

private static void SetCultureToUs()
{
    var cultureInfo = new CultureInfo("en-US");
    CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
    CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
}
Tratcher commented 6 years ago

This line needs to specify an invariant culture: https://github.com/aspnet/BasicMiddleware/blob/1203bcf63dc793ae3f7c9217bb2a46727bae9348/src/Microsoft.AspNetCore.HttpOverrides/Internal/IPEndPointParser.cs#L65

Tratcher commented 6 years ago

@shirhatti for triage.

Tratcher commented 5 years ago

What was the local culture when this exception was thrown?

Tratcher commented 5 years ago

FYI: I've changed the middleware code to specify the invariant culture which should avoid this problem. However it does not explain the underlying IndexOutOfRangeException error in Globalization. I'm going to move this issue to corefx for further followup.

Tratcher commented 5 years ago

This issue was moved to dotnet/corefx#32870