Search code examples
c#asp.net-mvc-5signalr-hub

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


Creating a SignalR 2 Hub class in the root of an ASP.Net MVC 5 project works perfectly fine. You can access the ~/signalr/hubs url and it returns the proxy. You can also place the class in a folder called Hubs or something similar and it'll still function correctly. As soon as you place the class into the App_Code special ASP.Net folder, then you get the following error:

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'.

On a side note, setting the class Build Action from Compile to Content causes the error to go away and everything works perfectly. I've read all the documentation on the App_Code folder but it doesn't mention anything about creating 2 dll's (which is what's happening here) although publishing the application creates only 1 dll and it's all working fine when deployed to IIS.

I've found this bug report: 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 but while it does seem similar it's not the same from what I can tell. I can't find anything else even vaguely similar so I'm hoping someone who knows SignalR / App_Code would be able to shed some light on this condition and possibly share their "best practice" advice.


Solution

  • So it turns out, you're being VERY naughty if you use the App_Code folder in anything but WebForms (and even then only for web forms) as that's the place where you can simply add code pages (even on the live server) and they'll compile at run time.

    As a rule, you should NEVER use the App_Code folder for actual system code, it should be for your web forms only.

    We created a separate folder called _Code and place our code in there now for all projects and have banned the use of App_Code and have even added it to our new hire kit (since we no longer use WebForms) to ensure no one gets trapped there ever again.

    Hope this one day helps out some other lost soul.