问题描述
我正在构建基于无服务器架构的应用程序。我被卡住的部分包括:SPA Web UI、C# .Net 5.0 隔离进程函数(dotnet 隔离)、Azure SignalR 服务(无服务器模式)。
有很多文档和示例如何使用我提到的相同堆栈构建不同的实时无服务器应用程序,但使用 .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.
我错过了什么?在哪里可以找到文档?
谢谢各位!
解决方法
这只是一个猜测,因为我也在努力寻找任何文档。
在以前的版本中,返回了 SignalRMessage:
/// <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; }
}
从评论中可以看出,通过检查设置了哪个属性来解析消息类型。
在您的代码中,我将删除“UserId”属性并添加 Json 序列化属性。
如果没有帮助,我们深表歉意。