有没有一种从控制器调用SignalR hub中的方法的好方法?
现在我有这个:
public class StatsHub : Hub
{
private static readonly Lazy<StatsHub> instance = new Lazy<StatsHub>(() => new StatsHub());
public static StatsHub Instance { get { return instance.Value; } }
public StatsHub()
{
if (this.Clients == null)
{
var hubContext = SignalR.GlobalHost.ConnectionManager.GetHubContext<StatsHub>();
this.Clients = hubContext.Clients;
this.Groups = hubContext.Groups;
}
}
// methods here...
}
Run Code Online (Sandbox Code Playgroud)
所以在我的控制器动作中,我可以说,例如
StatsHub.Instance.SendMessage("blah");
Run Code Online (Sandbox Code Playgroud)
它几乎是好的,除了hubContext没有Hub的Caller或Context属性 - 这很好.
希望有更好的方法吗?
将Python与SignalR集成有哪些选择?
Python代码是大型第三方产品的一部分,而不是语言偏好.SignalR服务器提供对现有.NET产品的订阅.
我们想在Python中重用.NET SignalR服务器.
我有一个带有简单HTML页面的ASP.NET Web应用程序和一些通过SignalR进行通信的JavaScript.这很好.现在我试图从另一个项目(在同一个解决方案中)调用Hub上的方法,并使用.NET Signalr Client Api:
var connection = new HubConnection("http://localhost:32986/");
var hub = connection.CreateHubProxy("MessageHub");
connection.Start();
hub.Invoke("SendMessage", "", "");
Run Code Online (Sandbox Code Playgroud)
最后一行导致InvalidOperationException: The connection has not been established.但我可以从我的JavaScript代码连接到集线器.
如何使用C#代码连接到Hub?
UPDATE
写完这篇文章后的那一刻,我试着添加.Wait()并且它有效!所以这将做:
var connection = new HubConnection("http://localhost:32986/");
var hub = connection.CreateHubProxy("MessageHub");
connection.Start().Wait();
hub.Invoke("SendMessage", "", "");
Run Code Online (Sandbox Code Playgroud) 我刚刚更新了一些SignalR引用,但为了允许一般类型的Hub,情况有所改变Hub<T>.在现有的示例和文档中,例如:
我们有一个静态类,通过以下机制保存对客户端的引用:
public class StockTicker()
{
private readonly static Lazy<StockTicker> _instance = new Lazy<StockTicker>(
() => new StockTicker(GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>().Clients));
IHubConnectionContext Clients {get;set;}
private StockTicker(IHubConnectionContext clients)
{
Clients = clients;
}
}
Run Code Online (Sandbox Code Playgroud)
因此检查静态引用,如果为null则它会到达:
GlobalHost.ConnectionManager.GetHubContext<StockTickerHub>().Clients
Run Code Online (Sandbox Code Playgroud)
创建实例并通过构造函数提供客户端.
所以这就是它以前的工作方式,而且确实是上面的网址是如何工作的.但现在Hub<T>构造函数需要稍微改变:
private StockTicker(IHubConnectionContext<dynamic> clients)
{
Clients = clients;
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是如何进一步扩展这一点,以便我的StockTicker版本可以为x类型的客户端提供强类型属性.
IHubConnectionContext<StockTickerHub> Clients {get;set;}
private StockTicker(IHubConnectionContext<dynamic> clients)
{
Clients = clients; // Fails, wont compile
}
Run Code Online (Sandbox Code Playgroud)
通过维护强类型引用,我可以调用强类型方法等.
我正在MassTransit和rabbitmq的帮助下开发分布式应用程序
我必须提供在网页上生成报告而无需通过点击按钮重新加载页面的能力,我也应该调用Windows服务进行数据准备(该服务处理每个请求30秒 - 1分钟).
我基于此示例的第一次尝试:https://github.com/MassTransit/Sample-RequestResponse
[HttpPost]
public async Task<HttpStatusCodeResult> GenerateReport(string someJsonData)
{
var serviceAddress = new Uri(ConfigurationManager.AppSettings["BaseLineRecordService"]);
var client = this.Bus.CreateRequestClient<ICreateReportRequest, ICreateReportResponse>(serviceAddress, TimeSpan.FromHours(1));
ICreateReportResponse response = await client.Request(new CreateReportRequest());
reportHub.ShowRepordData(response); // Update data by SingleR
return new HttpStatusCodeResult(200);
}
Run Code Online (Sandbox Code Playgroud)
但据我所知,这不是一个更好的方法,因为我在所有数据准备期间都保持联系.
我读了很多文章,我找到了三种方法.哪种方式更受欢迎?
1)喜欢这篇文章http://www.maldworth.com/2015/07/19/signalrchat-with-masstransit-v3/
2)作为第一个但使用Rest API调用而不是IIS端的消费者
3)本文的想法http://weblog.west-wind.com/posts/2013/Sep/04/SelfHosting-SignalR-in-a-Windows-Service
我有理由在Unity中使用SignalR,我决定使用NuGet的Microsoft.AspNetCore.SignalR.Client.
我已经尝试了一个包Nivot.SignalR.Client.Net35,但是它使用的是不再使用的AspNet.SignalR.Client,我已经使用Microsoft.AspNetCore.SignalR在我的WebApi解决方案上创建了我需要的一切,然后我改变了我的PlayerSettings中的Unity项目脚本Tuntime版本.NET 4.x与.NET Standard 2.0等效和Api兼容级别,与AspNetCore.SignalR.Client兼容.
但是在我编辑脚本的同时在visual studio中安装软件包并尝试使用它后,Visual Studio保持冷静,但Unity说名称空间Microsoft.AspNetCore.SignalR.Client不存在.我已经读过我必须将它们复制到Assets文件夹中的文件夹,所以我做了,然后Unity说"卸载破坏的程序集资产/软件包/ Microsoft.AspNetCore.SignalR.Client.dll,这个程序集可能导致崩溃运行时"它不起作用.
我失去了希望.但后来我记得有Stackoverflow,所以现在我希望有人可以帮助我.谢谢.
我的 .NET SignalR 客户端代码在 Core 3 (WPF) 中不再工作:
string hubAddress = "https://localhost:44329/Hub";
HubConnection hub = new HubConnectionBuilder().WithUrl(hubAddress).Build(); // WithUrl not found
Run Code Online (Sandbox Code Playgroud)
有人知道怎么修这个东西吗?
我已经为 REST API 设置了 Azure SignalR 服务。设置下共有三种模式:Default、Serverless、Classic。我找不到有关这些项目中的每一个设置的任何信息。到目前为止,我唯一得到的一件事是,如果我将 Azure SignalR 用于 Azure Functions 或 REST API,则最好使用Serverless option。
从文档:
仅当通过 Azure Functions 绑定或 REST API 使用 Azure SignalR 服务时,才将服务模式设置更改为无服务器。否则将其保留为经典或默认。
ASP.NET SignalR 应用程序不支持无服务器模式。始终对 Azure SignalR 服务实例使用默认或经典。
你能帮我找出每个选项的设置吗?
azure signalr asp.net-core-signalr azure-signalr azure-rest-api
在 blazor 中,我用于NavigationManager.NavigateTo(url)更改窗口位置,但是如何使用它打开具有指定 URL 的新选项卡,而无需在上调用 JSOnAfterRenderAsync()
我是新来的信号r,我正在尝试在c#visual studio 2012中创建一个基本的聊天应用程序,但我收到了以下错误.
The following errors occurred while attempting to load the app.
- No assembly found containing an OwinStartupAttribute.
- The discovered startup type 'SignalRTutorials.Startup, SignalRTutorials, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
conflicts with the type 'Microsoft.VisualStudio.Web.PageInspector.Runtime.Startup, Microsoft.VisualStudio.Web.PageInspector.Runtime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Remove or rename one of the types, or reference the desired type directly.
To disable OWIN startup discovery, add the appSetting owin:AutomaticAppStartup with a value of "false" in your web.config.
To specify the OWIN startup Assembly, Class, or Method, add the …Run Code Online (Sandbox Code Playgroud) signalr ×10
c# ×5
.net-core ×1
asp.net ×1
asp.net-mvc ×1
azure ×1
blazor ×1
integration ×1
javascript ×1
masstransit ×1
nuget ×1
owin ×1
python ×1
rabbitmq ×1
signalr-hub ×1
wpf ×1