Asp.net Core中使用SignalR出现ObjectDisposedException的解决

SignalR出现ObjectDisposedException的解决

问题描述

功能要求

业务上要实现服务器端定时向客户端推送某些最新消息,方案的思路是:当客户端连接上来时记录下它的ConnectionId,使用Timer创建定时任务,定时向该ConnectionId的客户端发送消息。

异常位置

在定时任务中使用Clients.Client(connId) 或其他方法获取客户端连接时冒出ObjectDisposedException。
异常图片
异常位置代码:

public async void LoopTask(object state)
{
	// state是ConnectionId
    string connId = state.ToString();

    // 任何方式获取客户端连接都会冒ObjectDisposedException
    var client = Clients.Client(connId);

    string arg = "this is an argument";
    await client.SendAsync("Test", arg);
}

有问题的代码总览:

    public class TestHub : Hub
    {
        // 定时任务字典。Dictionary不是线程安全的,实际应用中应该加锁或者使用ConcurrentDictionary
        private static Dictionary<string, Timer> Tasks { get; set; }

        static TestHub()
        {
            Tasks = new Dictionary<string, Timer>();
        }

        public override Task OnDisconnectedAsync(Exception exception)
        {
            string connId = Context.ConnectionId;
            if(Tasks.TryGetValue(connId,out Timer timer))
            {
                timer.Dispose();
            }
            return base.OnDisconnectedAsync(exception);
        }

        public override Task OnConnectedAsync()
        {
            string connId = Context.ConnectionId;
            // 0毫秒后开始每5秒执行一次
            Timer timer = new Timer(LoopTask, connId, 0, 5000);
            Tasks.Add(connId, timer);
            return base.OnConnectedAsync();
        }

        public async void LoopTask(object state)
        {
            string connId = state.ToString();
            // 任何方式获取客户端连接都会冒ObjectDisposedException
            var client = Clients.Client(connId);
            string arg = "this is an argument";
            await client.SendAsync("Test", arg);
        }
    }

解决办法

因为Hub是临时性的,请求完成后会dispose掉,所以所有不在请求内的操作都不能直接使用Hub里面的对象(Clients, Groups等)获取客户端连接。
来自微软文档的介绍
这里要单独开一个客户端连接的类,利用Asp.net core自带依赖注入将IHubContext注入到类中,在类中用IHubContext进行操作。
这里要注意的是,Asp.net 和Asp.net core是不一样的,Asp.net中可以用GlobalHost中获取到IHubContext,而Asp.net core中就不能这样获取。
来自微软文档的介绍
修改后代码总览:

    public class ClientConnection : IDisposable
    {
    	// 连接标识,也可以是userId等,反正能定位到客户端就行
        public string ConnectionId { get; set; }
		
		// HubContext,用于获取客户端连接
        private IHubContext<TestHub> Context { get; set; }

		// 定时任务
        private Timer Timer { get; set; }

        public ClientConnection(IHubContext<TestHub> hubContext, string connId)
        {
            this.ConnectionId = connId;
            this.Context = hubContext;

            // 0毫秒后开始每5秒执行一次
            Timer = new Timer(LoopTask, null, 0, 5000);
        }

        public async void LoopTask(object state)
        {
            var client = Context.Clients.Client(this.ConnectionId);
            string arg = "this is an argument";

            await client.SendAsync("Test", arg);
        }

        public void Dispose()
        {
            this.Timer.Dispose();
        }
    }

    public class TestHub : Hub
    {
        // 用于获取依赖注入对象
        private IServiceProvider Service { get; set; }

        // 这里其实不应该把连接保存在Hub中,demo的话随便了(记得加锁)
        private static List<ClientConnection> Connections;

        static TestHub()
        {
            Connections = new List<ClientConnection>();
        }

        public TestHub(IServiceProvider service)
        {
            Service = service;
        }

        public override Task OnDisconnectedAsync(Exception exception)
        {
            string connId = Context.ConnectionId;

            var conn = Connections.Find(s => s.ConnectionId == connId);
            if(conn != null)
            {
                Connections.Remove(conn);
                conn.Dispose();
            }

            return base.OnDisconnectedAsync(exception);
        }

        public override Task OnConnectedAsync()
        {
            string connId = Context.ConnectionId;
			// 这里不用在StartUp里面AddScoped也可以获取到
            IHubContext<TestHub> context = Service.GetService(typeof(IHubContext<TestHub>)) as IHubContext<TestHub>;
            ClientConnection conn = new ClientConnection(context, connId);
            Connections.Add(conn);

            return base.OnConnectedAsync();
        }
    }

问题原因

问题的原因就在于Hub实例的暂时性,跟Controller一样,一个请求生成一个实例,似乎也合情合理。但是,虽然Hub是临时性的但是它管理的连接是持久的(WebSocket长连接),这就很有迷惑性,容易让人以为Hub也是持久存在的。
在这里插入图片描述
最后,建议入坑的过程中还是要多看文档,其实微软的文档确实做得不错,虽然很多都是机器翻译,而且词汇都比较晦涩,但重点都会有特别说明,这点可以给个赞。

参考
Asp.net Core中SignalR获取IHubContext的文档

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET Core C#,可以使用System.Threading.Tasks.Task类来创建和管理线程池。以下是一个简单的线程池实现: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace ThreadPoolDemo { public class MyThreadPool { private readonly Queue<Action> _tasks = new Queue<Action>(); private readonly object _lock = new object(); private readonly int _maxThreads; private int _runningThreads = 0; private bool _disposed = false; public MyThreadPool(int maxThreads) { _maxThreads = maxThreads; } public void QueueTask(Action task) { if (_disposed) { throw new ObjectDisposedException(nameof(MyThreadPool)); } lock (_lock) { _tasks.Enqueue(task); if (_runningThreads < _maxThreads) { Interlocked.Increment(ref _runningThreads); Task.Factory.StartNew(ExecuteTask, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default); } } } private void ExecuteTask() { while (true) { Action task = null; lock (_lock) { if (_tasks.Count > 0) { task = _tasks.Dequeue(); } else { Interlocked.Decrement(ref _runningThreads); break; } } task(); } } public void Dispose() { if (_disposed) { return; } _disposed = true; lock (_lock) { while (_tasks.Count > 0) { var task = _tasks.Dequeue(); } _runningThreads = 0; } } } } ``` 使用示例: ```csharp using System; namespace ThreadPoolDemo { class Program { static void Main(string[] args) { using (var pool = new MyThreadPool(4)) { for (int i = 0; i < 10; i++) { var index = i; pool.QueueTask(() => Console.WriteLine($"Task {index} is running on thread {Thread.CurrentThread.ManagedThreadId}")); } } } } } ``` 这个线程池实现使用一个队列来存储待执行的任务。当有新的任务加入队列时,它会检查当前运行的线程数量是否已经达到最大值,如果没有,则创建新的线程来执行任务。当所有任务执行完毕时,线程池会自动关闭所有线程。 请注意,这个线程池实现只是一个简单的示例,没有考虑到一些高级功能,例如线程异常处理、任务取消等。在实际应用,需要根据实际需求进行完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值