aspnet / AspNetKatana

Microsoft's OWIN implementation, the Katana project
Apache License 2.0
963 stars 332 forks source link

Internal NullReferenceException in OwinHttpListener.cs on .Net(Core) 5 #392

Closed morpheusxx closed 3 years ago

morpheusxx commented 3 years ago

I am porting a .NET Framework 4.7 application to .NET 5 which uses Microsoft.AspNet.WebApi.OwinSelfHost 5.2.7 (which depends on Microsoft.Owin.Host.HttpListener).

When running my app in VisualStudio I can see NullReferenceExceptions logged in DEBUG outputs. When using decompile feature I can see the source from accessing the 2 static fields:

https://github.com/aspnet/AspNetKatana/blob/1fba52940ccfa74470c75c627699baca13aadfa4/src/Microsoft.Owin.Host.HttpListener/OwinHttpListener.cs#L30-L31

They depend on reflection of non-public fields.

The NRE itself happens at line 226 when accessing text = (((string)CookedPathField.GetValue(request)) ?? string.Empty);. For mono there seems to be another approach used.

Can you please review the reflection based access to the 2 fields using .NET 5?

Tratcher commented 3 years ago

These packages are not supported on .NET 5, you should be using AspNet Core instead.

morpheusxx commented 3 years ago

Thank you for the very quick response. I probably need to explain my environment a bit more:

The application is Windows Forms based and uses the Owin selfhosting inside the UPnP library used for client/server communication. The UPnP library can be found here as part of MediaPortal 2: https://github.com/MediaPortal/MediaPortal-2/tree/EXP_NET5/MediaPortal/Source/Core/UPnP.

you should be using AspNet Core instead.

So how could AspNet Core can be used in my case? Or are there any other .NET Core alternatives on Windows?

Tratcher commented 3 years ago

Unlike ASP.NET 4.x, ASP.NET Core fully supports self-host scenarios. Much of ASP.NET Core was derived from these Microsoft.Owin libraries.

This: https://github.com/MediaPortal/MediaPortal-2/blob/8234fb872b2f1b59338468cbd57d2127edff0388/MediaPortal/Source/Core/UPnP/Infrastructure/Dv/UPnPServer.cs#L185

Becomes something like: https://github.com/dotnet/aspnetcore/blob/b143a974a5574b9fccedbbe2389aabbfd48fdb6f/src/Hosting/samples/GenericWebHost/Program.cs#L14-L28 but change RunAsync to StartAsync since it looks like you want to start multiple servers.

ghost commented 3 years ago

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

morpheusxx commented 3 years ago

https://github.com/aspnet/AspNetKatana/blob/1fba52940ccfa74470c75c627699baca13aadfa4/src/Microsoft.Owin.Host.HttpListener/OwinHttpListener.cs#L289 I changed this code line to: if (IsMono || CookedPathField == null) and it fixed my issue. I didn't find other issues yet.