gpt4 book ai didi

c# - SignalR 中的 "Cannot access a disposed object"崩溃

转载 作者:行者123 更新时间:2023-12-01 21:57:25 24 4
gpt4 key购买 nike

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

客户端连接后会崩溃并出现以下错误:无法访问已处置的对象。

这里是错误:

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

这是中心代码:

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;
}
}
}

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


编辑,这里有更多信息:

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

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

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

public SignalRHubPath(string Route)
{
this.Route = Route;
}
}

然后以这种方式找到并注册它们:

    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 }));
}
}
}

最佳答案

集线器生命周期是每个请求(参见 https://learn.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-3.1 的注解)所以你得到处置异常,因为你正在访问处置对象的属性(客户端)。

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

关于c# - SignalR 中的 "Cannot access a disposed object"崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55795669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com