Owin/System.Web.Http.SelfHost - Won't Start after migration to .NET Core

0

I migrated the project from .NET Framework to .NET Core, by creating a new project, importing the code and installing dependencies. Everything has gone well, except for the API component.Previously I used System.Web.Http.Selfhost, however now when I try to run the program I get the following exception:

System.TypeLoadException HResult=0x80131522 Message=Could not load type 'System.ServiceModel.HostNameComparisonMode' from assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Source=mBot StackTrace: at mBot.Program.d__2.MoveNext() in C:\Users\maren\source\repos\mBot-Admin\mBot\Program.cs:line 70 at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) at mBot.Program.Run() at mBot.Program.Main(String[] args) in C:\Users\maren\source\repos\mBot-Admin\mBot\Program.cs:line 28

It's happening at this line, and commenting out the line makes the program start up successfully:
https://awau.moe/ad25d4.png
At this point I should note that the value of Config.BotConfig.ApiPort is 8080
I have also tried using OWIN to host, as that seems to be the more recent version. I get the following:

System.Reflection.TargetInvocationException HResult=0x80131604
Message=Exception has been thrown by the target of an invocation.
Source=System.Private.CoreLib StackTrace: at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create(IAppBuilder builder) at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context) at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options) at mBot.Program.d__2.MoveNext() in C:\Users\maren\source\repos\mBot-Admin\mBot\Program.cs:line 47 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at mBot.Program.Main(String[] args) in C:\Users\maren\source\repos\mBot-Admin\mBot\Program.cs:line 28

Inner Exception 1: HttpListenerException: Access is denied

https://awau.moe/21800e.png

I don't mind whether I use OWIN or Selfhost, as long as whatever it is works.

Does anyone have any idea what the cause of this could be? It's not my account's permission to use port 8080, I've already added that before, and it still works with the old code.

The current code that I'm using is at at https://gitlab.com/marens101/mBot-Admin/tree/rewrite/mBot
- Main code for webserver is in Program.cs, lines 45-55. Both OWIN and SelfHost are there, both commented out.
- The OwinStartup object is in API/OwinStartup.cs


Update: I'm now using ASP.NET Core with Kestrel (thanks, @Lex Li), which is mostly working except that the application no longer closes on an unhandled exception, which is rather important. Instead, the connection with my other API is closed, but the webserver remains running, and the application does not quit. Does anyone know a way around this?

c#
.net-core
owin
self-host-webapi
asked on Stack Overflow Oct 14, 2018 by marens101 • edited Nov 1, 2018 by marens101

1 Answer

0

Over 2 years later, I rediscovered my old post and figured I should provide my solution in the (unlikely) event someone else finds it useful in future. As seen in the question's comments sections, Lex Li pointed out that I should migrate away from OWIN (IIS-based) to ASP.NET Core (Kestrel-based).

This solution worked fine, as I noted in my update to the question.

answered on Stack Overflow Dec 17, 2020 by marens101

User contributions licensed under CC BY-SA 3.0