Azure / Azure/azure-functions-dotnet-worker
Azure Function method with return type ChannelReader<T> doesn't get triggered by microsoft signalr javascript connection.stream(...) method
- Dominant language
- C#
- Stars
- 466
- Forks
- 215
- Avg merge
- 3d 10h
- Merged PRs (30d)
- 7
Description
I'm attempting to stream text responses from a.NET 8 Azure function running in isolated mode to an ASP.NET Core Web App running the Azure signalr javascript client. I'm currently debugging this locally using the Azure signalr emulator.
I'm using the explicit `[SignalRTrigger]` / `[SignalROutput]` bindings on the Azure Function methods, as opposed to using the class-based model.
I initially tried to return an `IAsyncEnumerable` from the function method, however it's not supported by either Newtonsoft.json or System.Text.Json in the context of Azure Functions (see: https://github.com/Azure/azure-functions-dotnet-worker/issues/773).
I therefore switched to using `ChannelReader`, however when I attempt to invoke the function, the method doesn't get called when executing the `stream(...)` client method.
Javascript client code:
```
connection
.stream("streamMessages", message)
.subscribe({
next: (chunk) => {
var r = document.getElementById("response");
r.textContent = r.textContent + chunk;
},
complete: () => {
console.log("Stream completed");
},
error: (err) => {
console.error(err.toString());
},
});
```
Azure Function code:
```
[Function(nameof(StreamMessages))]
public ChannelReader StreamMessages(
[SignalRTrigger(
"AIAgentHub", "messages", "streamMessages", new string[] { "message"}, ConnectionStringSetting = "AzureSignalRConnectionString")]
SignalRInvocationContext invocationContext,
string message, FunctionContext functionContext, CancellationToken cancellationToken)
{
...
}
```
Note that the javascript `invoke(...)` method works so it seems there's nothing inherently wrong with setting up the connection, etc. Client code:
```
connection
.invoke("sendMessage", message)
.catch(function (err) {
return console.error(err.toString());
});
```
Azure Function code:
```
[Function(nameof(SendMessage))]
[SignalROutput(HubName = "AIAgentHub", ConnectionStringSetting = "AzureSignalRConnectionString")]
public async Task SendMessage(
[SignalRTrigger(
"AIAgentHub", "messages", "sendMessage", new string[] { "message"}, ConnectionStringSetting = "AzureSignalRConnectionString")]
SignalRInvocationContext invocationContext,
string message, FunctionContext functionContext, CancellationToken cancellationToken)
{
...
}
```
Is this something that only works with the class-based implementation or inside a Web API instead of an Azure function?
What am I doing wrong?
Many thanks, Richard
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.