You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function隔离模式下SignalR流式调用ChannelReader<T>失败排查

问题:Azure Functions隔离模式下SignalR流式传输无法触发函数方法

我正尝试将运行在隔离模式下的.NET 8 Azure Function的文本响应,流式传输到使用Azure SignalR JavaScript客户端的ASP.NET Core Web应用,目前通过Azure SignalR模拟器进行本地调试。

我在Azure Function方法上使用显式的[SignalRTrigger]/[SignalROutput]绑定,而非类基模型。最初尝试从函数方法返回IAsyncEnumerable<T>,但Azure Functions环境中Newtonsoft.json和System.Text.Json均不支持该类型,因此改用ChannelReader<T>。然而执行客户端的stream("streamMessages", message)方法时,对应的Function方法未被调用。

注:客户端使用invoke("sendMessage", message)方法可正常触发对应的Function方法,说明连接等配置本身无问题。


客户端流式调用代码

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 Functions流式处理方法代码

[Function(nameof(StreamMessages))]
public ChannelReader<string> StreamMessages(
    [SignalRTrigger(
        "AIAgentHub", "messages", "streamMessages", new string[] { "message"}, ConnectionStringSetting = "AzureSignalRConnectionString")]
            SignalRInvocationContext invocationContext,
    string message, FunctionContext functionContext, CancellationToken cancellationToken)
{
    ...
}

正常工作的客户端Invoke代码

connection
    .invoke("sendMessage", message)
    .catch(function (err) {
        return console.error(err.toString());
});

正常工作的Azure Functions Invoke方法代码

[Function(nameof(SendMessage))]
[SignalROutput(HubName = "AIAgentHub", ConnectionStringSetting = "AzureSignalRConnectionString")]
public async Task<SignalRMessageAction> SendMessage(
    [SignalRTrigger(
        "AIAgentHub", "messages", "sendMessage", new string[] { "message"}, ConnectionStringSetting = "AzureSignalRConnectionString")]
            SignalRInvocationContext invocationContext,
    string message, FunctionContext functionContext, CancellationToken cancellationToken)
{
    ...
}

疑问

该流式功能是否仅支持类基实现或Web API而非Azure Function?我哪里操作有误?


内容的提问来源于stack exchange,提问作者Richard Urwin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 10:42:51