Owin/System.Web.Http.SelfHost - 迁移到 .NET Core 后不会启动

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

我通过创建一个新项目、导入代码和安装依赖项,将项目从 .NET Framework 迁移到 .NET Core。一切都很顺利,除了 API component.Previously 我使用 System.Web.Http.Selfhost,但是现在当我尝试 运行 程序时,我得到以下异常:

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

发生在这一行,注释掉该行使程序启动成功:
https://awau.moe/ad25d4.png
此时我应该注意到 Config.BotConfig.ApiPort 的值是 8080
我也尝试过使用 OWIN 来托管,因为那似乎是更新的版本。我得到以下信息:

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

我不介意使用 OWIN 还是 Selfhost,只要它是有效

有人知道这是什么原因吗? 8080端口不是我账号的权限,我之前已经加过了,用老代码还是可以的。

我当前使用的代码位于 https://gitlab.com/marens101/mBot-Admin/tree/rewrite/mBot
- 网络服务器的主要代码在 Program.cs,第 45-55 行。 OWIN 和 SelfHost 都有,都注释掉了。
- OwinStartup 对象位于 API/OwinStartup.cs


更新:我现在正在使用 ASP.NET Core with Kestrel(感谢,@Lex Li),除了应用程序不再关闭一个未处理的异常,这是相当重要的。相反,与我的另一个 API 的连接已关闭,但网络服务器仍保持 运行ning,并且应用程序不会退出。有人知道解决这个问题的方法吗?

2 年后,我重新发现了我的旧 post 并认为我应该在(不太可能)其他人发现它在未来有用的情况下提供我的解决方案。 如问题的评论部分所示,Lex Li 指出我应该从 OWIN(基于 IIS)迁移到 ASP.NET Core(基于 Kestrel)。

正如我在问题更新中指出的那样,此解决方案运行良好。