SignalR 中“无法访问已处置的对象”崩溃

Tho*_*mas 2 c# signalr

我有一个带有计时器的测试中心,可以将日期发送给所有客户端。

一旦客户端连接,它就会崩溃并出现以下错误:无法访问已释放的对象。

这是错误:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'MyHub'.
   at Microsoft.AspNetCore.SignalR.Hub.CheckDisposed()
   at Microsoft.AspNetCore.SignalR.Hub.get_Clients()
Run Code Online (Sandbox Code Playgroud)

这是中心代码:

public class MyHub : Hub
{
    public MyHub()
    {
        Program.T = new Timer(TickTimer, null, 1000, 1000);
    }

    private void TickTimer(object State)
    {
        try
        {
            var Time = DateTime.UtcNow.ToString(CultureInfo.InvariantCulture);
            Console.WriteLine(Time);

            Clients.All.SendCoreAsync("update", new object[] { Time });
        }
        catch (Exception E)
        {
            Console.WriteLine(E);
            throw;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

看起来Clients对象已被处理,但我不明白为什么。


编辑,这里有更多信息:

集线器可以来自不同的程序集,因此它们在 asp 启动的配置部分动态注册。

每个集线器都用一个属性来装饰以识别它并提供路径:

[AttributeUsage(AttributeTargets.Class)]
public class SignalRHub : Attribute
{
    public readonly string Route;

    public SignalRHubPath(string Route)
    {
        this.Route = Route;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后通过以下方式找到并注册它们:

    private static void RegisterHubs(IApplicationBuilder Application)
    {
        // find all SignalR hubs
        var HubsList = ReflectionHelper.FindType<SignalRHubPath>();
        Logging.Info($"Found {HubsList.Count} hubs");

        // get a link to the mapper method of the hubroutebuilder.
        var MapperMethodInfo = typeof(HubRouteBuilder).GetMethod("MapHub", new[] { typeof(PathString) }, null);

        // register them
        foreach (var H in HubsList)
        {
            // get the route attribute
            var Route = string.Empty;
            var Attributes = Attribute.GetCustomAttributes(H);
            foreach (var Attribute in Attributes)
            {
                if (Attribute is SignalRHubPath A) { Route = A.Route; break; }
            }

            // register the hub
            if (string.IsNullOrEmpty(Route))
            {
                Logging.Warn($"[Hub] {H.Name} does not have a path, skipping");
            }
            else
            {
                Logging.Info($"[Hub] Registering {H.Name} with path {Route}");
                // Application.UseSignalR(_ => _.MapHub<Hub>("/" + Route));
                // use the mapper method call instead so we can pass the hub type
                var Path = new PathString("/" + Route);
                Application.UseSignalR(R => MapperMethodInfo.MakeGenericMethod(H).Invoke(R, new object [] { Path }));
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

ter*_*lut 6

集线器生命周期是按请求计算的(请参阅https://learn.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-3.1上的注释),因此您会收到已处理的异常,因为您正在访问属性(客户端)已处置对象。

当您想要向集线器外部的客户端发送消息时(并且您在外部,因为对计时器做出反应,因此在 .netcore 集线器生命周期之后),您应该使用 IHubContext(您可以通过 DI 获取),看看在https://learn.microsoft.com/en-us/aspnet/core/signalr/hubcontext?view=aspnetcore-3.1