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

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

问题描述

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



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

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

但在 ASP.NET 核心没有 GlobalHost



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



第二个答案提供了一种方法以获得hubcontext在请求线程之外,但这在 ASP.NET Core 中也不起作用。



因此我的问题是:如何在ASP.NET Core的请求范围之外获取中心上下文?

解决方案

从以下版本中提取当前github版本: Signalr Github
(提交: b95ac7b (在撰写本文时)



一旦您拥有了该文件,并已加载该解决方案,或者将所有三个项目添加到您现有的解决方案中,更改所有三个项目中的project.json。



Microsoft.AspNetCore.SignalR.Server-项目.json



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

  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,

现在保存文件,相关性将



Messaging Infrastructure project.json文件执行相同操作,替换任何1.1.0- *使用1.0.0



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

现在已经加载了,打开 Startup.cs



ConfigureServices内部方法,添加:

  services.AddSignalR(); 

Configure 方法内添加:

  app.UseSignalR(); 

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

 使用Microsoft.AspNetCore.SignalR.Infrastructure; 

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

 公共静态IConnectionManager ConnectionManager; 

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

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

现在,在您的集线器中/其他任何位置,而不是使用Globalhost,只需使用启动即可。例如:

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


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.

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

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

But in ASP.NET Core there is no GlobalHost.

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

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.

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

解决方案

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

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

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.

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

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

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

Inside the ConfigureServices method, add:

 services.AddSignalR();

Inside the Configure Method add:

 app.UseSignalR();

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

using Microsoft.AspNetCore.SignalR.Infrastructure;

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

public static IConnectionManager ConnectionManager;

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>();

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集线器上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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