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?

marens101
  • 101
  • 1
  • 5
  • Post the exceptions and code in the question, not as a link. – Jeroen Heier Oct 14 '18 at 08:06
  • Try not to use links and images, it means we have to click on everything to see images of codes and errors – TheGeneral Oct 14 '18 at 08:37
  • Ok, I'll keep that in mind in future, I just didn't want to post a massive wall of text if I could avoid it. @TheGeneral Thanks for swapping the text links out for me! Should I do images too or is it too big already? – marens101 Oct 14 '18 at 09:16
  • Why do you need to port that? Microsoft Owin bits are .NET Framework only, and for .NET Core you no longer need them. – Lex Li Oct 14 '18 at 12:34
  • @LexLi It doesn't need to be OWIN, it's just that I previously used Http.SelfHost and it appears that OWIN is the later version. I don't mind what I use, as long as I can use it with an existing .NET Core project and it works as a REST API. – marens101 Oct 14 '18 at 22:27
  • 1
    You should check out the official migration article, https://learn.microsoft.com/en-us/aspnet/core/migration/webapi?view=aspnetcore-2.1 . By default, ASP.NET Core is self hosted on Kestrel (not IIS any more), and that's why OWIN is no longer needed. – Lex Li Oct 15 '18 at 00:00
  • @LexLi I'll look into that, but it looks like I need to create a new project specifically to do this. Is there any way that I could add deps and stuff to an existing .net core project? – marens101 Oct 15 '18 at 02:21
  • @Max create a new project and cut/paste your code would be the easiest way to move. – Lex Li Oct 15 '18 at 02:33
  • @LexLi I've done that, however this has caused an issue with my error handling, I've explained it better in the updated question. Any ideas what this could be? – marens101 Nov 01 '18 at 07:33

1 Answers1

1

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.

marens101
  • 101
  • 1
  • 5
  • 1
    It's nice to see that you've solved it but some detail about exactly what you used might help others. I'm migrating someone else's large project and there's something using `System.Web.Http.SelfHost`. Finding this answer doesn't really lead me to any solution of what I might replace it with. – Rory Dec 20 '20 at 19:40