【问题标题】:Where to find documentation for SignalR Service output binding for Azure Function on .Net 5.0 C# dotnet-isolated在哪里可以找到 .Net 5.0 C# dotnet-isolated 上 Azure 函数的 SignalR 服务输出绑定的文档
【发布时间】:2021-08-11 09:44:48
【问题描述】:

我正在构建一个基于无服务器架构的应用程序。我卡住的部分包括:SPA Web UI、C# .Net 5.0 隔离进程函数(dotnet-isolated)、Azure SignalR Service(无服务器模式)。

有很多文档和示例如何使用我提到的相同堆栈构建不同的实时无服务器应用程序,但使用 .Net 3.1 函数。而且几乎没有关于 SignalR 触发的 .Net 5.0 隔离进程函数的 SignalR 输出绑定的信息。

唯一有用的文档如下: C#隔离进程:https://docs.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide SignalR 扩展:https://github.com/Azure/azure-functions-dotnet-worker/tree/main/samples/Extensions/SignalR

所以,那里涵盖了基础知识。但是,没有关于一些高级 SignalR 输出绑定的信息,这将有助于构建更多功能,例如:将用户添加到组,从组中删除用户。等等。

到目前为止,我能够构建和运行:协商、发送消息(对所有人、用户或组)、已连接、已断开

但我在 AddToGroup 部分苦苦挣扎......这是我的代码:

        [Function(nameof(AddToGroup))]
        [SignalROutput(HubName = "WebUINotifications", ConnectionStringSetting = "AzureSignalRConnectionString")]
        public static OutputObject AddToGroup(
            [SignalRTrigger("WebUINotifications", "messages", "AddToGroup")] string item,
            FunctionContext context)
        {
            var logger = context.GetLogger("AddToGroup");

            var signalrGroup = JsonSerializer.Deserialize<SignalRGroup>(item);
            var userId = signalrGroup.UserId;
            var groupName = signalrGroup.Arguments[0];

            logger.LogInformation($"UserId: {userId}");
            logger.LogInformation($"Group Name: {groupName}");

            return new OutputObject()
            {
                UserId = userId,
                GroupName = groupName,
            };
        }

在 Azure 中为 SignalR 服务配置 UpStream 设置后,我的函数被 Web UI 中的 javascript 成功触发

this.connection.invoke('AddToGroup', 'TestGroup');

但我得到了错误,这并不奇怪,因为不清楚如何精确绑定:

System.Private.CoreLib: Exception while executing function: Functions.AddToGroup. Microsoft.Azure.WebJobs.Extensions.SignalRService: Unable to convert JObject to valid output binding type, check parameters.

我错过了什么?在哪里可以找到文档?

谢谢大家!

【问题讨论】:

  • 我也面临同样的问题。我到处寻找,文档似乎只是忽略了这一点。所有的代码示例都已经过时了,关于这个的问题都没有得到答案。我认为目前缺少 .net 5 azure 函数中的 SignalR 支持。
  • George 您是如何实现SendMessage(对所有人、用户或组)的?即使在这一点上,我似乎也碰壁了。

标签: azure-functions .net-5 signalr-service


【解决方案1】:

这只是一个猜测,因为我也在努力寻找任何文档。

在以前的版本中,返回了 SignalRMessage:

https://github.com/Azure/azure-functions-signalrservice-extension/blob/e191a3011099049ceb6a3580a54ae2ec8dbbc2b6/src/SignalRServiceExtension/Bindings/SignalRMessage.cs

 /// <summary>
/// Class that contains parameters needed for sending messages.
/// There are three kinds of scope to send, and if more than one
/// scopes are set, it will be resolved by the following order:
///     1. ConnectionId
///     2. UserId
///     3. GroupName
/// </summary>
[JsonObject]
public class SignalRMessage
{
    [JsonProperty("connectionId")]
    public string ConnectionId { get; set; }

    [JsonProperty("userId")]
    public string UserId { get; set; }

    [JsonProperty("groupName")]
    public string GroupName { get; set; }

    [JsonProperty("target"), JsonRequired]
    public string Target { get; set; }

    [JsonProperty("arguments"), JsonRequired]
    public object[] Arguments { get; set; }

    [JsonProperty("endpoints")]
    public ServiceEndpoint[] Endpoints { get; set; }
}

从 cmets 中可以看出,消息的类型是通过检查设置了哪个属性来解析的。

在您的代码中,我将删除“UserId”属性并添加 Json 序列化属性。

抱歉,如果这没有帮助。

【讨论】:

    【解决方案2】:

    我创建了一个sample project 来显示 SignalR 扩展期望的数据类型。并 here is a link 到 github 讨论以获取更多信息和上下文。

    这里是组动作的数据类型的 sn-p。请注意,组名是必需的,即使在使用操作 removeAll 时也是如此,因为 SignalR 代码有 oversight in an update

    public class SignalRGroupAction
    {
        public string connectionId { get; set; }
    
        public string userId { get; set; }
    
        /// <summary>
        /// Required
        /// </summary>
        public string groupName { get; set; }
    
        /// <summary>
        /// Required. Must be one of "add", "remove", "removeAll"
        /// </summary>
        public string action { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-09
      • 2016-03-16
      • 2021-11-05
      • 2020-11-28
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 2014-10-11
      相关资源
      最近更新 更多