简体   繁体   English

如何在ASP.NET MVC 4中使用Signalr V2 Beta

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

Before v2: 在v2之前:

RouteTable.Routes.MapHubs();

In v2, MapHubs does not exist anymore. 在v2中,MapHubs不再存在。 The wiki says to add a Startup class and a Configuration method and a call to app.MapHubs(). Wiki说要添加一个Startup类和一个Configuration方法以及对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 我想有些代码要添加到global.asax.cs

What is the secret ? 秘密是什么?

尝试定义[assembly:OwinStartup(typeof(MyAssembly.Startup))],以查看您的Startup类是否被拾取。

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. 还要同时更新MVC4(不是预发行版,而是最新的稳定版本)和SignalR / owin nuget。

plus fix bugs in js client : 以及修复js客户端中的错误:

  • if disconnectTimeout=999000 then it is disabled. 如果disconnectTimeout = 999000,则将其禁用。 Must be set server-side with: GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(999); 必须在服务器端设置:GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(999); note: server side can not set a value < 6 (signalr throws an exception if DisconnectTimeout < 6). 注意:服务器端不能将值设置为<6(如果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 webSockets:在sockettransport中设置connection.socket = null,否则(手动)停止调用后,开始调用将失败
  • serverSentEvents: prevent error caused by a reconnection attempt when the window is unloading serverSentEvents:防止在卸载窗口时由重新连接尝试导致的错误
  • chrome fails with exception if signalr hub url not available (temporarily) : Instead of giving up try the next available protocol / try to reconnect. 如果Signalr Hub网址不可用(临时),则chrome会失败,但会异常:不要放弃尝试下一个可用协议/尝试重新连接。

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 删除对旧版SignalR的所有引用,即nuget卸载库并仔细检查/ bin

  • Installed SignalR 2.0.0-beta2 via Package Manager Console Install-Package Microsoft.AspNet.SignalR -Pre 通过程序包管理器控制台Install-Package Microsoft.AspNet.SignalR -Pre SignalR 2.0.0-beta2已安装程序Install-Package Microsoft.AspNet.SignalR -Pre

  • Following the steps in the 1.x to 2.0 migration outlined here 遵循此处概述的从1.x到2.0迁移中的步骤

  • And most importantly changing the project config to use Local IIS Web server instead of Visual Studio Developer Server (Cassini). 最重要的是,将项目配置更改为使用本地IIS Web服务器而不是Visual Studio Developer Server(Cassini)。

More info in the question/answer I posted here 我在此处发布的问题/答案中的更多信息

In web.config there must be a fully qualified name of the class, eg 在web.config中,必须有该类的完全限定名称,例如

<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! 当我使用名称空间而不是程序集名称时遇到了一个问题,但是使用完全限定的名称时,它可以对web.config进行任何其他更改!

UPDATE: I also followed the steps from the link: http://www.asp.net/vnext/overview/latest/release-notes#TOC13 , ie removed a NuGet package "Microsoft.AspNet.SignalR.Owin" 更新:我还按照以下链接中的步骤操作: http : //www.asp.net/vnext/overview/latest/release-notes#TOC13 ,即删除了NuGet包“ Microsoft.AspNet.SignalR.Owin”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM