Signalr Hub in Separate Dll

2019-05-23 06:37发布

问题:

I had a hub hosted in a console app with a WPF app connecting to it. It worked just fine. Then I moved the hub into a separate project and added a reference from the host to new project. Now I am getting a 500 error, with no other details.

Is there anything different that needs to be done in order to host a hub from another assemble/namespace?

Edit: I tried a few things after opening the question. Here is what I have tried so far:

  • Setting the HubName attribute. - Did not work.
  • Passing the full namespace + class into HubConnection.CreateHubProxy. - Did not work.
  • Putting my hub class into the same project but a different namespace. - This worked.

Here is the exception that gets passed back. "System.Net.WebException: The remote server returned an error: (500) Internal Server Error. at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at Microsoft.AspNet.SignalR.Client.Http.HttpHelper.<>c_DisplayClass2.b_0(IAsyncResult ar) at System.Threading.Tasks.TaskFactory1.FromAsyncCoreLogic(IAsyncResult iar, Func2 endFunction, Action1 endAction, Task1 promise, Boolean requiresSynchronization)"

回答1:

This worked for me (in startup)

AppDomain.CurrentDomain.Load(typeof(MyHub).Assembly.FullName);

Credits to map hubs in referenced project



回答2:

Try to see the http response message

        try
        {
            hubConnection.Start().Wait();
        }
        catch (Exception error)
        {
            if (error.InnerException is WebException)
            {
                WebException webError = (WebException)error.InnerException;
                Console.WriteLine(webError.Status);
                Console.WriteLine(webError.Response);
            }
        }


回答3:

Have you moved your call to register your hubs to the new dll? I.e. the following needs to be in your new project (but still called from global.asax):

RouteTable.Routes.MapHubs();

You probably also need to use the [WebActivator PreApplicationStart attribute] (http://msdn.microsoft.com/en-us/library/system.web.preapplicationstartmethodattribute.aspx) in the new RegisterHubs class to ensure it is registered in time



回答4:

Make sure the separate DLL has a reference to Microsoft.CSharp.dll - which is required for dynamic code.