MVC 6,.Net461上的SignalR

时间:2016-07-19 18:19:02

标签: c# asp.net signalr asp.net-core-mvc

我试图找到一种方法来启动signalR并映射Hubs ,这是因为NHibernate依赖于.Net461的MVC6应用程序。

我尝试使用" Microsoft.AspNet.SignalR.Server":" 3.0.0-beta5" ,但我仍然在包恢复上遇到错误:

  

"依赖项Microsoft.AspNet.SignalR.Server 3.0.0-beta5没有   支持框架.NetFramework,Version = v4.6.1。"

{
  "dependencies": {
    "Domain": "1.0.0-*",
    "Globalization": "1.0.0-*",
    "Infra": "1.0.0-*",
    "Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
    "Microsoft.AspNet.SignalR": "2.2.1",
    "Microsoft.AspNet.SignalR.Client": "2.2.1",
    "Microsoft.AspNet.SignalR.Core": "2.2.1",
    "Microsoft.AspNet.SignalR.JS": "2.2.1",
    "Microsoft.AspNet.SignalR.Owin": "1.2.2",
    "Microsoft.AspNet.SignalR.SystemWeb": "2.2.1",
    "Microsoft.AspNet.SignalR.Server":"3.0.0-beta5",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    "Util": "1.0.0-*",
    "UtilTagHelpers": "1.0.0-*"
  },

    "tools": {
        "BundlerMinifier.Core": "2.0.238",
        "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
        "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
    },

    "commands": {
        "web": "Microsoft.AspNet.Server.Kestrel --server.urls http://*:5004"
    },
    "frameworks": {
        "net461": {
            "frameworkAssemblies": {
                "System.Runtime": {}
            }
        }
    },

    "buildOptions": {
        "emitEntryPoint": true,
        "preserveCompilationContext": true
    },

    "publishOptions": {
        "include": [
            "wwwroot",
            "Views",
            "Areas/**/Views",
            "appsettings.json",
            "web.config"
        ]
    },

    "scripts": {
        "prepublish": [ "bower install", "dotnet bundle" ],
        "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
    }
}

我也试过了owin服务器,但它也没有启动。

using Owin;
using Microsoft.Owin;
using Microsoft.AspNet.SignalR;

[assembly: OwinStartup(typeof(RLG.SignalR.OwinStart))]

namespace RLG.SignalR
{
    public class OwinStart
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

在这种情况下使用SignalR的更好方法是什么?

2 个答案:

答案 0 :(得分:2)

为了在net461框架上运行ASP.Net Core使用SignalR,你必须做一些设置。

1)您需要在“net461”配置中导入“dnxcore50”框架。

"frameworks": {
    "net461": {
      "imports": "dnxcore50"
    }
  },

2)您需要在Web应用程序项目的根目录中创建一个新文件 NuGet.config 。用以下内容替换内容后:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="AspNetCore" value="https://dotnet.myget.org/F/aspnetcore-ci-dev/api/v3/index.json" />
    <add key="NuGet" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

3)将相应的依赖项添加到 project.json 文件中。

"Microsoft.AspNetCore.SignalR.Server": "0.2.0-*"
"Microsoft.AspNetCore.WebSockets": "0.2.0-*"

4)将相应的依赖项添加到 bowser.json 文件中。

"signalr": "^2.2.1"

5)将 Hubs 文件夹添加到Web应用程序项目的根目录。

6)在 Startup.cs 文件中,在配置(app,env,loggerFactory)方法中添加以下行。

app.UseSignalR();

7)在 Startup.cs 文件中,在 ConfigureServices(services)方法中添加以下行。

services.AddSignalR(options => options.Hubs.EnableDetailedErrors = true);

此时,在C#和JavaScript中设置集线器与旧版本或SignalR相同。我已成功地在.Net框架上运行目标4.6.1。

部分功劳:https://chsakell.com/2016/10/10/real-time-applications-using-asp-net-core-signalr-angular/

答案 1 :(得分:0)

使用最新的稳定(读取:非测试版)版本,应该可以正常使用

编辑:似乎是版本2.2.1