0

Can I use an output binding argument in a foreach, multiple times?

[FunctionName("OnClientConnectedDisconnected")]
public async Task Run(
    [EventGridTrigger] EventGridEvent eventGridEvent,
    [SignalR(HubName = "Lobby")] IAsyncCollector<SignalRMessage> signalRMessage,
    [SignalR(HubName = "Lobby")] IAsyncCollector<SignalRGroupAction> signalRGroupMessage,
    ILogger log)
{
    ...
    ...
    foreach (var player in onlineFriends)
    {
        await signalRGroupMessage.AddAsync(
            new SignalRGroupAction
            {
                GroupName = $"Group_{player}",
                Action = GroupAction.Add,
                UserId = eventGridData.UserId
            }
        );
    }
}
Orhun
  • 1,162
  • 14
  • 22

2 Answers2

0

Whether you can call an output binding multiple times in a function depends on the binding type and how it is written. For example, the table storage output binding will add multiple entities to a table (once for each call), but the queue binding will only post one message. I'm not seeing that the SignalR output binding allows multiple calls.

Remember that binding are optional--an alternate strategy would be to write your own code to create your group and exit the function after the loop.

Matthew
  • 579
  • 3
  • 6
  • How do you know whether these bindings support multiple calls or not? – Orhun Sep 09 '21 at 14:03
  • If the function exits after the output binding is called the first time, then it is designed to only be called once. – Matthew Sep 09 '21 at 14:05
  • I'm looking to [SignalRAsyncCollector.AddAsync() method implementation](https://github.com/Azure/azure-functions-signalrservice-extension/blob/e191a3011099049ceb6a3580a54ae2ec8dbbc2b6/src/SignalRServiceExtension/Bindings/SignalRAsyncCollector.cs#L21) and can't understand how it can avoid following calls? – Orhun Sep 09 '21 at 14:12
0

Yes, you can.

In C#, you can use SignalR output binding multiple times and the invocation just happens at where you call await signalRGroupMessage.AddAsync()

Zackliu
  • 36
  • 4