【问题标题】:SignalR Azure function with input binding returning value具有输入绑定返回值的 SignalR Azure 函数
【发布时间】:2021-05-27 16:15:56
【问题描述】:

带有signalR输入绑定的天蓝色函数可以获取一些参数。 例如

 [FunctionName("SignalRTest")]
    public async Task SendMessage([SignalRTrigger]InvocationContext invocationContext, string message, ILogger logger)
    {
        logger.LogInformation($"Receive {message} from {invocationContext.ConnectionId}.");
    }

在函数中它可以调用客户端,例如

[FunctionName("SendMessage")]
public static Task SendMessage(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post")]object message, 
    [SignalR(HubName = "chat")]IAsyncCollector<SignalRMessage> signalRMessages)
{
    return signalRMessages.AddAsync(
        new SignalRMessage 
        {
            // the message will only be sent to this user ID
            UserId = "userId1",
            Target = "newMessage",
            Arguments = new [] { message }
        });
}

但是带有输入绑定的 azure 函数是否有可能返回一些东西

我想要

 [FunctionName("SignalRTest")]
    public async Task<string> SendMessage([SignalRTrigger]InvocationContext invocationContext, string message, ILogger logger)
    {
        return "123";
    }

id = connection.invoke("SendMessage", "test");

但它似乎不起作用。

谢谢。

【问题讨论】:

    标签: azure azure-functions signalr azure-signalr


    【解决方案1】:

    我的团队进行了设置,因此中心操作实际上“返回”两次,也许这就是您要寻找的。​​p>

    当调用集线器操作时,我们让同步代码执行任何它需要执行的操作并返回一个结果对象,该对象通常是我们正在调用的后端服务的模型。这是通过client.SendAsync 推送到客户端的,其中clientIClientProxy。这最终会在你的前端调用一个 JS 处理程序。

    但是,如果出现问题,我们可能无法将回调传递给 JS 处理程序。在这种情况下,我们的 hub 操作也会返回一个前端 JS 可以处理的Task&lt;IActionResult&gt;。这更像是一个 HTTP 状态码响应。

    最后,我们调用的每个 hub 操作至少有 1 个响应,即 IActionResult:

    {"type":3,"invocationId":"0","result":{"statusCode":200}}
    

    这种方法允许我们设置一个可重用的客户端,它的行为更像一个普通的 REST 客户端,因为我们可以使用IActionResult 响应来触发异常处理。

    例子:

    try
        {
          const result = await this.connection.invoke(callSettings.operation, callSettings.args);
    
          if (responseHandler){
            const apiResponse = new ApiResponse({ statusCode: result.statusCode });
            responseHandler.handleStatusCode(apiResponse, callSettings.operation, callSettings.args);
          }
    
          hubResponse.success = result.statusCode < 300;
          hubResponse.httpStatusCode = result.statusCode;
          hubResponse.body = result;
        }
        catch (err)
        {
          hubResponse.success = false;
          this.loggerService.logException(err, callSettings.operation);
          throw(err);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 2021-04-01
      • 2020-07-19
      • 1970-01-01
      相关资源
      最近更新 更多