为什么Azure SignalR不发送Azure Functions发布的消息?

时间:2019-06-14 22:16:05

标签: c# azure azure-functions azure-signalr

我已经部署了一组Azure函数(v2)。

我希望在服务器端发生故障时通知iOS应用程序,因此我创建了一个Azure SignalR服务,配置为Serverless

enter image description here

我已经创建并部署了一个新的negotiate Azure函数,如in the documentation所述,该函数将订阅信息发布在服务总线队列上:

[FunctionName("negotiate")]
public async Task<SignalRConnectionInfo> Negotiate(
    [HttpTrigger(AuthorizationLevel.Anonymous)]
    HttpRequest httpRequest,
    ClaimsPrincipal claimsPrincipal,
    [SignalRConnectionInfo(HubName = "updates", UserId = "{headers.x-ms-client-principal-id}")]
    SignalRConnectionInfo connectionInfo)
{
        try
        {
              //  Uses the name identifier for now.
              Claim nameIdentifierClaim = claimsPrincipal.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier);

             string userId = nameIdentifierClaim.Value;

             foreach (Guid groupId in claimsPrincipal.GetGroups())
             {
                 var subscription = new SynchronizationSubscriptionContract {UserId = userId, GroupId = groupId};
                 await m_serviceBus.SendAsync(JsonConvert.SerializeObject(subscription));
             }

             return connectionInfo;
       }
       catch (Exception exc)
       {
           // 
       }
}

服务总线绑定到Azure功能,该功能将用户添加到组列表中:

[ServiceBusAccount(Constants.Configuration.ServiceBusConnectionString)]
[FunctionName("subscribe")]
public async Task AddToGroupAsync(
        [ServiceBusTrigger("synchronization-subscriptions")] string requestMessage,
        [SignalR(HubName = "updates")] IAsyncCollector<SignalRGroupAction> signalRGroupActions)
{
      var synchronizationSubscriptionContract = JsonConvert.DeserializeObject<SynchronizationSubscriptionContract>(requestMessage);

      string groupName = synchronizationSubscriptionContract.GroupId;

      var addGroupAction = new SignalRGroupAction
      {
          UserId = synchronizationSubscriptionContract.UserId,
          GroupName = groupName,
          Action = GroupAction.Add
      };

      await signalRGroupActions.AddAsync(addGroupAction);
}

到目前为止很好。

每当我现有的Azure功能之一需要在SignalR通道上发布消息时,它就会在绑定到另一个Azure功能的专用服务总线队列上发送消息。然后,Azure功能获取消息并将其发送到Azure SignalR服务:

[ServiceBusAccount(Constants.Configuration.ServiceBusConnectionString)]
   [FunctionName(Api.Functions.Synchronization.UpdateSynchronizationInfo)]
public async Task Run(
       [ServiceBusTrigger("synchronization-requests")] string requestMessage,
       [SignalR(HubName = "updates")]
       IAsyncCollector<SignalRMessage> signalRMessages
)
{
     var requestContract = JsonConvert.DeserializeObject<SynchronizationUpdateRequestContract>(requestMessage);

     var request = m_mapper.Map<SynchronizationUpdateRequest>(requestContract);

    // Do more stuff.

    string groupName = request.GroupId;

    var updateMessage = new SignalRMessage
    {
        GroupName = groupName,
        Target = "notify",
        Arguments = new string[] {}
    };

    await signalRMessages.AddAsync(updateMessage);

}

基于我在Azure函数中不同位置添加的日志,我看不到任何错误,并且一切似乎都已正确调用。但是,我在signalR上没有看到任何消息:即使连接计数增加,消息计数仍保持为0。我的应用程序未收到任何消息。

Connections

Messages

问题

为什么消息没有发送到Azure SignalR?我想念什么?

1 个答案:

答案 0 :(得分:0)

哇。

如果Arguments属性为空列表,则该消息似乎已被删除。

当我将Message实例更改为以下内容时,它立即起作用:

var updateMessage = new SignalRMessage
{
    GroupName = groupName,     
    Target = "notify",
    Arguments = new[] { "Lulz" }
};