I have a SignalR service that uses IHostedService
to access SignalR on Azure. I’m trying to implement a Negotiate function that returns SignalRConnectionInfo
. Problem is i need to pass the UserId into the ConnectionInfo but i don’t know it yet when filling in parameters. I’m using .NET 8 in Isolated mode and following these Microsoft docs.
Function
[Function("Negotiate")]
public static string Negotiate([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequestData req,
[SignalRConnectionInfoInput(HubName = "SomeHub", UserId = "????")] string connectionInfo)
{
Guid userId = // Get userId from database
await signalRService.AddUserToGroupAll(userId);
return connectionInfo; // How to get this when i don't know UserId in the parameters yet??
}
SignalRService
public class SignalRService(IConfiguration configuration)
{
private ServiceHubContext _messageHubContext;
public async Task AddUserToGroupAll(Guid userId)
{
await _messageHubContext.Groups.AddToGroupAsync(userId.ToString(), /** groupname **/);
}
#region IHostService Functions
async Task IHostedService.StartAsync(CancellationToken cancellationToken)
{
using ServiceManager serviceManager = new ServiceManagerBuilder()
.WithOptions(o => o.ConnectionString = configuration[ConfigurationConstants.AzureSignalRConnectionString])
.BuildServiceManager();
_messageHubContext = await serviceManager.CreateHubContextAsync("SomeHub", cancellationToken);
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _messageHubContext.DisposeAsync();
}
#endregion
}
My question is how do i return SignalRConnectionInfo
in this case?
Thanks!
2
Answers
If you want to negotiate an anonymous session, you can get it from the SignalRService, so you don’t need to pass UserId to your Negotiate function.
Use below code to return
ConnectionInfo
in Isolated Azure functions.UserId
is optional parameter as mentioned in MSDOC and ConnectionInfo can be returned even without passing UserId.Console Response:
Output: