- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我开始使用 SignalR(.Net 客户端),在我目前看到的示例中,它们创建连接,然后创建集线器代理,然后启动连接,例如:-
var hubConnection = new HubConnection("http://localhost:8080/");
var fooProxy = hubConnection.CreateHubProxy("FooHub");
var barProxy = hubConnection.CreateHubProxy("BarHub");
hubConnection.Start().Wait();
// The proxies can now be used
我的服务器将由多个集线器组成(主要为客户端提供请求-响应式操作),但客户端应该创建它可能需要的所有代理感觉不对启动,然后将它们传递给客户端中的其他类使用。
是否可以仅在需要时(例如在方法中)创建集线器代理,然后在完成时将其丢弃? (我已经试过了,但是一旦连接开始,SignalR 将不允许您创建集线器代理)。或者创建中心代理是否“昂贵”,因此为什么要“预先”完成?
如果我坚持以这种方式创建代理,那么将它们暴露给其余客户端代码的好方法是什么?某种单例工厂?
最佳答案
错误信息很清楚——连接建立后不能创建代理。在建立连接时,SignalR 需要知道您对哪些集线器感兴趣。来自 documentation :
For JavaScript clients you have to register at least one event handler before calling the Start method to establish the connection. This is not necessary for .NET clients. For JavaScript clients, the generated proxy code automatically creates proxies for all Hubs that exist on the server, and registering a handler is how you indicate which Hubs your client intends to use. But for a .NET client you create Hub proxies manually, so SignalR assumes that you will be using any Hub that you create a proxy for.
(我相信这基本上归结为他们如何在幕后设计他们的协议(protocol),并且基本上是当建立连接时,发送的第一个信息是关于正在使用的集线器,然后是连接只是用来传递实际的方法调用信息,但我绝不是专家)
Is it possible to create a hub proxy only when it's needed (e.g. in a method) then dispose of it when finished with?
除非您还想建立一个单独的 HubConnection
对象,否则不会。取决于您希望每个客户端创建多少个连接。
Or is it "expensive" to create a hub proxy, hence why it's all done "up front"?
代理并不昂贵,但启动 HubConnection
是昂贵的。在决定他们会让您在开始连接之前创建代理之后,显然您会发现的每个示例都会这样做。
If I'm stuck with creating the proxies in this way, what would be a good way of exposing them to the rest of the client code? Some kind of singleton factory?
无论您最喜欢什么 - 如果您愿意,可以将它们存储在静态类的静态字段中(最接近 C# 的全局变量)。对我来说,除了玩具项目之外的任何东西都倾向于总是有一个依赖注入(inject)组件,所以对我来说,我会尽早创建代理并将它们放入 DI 容器中。它们可以安全地作为单例存储在容器中,因为:
The proxy object is thread-safe. In fact, if you call
HubConnection.CreateHubProxy
multiple times with the same hubName, you get the same cachedIHubProxy
object.
关于c# - 在 .Net 客户端中创建多个 Signalr hub 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21112147/
这是 Controller 方法,它接收应该被踢出的userName。 public ActionResult Kick(string userName) { var h
ASP.NET SignalR Hubs API 指南 - 服务器 ( https://learn.microsoft.com/en-us/aspnet/signalr/overview/guide-
我知道程序以及如何从 GitHub 或 BitBucket 配置自动化 docker hub。 但是我现在要做的是创建两个名为 Test/main 的 docker hub 存储库。和 Test/de
这是场景: Device1同时发送两条iothub D2C消息: 这会将标志设置为 true 这会将标志设置为 false 物联网消息路由将消息路由到 eventhub,这将触发 EventHubTr
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我是 selenium 的新手,我一直在尝试启动 Selenium Hub 并使用 java 将节点添加到 Hub。我已使用命令提示符启动它,但我希望它自行发生。我能够启动 Hub,但不知道如何添加节
我正在使用 SignalR 项目,我想在 WebApi 项目和 Web 项目中使用 Hub。所以我创建了一个类库项目并在那里实现了 Hub。 我的项目结构如下: -ChatHub -Hub -We
在开始之前,我想提一下,这并不完全是重复的,其他类似的问题对我的情况没有任何有用的答案。 在我的 ASP.Net Core 2.1 中,我有一个可以使用 AJAX 调用的操作(它的名称是 Create
我得到以下堆栈跟踪: { “状态”:13 “值(value)”: { “class”:“java.lang.NullPointerException”, “堆栈跟踪”: [ { “fileName”:
这是我当前的设置: 从 Bitbucket 获取存储库 使用 Amazon Linux 2 AWS 托管镜像构建 docker 镜像 将图像推送到 ECR 我现在有时会在 docker 构建阶段收到
我开发了一个 PDF Converter / Viewer for Windows Phone 7 , 但除非它可以从某处获取源文件(Word、Excel、PowerPoint 等),否则它毫无用处。
在 ASP.Net MVC 5 项目的根目录中创建 SignalR 2 Hub 类工作得很好。您可以访问 ~/signalr/hubs url 并返回代理。您还可以将该类放在名为 Hubs 或类似名称
我正在从我的工作站测试 ML DHF Quickstart Hub,并且可以很好地连接到 MarkLogic 的本地实例。但是当我创建一个新的环境 gradle-DEV.properties 文件并尝
我想监视来 self 的应用程序的一些事件。 一种选择是将数据发送到 Azure 事件中心并使用流分析进行一些后处理并将数据输入到 cosmos db 中。 另一个选项是从应用程序存储到 cosmos
我想监视来 self 的应用程序的一些事件。 一种选择是将数据发送到 Azure 事件中心并使用流分析进行一些后处理并将数据输入到 cosmos db 中。 另一个选项是从应用程序存储到 cosmos
(我是 Signalr 新手) 我正在开发一个 Web 应用程序,它使用 Signalr-core 来实时更新页面。 我遇到的问题是,当我运行多个客户端时,我正在运行的方法将同时运行与客户端数量一样多
我是 SignalR 库的新手。我在 VS2010 中创建了一个新的 Web 项目,然后在包管理器控制台中我执行了以下命令来设置 SignalR 包。 PM> Install-Package Micr
我将我的Docker镜像标记为foo/foo:latest 当只有一张图片时,可以做docker push 但是由于我总是将它们标记为foo/foo:latest,所以我想知道是否可以上传不是最新生成
我有一个容器,我想对其进行更改。 它托管在docker hub上,我想更改其命令之一以安装其他应用程序。 我可以在docker hub中看到各个步骤,但不能看到dockerfile本身(不确定原因)。
我正在尝试找到一种方法,可以在短时间内将100幅图像推送到Docker集线器。有没有更好,更有效的方法来做到这一点? alpine:1.0.0.0 alpine:2.0.0.0 . .. ... al
我是一名优秀的程序员,十分优秀!