C# SignalR 客户端
C# SignalR Client
我正在使用 SignalR 客户端版本 2.2.0 nuget。
this._connection = new HubConnection(this._url);
this._connection.Closed += this.ConnectionOnClosed;
this._connection.Error += this.ConnectionOnError;
ServicePointManager.DefaultConnectionLimit = 250;
this._client = this._connection.CreateHubProxy(HubName);
this._connection.Start(new WebSocketTransport()).Wait();
那我每次都复用客户端(多线程):
<code>client.Invoke<bool>(nameof(this.AddIfNotExists), key, data, expireAfter)
Parallel.For(
0,
50000,
new ParallelOptions
{
MaxDegreeOfParallelism = 10,
},
async (i) =>
{
var result = await client.ExecuteDistributedAsync(
(i % 2).ToString(),
TimeSpan.FromSeconds(5),
async () =>
{
await Task.Delay(500);
return $"Done: {i} [{(i % 2)}]";
});
Console.WriteLine("--->" + (result ?? "TIMEOUT:" + i));
});
这几乎立即崩溃(第一个 addifnotexist 起作用,然后接下来的 8 个崩溃)。从我到处阅读的内容来看,IHubProxy 应该是线程安全的......
有什么建议吗?
解决方案是创建一个对象池,线程会从池中取出它,并在使用完毕后放回原处。 SignalR 客户端肯定不是线程安全的。
我正在使用 SignalR 客户端版本 2.2.0 nuget。
this._connection = new HubConnection(this._url);
this._connection.Closed += this.ConnectionOnClosed;
this._connection.Error += this.ConnectionOnError;
ServicePointManager.DefaultConnectionLimit = 250;
this._client = this._connection.CreateHubProxy(HubName);
this._connection.Start(new WebSocketTransport()).Wait();
那我每次都复用客户端(多线程):
<code>client.Invoke<bool>(nameof(this.AddIfNotExists), key, data, expireAfter)
Parallel.For(
0,
50000,
new ParallelOptions
{
MaxDegreeOfParallelism = 10,
},
async (i) =>
{
var result = await client.ExecuteDistributedAsync(
(i % 2).ToString(),
TimeSpan.FromSeconds(5),
async () =>
{
await Task.Delay(500);
return $"Done: {i} [{(i % 2)}]";
});
Console.WriteLine("--->" + (result ?? "TIMEOUT:" + i));
});
这几乎立即崩溃(第一个 addifnotexist 起作用,然后接下来的 8 个崩溃)。从我到处阅读的内容来看,IHubProxy 应该是线程安全的...... 有什么建议吗?
解决方案是创建一个对象池,线程会从池中取出它,并在使用完毕后放回原处。 SignalR 客户端肯定不是线程安全的。