C
C#2y ago
sljavad

❔ cannot use InvokeAsync when streaming in signalR

hi i have a streaming channel from client to server using Channel. the stream channel closes when i invoke a method in client from server and wait for response.
4 Replies
sljavad
sljavad2y ago
here is my server side code that reads stream:
public async Task ReplayFromAdminToServer(ChannelReader<int> replay)
{
try
{
while (await replay.WaitToReadAsync())
{
while (replay.TryRead(out var item))
{
Console.WriteLine(item);
}
}
}
catch (Exception ex)
{
Console.WriteLine("EXCEPTION");
Console.WriteLine(ex.Message);
}

Console.WriteLine("END OF ReplayFromAdminToServer");
}
public async Task ReplayFromAdminToServer(ChannelReader<int> replay)
{
try
{
while (await replay.WaitToReadAsync())
{
while (replay.TryRead(out var item))
{
Console.WriteLine(item);
}
}
}
catch (Exception ex)
{
Console.WriteLine("EXCEPTION");
Console.WriteLine(ex.Message);
}

Console.WriteLine("END OF ReplayFromAdminToServer");
}
here is client code that sends stream :
var channel = Channel.CreateUnbounded<int>();
await connection.SendAsync("ReplayFromAdminToServer", channel.Reader);
for (int i = 0; i < 1000; i++)
{
await Task.Delay(100);
await channel.Writer.WriteAsync(i);
richTextBox1.AppendText(i.ToString()+"\r\n");
}
var channel = Channel.CreateUnbounded<int>();
await connection.SendAsync("ReplayFromAdminToServer", channel.Reader);
for (int i = 0; i < 1000; i++)
{
await Task.Delay(100);
await channel.Writer.WriteAsync(i);
richTextBox1.AppendText(i.ToString()+"\r\n");
}
this code will Invoke StreamHandshake on the server and waits for response :
var res = await connection.InvokeAsync<bool>("StreamHandshake", "stream_handshake");
if (res)
{
await connection.SendAsync("ImageMessage", "hello");
}
var res = await connection.InvokeAsync<bool>("StreamHandshake", "stream_handshake");
if (res)
{
await connection.SendAsync("ImageMessage", "hello");
}
StreamHandshake method in the server side :
public async Task<bool> StreamHandshake(string data)
{
if (data == "stream_handshake")
{
try
{
var token = new CancellationTokenSource();
var res = await Clients.Caller.InvokeAsync<bool>("stream_handshake", "someData", token.Token);
return true;
}
catch (Exception ex)
{
Console.WriteLine("EX in StreamHandshake");
Console.WriteLine(ex.Message);
return false;
}

}
return false;
}
public async Task<bool> StreamHandshake(string data)
{
if (data == "stream_handshake")
{
try
{
var token = new CancellationTokenSource();
var res = await Clients.Caller.InvokeAsync<bool>("stream_handshake", "someData", token.Token);
return true;
}
catch (Exception ex)
{
Console.WriteLine("EX in StreamHandshake");
Console.WriteLine(ex.Message);
return false;
}

}
return false;
}
my stream channel closes when code reaches to line await Clients.Caller.InvokeAsync<bool>("stream_handshake", "someData", token.Token); it invokes a method in the client and waits for response , but the porgram will not continue from this line when client returns the response. here is stream_handshake listener in client side:
connection.On<string, bool>("stream_handshake", async (input) =>
{
return true;
});
connection.On<string, bool>("stream_handshake", async (input) =>
{
return true;
});
it just return true , but server side will not get it and stream closes. is this a problem in signalR ? should i open issue in github ?
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
sljavad
sljavad2y ago
FYI, i opened an issue for this you can follow it on github https://github.com/dotnet/aspnetcore/issues/46503
GitHub
[SignalR] Cannot use InvokeAsync While there is an stream ongoing i...
Is there an existing issue for this? I have searched the existing issues Describe the bug i have an stream from client to server using SignalR. stream channel closes and completes when i invoke a c...
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.