在 ASP.NET 5 应用程序中使用 SignalR 2

Using SignalR 2 in ASP.NET 5 application

SignalR 3 和 ASP.NET 5 在 beta7 版本之前可以很好地协同工作。现在 Microsoft 声明 SignalR 3 是 'on hold',并且不要指望两者在不久的将来一起工作:

https://github.com/aspnet/SignalR-Server/issues/119

https://github.com/aspnet/SignalR-Server/issues/121

所以问题是:有没有办法让 SignalR 2 在 ASP.NET 5 应用程序中至少工作?

在这篇文章中找到了使用owin兼容中间件的通用解决方案: https://lbadri.wordpress.com/2014/11/01/asp-net-vnext-middleware-versus-owinkatana-middleware/

  1. 引用 Microsoft.AspNet.Owin
  2. 将以下代码插入 Startup.Configure:
app.UseOwin(addToPipeline =>
{
    addToPipeline(next =>
    {
        var appBuilder = new AppBuilder();
        appBuilder.Properties["builder.DefaultApp"] = next;

        appBuilder.MapSignalR();

        return appBuilder.Build<AppFunc>();
    });
});

SignalR 2 也可以在 .NET Core 2 上运行,当您深入了解内部结构时:

// SignalR checks if it's running in a Mono environment and then
// disables features like performance counters
// .NET Core isn't Mono, but doesn't have the performance counters DLL
// Let's make .NET Core a Mono
var signalRAssembly = typeof(Microsoft.AspNet.SignalR.PersistentConnection).Assembly;
// This type is internal
var monoUtility = signalRAssembly.GetType("Microsoft.AspNet.SignalR.Infrastructure.MonoUtility");
var field = monoUtility.GetField(
    "_isRunningMono",
    BindingFlags.NonPublic | BindingFlags.Static
);
field.SetValue(null, new System.Lazy<bool>(() => true));