在Windows上使用Kestrel服务器运行ASP.NET5 Web应用程序

3
我已经成功地在Windows 8.1上使用Kestrel服务器运行了来自ASP.NET5 GitHub库的“HelloWeb”示例,方法如下:
dnx . kestrel
现在我想使用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。 - agua from mars
1
如果您在VS中以调试模式运行它,应该能够在异常上中断并检查LoaderExceptions属性,就像错误消息建议的那样。 - lgoncalves
@Igoncalves:我在命令行中运行了ASP.NET5示例,但是在VS中有自己的应用程序。我能够按照您建议的方式设置断点。我将使用新结果更新我的问题... - NicolasR
“Nuget 管理器只建议我安装 Kestrel beta 4,而其他所有包都已安装为 beta 6。” @NicolasR,Kestrel 似乎落后于 DNX 的其他部分。我刚遇到了同样的问题,NuGet 为 DNX beta 8 安装了 Kestrel beta 6。 - CrazyPyro
2个回答

3
该错误似乎表明运行时与应用程序期望的运行时不匹配,并且可能针对特定运行时可用的依赖项也存在问题。
首先使用“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可能会自动为非核心clr x86 dnx的beta4版本恢复依赖项,但由于路径(由dnvm设置),当您在命令行上执行时,您正在执行core clr x64的beta6版本。如果这些依赖项没有被恢复,则会出现错误。

1
为了找到异常的真正原因,您可以使用dnx的--debug开关来调试此错误。尝试:
dnx --debug . kestrel

然后您可以使用Visual Studio连接到dnx.exe,并查看确切的异常并检索其LoaderExceptions属性。我曾经遇到过类似的问题,都是由运行时不匹配和源代码错误引起的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接