How to use signalr v2 beta in asp.net mvc 4

2019-06-28 03:45发布

问题:

Before v2:

RouteTable.Routes.MapHubs();

In v2, MapHubs does not exist anymore. The wiki says to add a Startup class and a Configuration method and a call to app.MapHubs().

namespace MyAssembly 
{
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        //Before v2
        //RouteTable.Routes.MapHubs();

        app.MapHubs();
    }
}
}

But the method is never called, no error occurs, and ... no hub are setup.

I suppose there is some code to add to global.asax.cs

What is the secret ?

回答1:

Try defining [assembly : OwinStartup(typeof(MyAssembly.Startup))] to see if your Startup class is being picked up.



回答2:

EDIT: removed lines not relevant.

Solution !

<appSettings>
    <add key="owin:AppStartup" value="MyNameSpace.Startup, MyNameSpace" />
</appSettings>

plus update both MVC4 (not to prerelease, but to latest stable version) and SignalR/owin nugets.

plus fix bugs in js client :

  • if disconnectTimeout=999000 then it is disabled. Must be set server-side with: GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(999); note: server side can not set a value < 6 (signalr throws an exception if DisconnectTimeout < 6). So use this magic number.
  • webSockets: set connection.socket = null in sockettransport, otherwise the call to start fails after a (manual) call to stop
  • serverSentEvents: prevent error caused by a reconnection attempt when the window is unloading
  • chrome fails with exception if signalr hub url not available (temporarily) : Instead of giving up try the next available protocol / try to reconnect.


回答3:

I was able to get the 2.0 beta working by

  • Removing all references to the older version of SignalR, ie nuget uninstall of the library and double checking /bin

  • Installed SignalR 2.0.0-beta2 via Package Manager Console Install-Package Microsoft.AspNet.SignalR -Pre

  • Following the steps in the 1.x to 2.0 migration outlined here

  • And most importantly changing the project config to use Local IIS Web server instead of Visual Studio Developer Server (Cassini).

More info in the question/answer I posted here



回答4:

In web.config there must be a fully qualified name of the class, e.g.

<appSettings>
    <add key="owin:AppStartup" value="**My.Name.Space.Startup**, **AssemblyName**" />
</appSettings>

I had a problem when I put namespace instead of assembly name, but with the fully qualified name it works without any other changes to web.config!

UPDATE: I also followed the steps from the link: http://www.asp.net/vnext/overview/latest/release-notes#TOC13, i.e. removed a NuGet package "Microsoft.AspNet.SignalR.Owin"