0

I'm using SignalR client version 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();

Then I reuse the client every time (multi-threaded):

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

This crashes almost immediately (1st addifnotexist works and then the next 8 it crashes). From what I've read everywhere the IHubProxy is supposed to be thread-safe... Any suggestions?

6
  • I dont know this library, but most webclients I know are not threadsafe and only 1 client per user. Commented Jul 12, 2016 at 12:43
  • 1
    "From what I've read everywhere the IHubProxy is supposed to be thread-safe" - any prooflinks? Commented Jul 12, 2016 at 12:45
  • That doesn't make sense imho. I cannot create a new hubproxy after the connection.start is called. That would mean that the websocket is created each time I use the client... Then I could use a webclient. It's supposed to be faster by keeping the connection open. Commented Jul 12, 2016 at 12:46
  • asp.net/signalr/overview/guide-to-the-api/… Search: Thread-safe. Commented Jul 12, 2016 at 12:47
  • What is the crash? If it crashes for another reason, all your description above is useless. Show the exception message in all detail. Commented Jul 12, 2016 at 12:54

1 Answer 1

1

Solution was to create an objectpool and threads would take it from the pool and put it back when done using. The SignalR client is not threadsafe for sure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.