Azure / azure-functions-signalrservice-extension

Azure Functions bindings for SignalR Service. Project moved to https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/signalr/Microsoft.Azure.WebJobs.Extensions.SignalRService .
MIT License
97 stars 48 forks source link

How do I add ServerlessHub? #270

Open danylo-dudok opened 3 years ago

danylo-dudok commented 3 years ago

Trying to add multiple ServerlessHub in one azure function .NET 5 project.

Having "No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).".

    public class ChatHub : ServerlessHub
    {
        [FunctionName("negotiate")]
        public SignalRConnectionInfo Negotiate([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req)
        {
            return Negotiate(req.Headers["x-ms-signalr-user-id"], GetClaims(req.Headers["Authorization"]));
        }

        [FunctionName(nameof(OnConnected))]
        public async Task OnConnected([SignalRTrigger]InvocationContext invocationContext, ILogger logger)
        {
            if (invocationContext.Claims.TryGetValue("id", out var groupName))
            {
                await Groups.AddToGroupAsync(invocationContext.ConnectionId, groupName);
                logger.LogInformation($"{invocationContext.ConnectionId} has connected");
            }
            else
            {
                logger.LogInformation($"{invocationContext.ConnectionId} does not have id claim");
            }
        }

        [FunctionName(nameof(OnDisconnected))]
        public async Task OnDisconnected([SignalRTrigger]InvocationContext invocationContext, ILogger logger)
        {
            if (invocationContext.Claims.TryGetValue("id", out var groupName))
            {
                await Groups.RemoveFromGroupAsync(invocationContext.ConnectionId, groupName);
                logger.LogInformation($"{invocationContext.ConnectionId} disconnected");
            }
            else
            {
                logger.LogInformation($"{invocationContext.ConnectionId} does not have id claim");
            }
        }
    }
Y-Sindo commented 3 years ago

.NET 5 function project has an isolated-process model, and you have to use the package Microsoft.Azure.Functions.Worker.Extensions.SignalRService. ServerlessHub is in Microsoft.Azure.WebJobs.Extensions.SignalRService package, so not supported in .NET 5 function project. We are investigating into how to provide a similar developing experience in isolated-process model.