【问题标题】:Run ASP.NET5 web application with Kestrel server on Windows在 Windows 上使用 Kestrel 服务器运行 ASP.NET 5 Web 应用程序
【发布时间】:2015-11-26 16:23:56
【问题描述】:

我已使用 Windows 8.1 上的 Kestrel 服务器成功运行 ASP.NET5 GitHub 存储库中的“HelloWeb”示例:

dnx 。红隼

现在我想用 kestrel 运行我自己的应用程序。我从 Visual Studio 2015RC 和几个运行时直接使用 dnx 进行了尝试。结果总是:

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the
 requested types. Retrieve the LoaderExceptions property for more information.
   at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   at System.Reflection.RuntimeAssembly.get_DefinedTypes()
   at Microsoft.AspNet.Hosting.Server.ServerLoader.LoadServerFactory(String serv
erFactoryIdentifier)
   at Microsoft.AspNet.Hosting.Internal.HostingEngine.BuildApplication()
   at Microsoft.AspNet.Hosting.Internal.HostingEngine.Start()
   at Microsoft.AspNet.Hosting.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Framework.Runtime.Common.EntryPointExecutor.Execute(Assembly ass
embly, String[] args, IServiceProvider serviceProvider)
   at Microsoft.Framework.ApplicationHost.Program.ExecuteMain(DefaultHost host,
String applicationName, String[] args)
   at Microsoft.Framework.ApplicationHost.Program.Main(String[] args)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.Framework.Runtime.Common.EntryPointExecutor.Execute(Assembly ass
embly, String[] args, IServiceProvider serviceProvider)
   at dnx.host.Bootstrapper.RunAsync(List`1 args, IRuntimeEnvironment env)
   at dnx.host.RuntimeBootstrapper.ExecuteAsync(String[] args)
   at dnx.host.RuntimeBootstrapper.Execute(String[] args)

有什么方法可以找到哪些类型无法加载?

更新

这里是 project.json

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.AspNet.Mvc": "6.0.0-beta6-14023",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta6-11864",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta6-12245",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta6-11996",
    "EntityFramework.SqlServer": "7.0.0-beta6-13336",
    "EntityFramework.Commands": "7.0.0-beta6-13336",
    "Microsoft.Framework.ConfigurationModel.Json": "1.0.0-beta5-11337",
    "Microsoft.AspNet.Identity.EntityFramework": "3.0.0-beta6-12521",
    "Microsoft.AspNet.SignalR.Server": "3.0.0-beta6-12519",
    "Microsoft.AspNet.Authentication.OAuthBearer": "1.0.0-beta4",
    "Kestrel": "1.0.0-beta4"
  },

  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",
    "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5004",
    "ef": "EntityFramework.Commands"
  },

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],

  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ]

}

我尝试在 VS 中使用 Kestrel 启动它,并将 Kestrel 设置为使用以下运行时,这导致在 VS 中显示错误消息。

运行时:1.0.0-beta6-11921 .NET Framework x86

Exception thrown: 'System.Reflection.ReflectionTypeLoadException' in mscorlib.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'System.Reflection.ReflectionTypeLoadException' in mscorlib.dll
Exception thrown: 'System.Reflection.ReflectionTypeLoadException' in Microsoft.Framework.ApplicationHost.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
Exception thrown: 'System.Reflection.ReflectionTypeLoadException' in mscorlib.dll
Exception thrown: 'System.Reflection.ReflectionTypeLoadException' in dnx.host.dll

最终更新

问题出在 kestrel 软件包版本上。无论出于何种原因,Nuget 经理只建议我使用 Kestrel beta 4,而所有其他软件包都安装为 beta 6。在 project.json 中手动将 Kestrel 设置为 beta 6 后,一切正常。我接受 Gerald Davies 的回答,因为他给了我正确的方向。

【问题讨论】:

  • 您使用哪个运行时?运行时版本必须与依赖版本一致,我建议使用最后一个稳定版本:beta4
  • 如果您在 VS 内调试时运行它,您应该能够中断异常并检查 LoaderExceptions 属性,如错误消息中所建议的那样。
  • @Igoncalves:我确实在命令行中运行了 ASP.NET5 示例,但在 VS 中有自己的应用程序。我能够按照您的建议设置断点。我将使用新结果更新我的问题...
  • "Nuget manager only suggested me Kestrel beta 4 while all other packages got installed as beta 6" @NicolasR ,Kestrel 似乎落后于 DNX 的其余部分。我刚刚遇到了同样的问题,NuGet 为 DNX beta 8 安装了 Kestrel beta 6。

标签: asp.net-core


【解决方案1】:

该错误似乎表明正在使用的运行时、应用程序预期的运行时以及特定运行时可用的依赖项之间不匹配。

首先使用dnvm list 并验证正在使用哪个运行时(活动)。请在更新中发布。如果错误的运行时设置为活动,您将需要使用dnvm use 更改它。还要将此与您的解决方案中的 global.json 值进行比较。

您可能需要恢复依赖项。如果 VS 中的构建使用的运行时与 dnvm 设置为活动的运行时不同,则可能会发生这种情况。你可以通过dnu restore强制恢复依赖。

始终需要“dnu restore”,但 VS 有时会在依赖项发生变化时自动执行此操作来隐藏它,但仅适用于当前运行时 VS 正在执行的操作。请记住,依赖项可能因运行时而异。

这很复杂,因为 VS 使用 global.json 来确定要针对哪个 dnx(版本、运行时和架构)执行但在命令行上,除非明确指出 dnx/dnu 使用由 dnvm 设置的“活动”运行时.从技术上讲,dnvm 只是设置特定运行时的路径,因此当使用执行 dnx 或 dnu 时,您正在运行特定的运行时(版本、运行时和架构),具体取决于路径是什么。

关键是VS执行的dnx和你在命令行执行的dnx不一定相同。在 VS (global.json) 或 dnvm (dnvm use) 中更改“目标”不会自动更新另一个。

因此,例如,VS 可能会自动恢复 beta4 版本的非核心 clr x86 dnx 的依赖关系,但由于路径(由 dnvm 设置),当您在命令行上执行时,您正在针对核心 clr x64 的 beta6 版本执行.如果这些依赖项还没有恢复,那么你会得到一个错误。

【讨论】:

    【解决方案2】:

    要找到异常的真正原因,可以使用dnx的--debug开关调试这个错误。试试:

    dnx --debug . kestrel
    

    然后您可以使用 Visual Studio 附加到 dnx.exe 并查看确切的异常并检索它的 LoaderExceptions 属性。我有similar problems,都是由运行时不匹配和源代码错误引起的。

    【讨论】:

      猜你喜欢
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-21
      • 2020-03-09
      • 2011-01-20
      • 2012-12-25
      • 2021-10-15
      相关资源
      最近更新 更多