App_Code 中的 SignalR Hub 导致 "Two Hubs must not share the same name" 错误

SignalR Hub in App_Code causes "Two Hubs must not share the same name" error

在 ASP.Net MVC 5 项目的根目录中创建 SignalR 2 Hub class 工作得很好。您可以访问 ~/signalr/hubs url 和它 returns 代理。您还可以将 class 放在一个名为 Hubs 或类似文件夹的文件夹中,它仍然可以正常运行。一旦将 class 放入 App_Code 特殊 ASP.Net 文件夹中,就会出现以下错误:

Exception Details: System.InvalidOperationException: Two Hubs must not share the same name. 'SignalR_MVC.EchoHub, SignalRQuickTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'SignalR_MVC.EchoHub, App_Code.gjrzytie, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' both share the name 'echo'.

附带说明一下,将 class Build ActionCompile 设置为 Content 会导致错误消失并且一切正常。我已经阅读了 App_Code 文件夹中的所有文档,但它没有提到任何关于创建 2 个 dll 的内容(这就是这里发生的事情),尽管发布应用程序只创建了 1 个 dll,并且在部署到 IIS 时一切正常.

我发现了这个错误报告:Two Hubs must not share the same name but the person never followed through and they closed the bug. I also found this SO post An unhandled exception : Two Hubs must not share the same name 但是虽然它看起来很相似,但据我所知并不相同。我找不到其他任何东西,甚至有点相似,所以我希望知道 SignalR / App_Code 的人能够阐明这种情况,并可能分享他们的 "best practice" 建议。

事实证明,如果您在 WebForms 以外的任何地方使用 App_Code 文件夹(甚至仅用于 Web 表单),那您就太顽皮了,因为这是您可以简单地添加代码页的地方(即使在实时服务器上)它们将在 运行 时间编译。

作为规则,您永远不应将 App_Code 文件夹用于实际的系统代码,它应该只用于您的 Web 表单。

我们创建了一个名为 _Code 的单独文件夹,现在将所有项目的代码放在那里,并禁止使用 App_Code,甚至将其添加到我们的新员工工具包中(因为我们不再使用 WebForms ) 以确保没有人再次被困在那里。

希望这一天能帮助其他迷失的灵魂。