请求线程外的 ASP.NET Core RC2 SignalR Hub 上下文 [英] ASP.NET Core RC2 SignalR Hub Context outside request thread

查看:29
本文介绍了请求线程外的 ASP.NET Core RC2 SignalR Hub 上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在试用 ASP.NET CoreRC2 版本,但我遇到了 SignalR 的问题.我需要能够在请求线程之外向客户端发送消息.

I am currently trying out the RC2 release of ASP.NET Core and I am running into an issue with SignalR. I need to be able to send messages to the client outside of the request thread.

现在在完整的 .NET 框架中,您可以这样做:

Now in the full .NET framework you can do this like:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.<SendMessage>()

但是在 ASP.NET Core 中没有 GlobalHost.

But in ASP.NET Core there is no GlobalHost.

我发现了一个类似的问题:如何在 vNext 项目中获取 SignalR Hub 上下文?

I found a similar question: How to get SignalR Hub Context in a vNext Project?

其中第二个答案提供了一种在请求线程之外获取集线器上下文的方法,但这在 ASP.NET Core 中也不起作用.

Where the second answer provides a method to get the hubcontext outside the request thread, but this also does not work in ASP.NET Core.

所以我的问题是:如何在 ASP.NET Core 的请求范围之外获取集线器上下文?

So my question is: how can I get the hub context outside of the request scope in ASP.NET Core?

推荐答案

您必须从以下位置拉取当前的 github 版本:Signalr Github(提交:b95ac7b 在撰写本文时)

You will have to pull the current github version from : Signalr Github (Commit: b95ac7b at time of writing)

一旦你有了这个,并加载了解决方案,或者将所有三个项目添加到你现有的解决方案中,你将需要更改所有三个项目中的 project.json.

Once you have this, and have loaded the solution, or added all three of the projects to your existing solution, you will need to change project.json in all three projects.

Microsoft.AspNetCore.SignalR.Server - project.json

您将看到对每个程序集版本 1.1.0-* (RC3) 的引用.将这些更改为当前的 RC2,直到您看到以下内容

You will see references to version 1.1.0-* (RC3) of each assembly. Change these to the current RC2, until you see the following

"Microsoft.AspNetCore.DataProtection": "1.0.0",
"Microsoft.AspNetCore.Hosting.Abstractions": "1.0.0",
"Microsoft.AspNetCore.Http.Extensions": "1.0.0",
"Microsoft.Extensions.DependencyModel": "1.0.0",

现在保存文件,依赖项将更新.

Now save the file, and the dependencies will update.

MessagingInfrastructure project.json 文件执行相同操作,将任何 1.1.0-* 替换为 1.0.0

Do the same with The Messaging and Infrastructure project.json file's, replacing any 1.1.0-* with 1.0.0

完成后,您可以向 Microsoft.AspNetCore.SignalR.Server 的主项目添加项目引用

Once that is done, you can add a project reference to your main project of Microsoft.AspNetCore.SignalR.Server

既然你已经加载了,打开你的Startup.cs

Now that you have that loaded, Open your Startup.cs

ConfigureServices 方法中,添加:

 services.AddSignalR();

Configure方法中添加:

 app.UseSignalR();

接下来,添加 using 语句并导入 Infrastructure 命名空间,如下所示:

Next, add a using statement and import the Infrastructure namespace as follows:

using Microsoft.AspNetCore.SignalR.Infrastructure;

最后在 Startup.cs 中创建一个名为 ConnectionManager 的静态属性,如下所示:

And finally Create a static property in Startup.cs called ConnectionManager as follows:

public static IConnectionManager ConnectionManager;

最后在Startup.cs中的Configure方法中添加一个IServiceProvider属性(需要导入System命名空间).然后从中加载 ConfigurationManager.

Finally add a IServiceProvider property to the Configure method in Startup.cs (Need to import the System namespace). Then Load the ConfigurationManager from this.

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
    {
        ConnectionManager = serviceProvider.GetService<IConnectionManager>();

现在,在您的集线器/其他任何地方,无需使用 Globalhost,只需使用启动即可.例如:

Now, in your hubs / anywhere else, instead of using Globalhost, Simply use startup. For example:

IHubContext context = Startup.ConnectionManager.GetHubContext<SomeHub>();
context.Clients.All.someMethod();

这篇关于请求线程外的 ASP.NET Core RC2 SignalR Hub 上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆