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

谁说胖子不能爱 提交于 2019-12-10 17:24:43

问题


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"



来源:https://stackoverflow.com/questions/17862446/how-to-use-signalr-v2-beta-in-asp-net-mvc-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!