在SignalR中,Hub.Context线程安全吗? [英] In SignalR is Hub.Context thread safe?

查看:229
本文介绍了在SignalR中,Hub.Context线程安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果同时有多个来自不同客户端的请求,则Hub.Context.ConnectionId的值在处理程序执行期间会更改.

If there is more than one request occurring from different clients simultaneously then the value of Hub.Context.ConnectionId changes during the execution of the handler.

说我有2个与客户端ID A和B连接的客户端,并且我的Hub上有一个名为foo()的方法.我从A发送一个请求到服务器,调用foo(),然后在处理来自A的请求的同时,我从B发送一个请求,调用foo().在处理A的请求开始时,Hub.Context.ConnectionId == A,但在方法结束时,调用Hub.Context.ConnectionId == B.

Say I've got 2 clients connected with client Ids A and B, and I've got a method on my Hub called foo(). I send a request from A to the Server invoking foo(), then whilst the request from A is being processed I send a request from B invoking foo(). At the start of the processing of A's request Hub.Context.ConnectionId == A but at the end of the method call Hub.Context.ConnectionId == B.

我应该复制Hub.Context吗?我应该在什么时候这样做?

Should I be copying the Hub.Context? At what point should I do this?

推荐答案

它不需要是线程安全的,因为Hub实例不是静态的,因此您无需复制任何内容.

It doesn't need to be thread safe since Hub instances aren't static so you don't need to copy anything.

它们是根据每次呼叫创建的.因此,来自客户端的每个调用都将创建一个新的Hub实例和HubContext.

They are created per call. So each call from the client will create a new Hub instance and HubContext.

这篇关于在SignalR中,Hub.Context线程安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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