Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
431 stars 184 forks source link

SignalR input binding interaction #2723

Closed agerasko closed 3 weeks ago

agerasko commented 1 month ago

What version of .NET does your existing project use?

.NET 6

What version of .NET are you attempting to target?

.NET 8

Description

Prerequisites:

  1. Azure Function Isolated worker model, .NET 8.0
  2. Function has:
    • HttpTrigger
    • One SignalR input binding to obtain connection info on Negotiation - URL and access token

I have slightly complex logic to define UserId parameter for SignalR input binding. So, I can't use binding expression semantic:

[SignalRConnectionInfoInput(HubName = "{headers.hub}", ConnectionStringSetting = "SignalRConnection", UserId = "{headers.userId")]`

Is it possible to:

  1. Make an impact to the SignalR binding somehow? Handler to configure its options or interceptor to affect its execution at runtime etc... or
  2. Have an access to request before input binding occurrence, to put calculation logic there and operate through binding expression (headers, queries) further? I have tried middleware, but unfortunately, input binding occurs before the middleware processing.

Any other ideas?

Class-based model approach is not suitable for me, as there is need to have one function for many hubs.

Project configuration and dependencies

Link to a repository that reproduces the issue

No response

Y-Sindo commented 1 month ago

Hi @agerasko, I'd suggest you using management SDK to generate the client access token instead of SignalR input binding.

As the latest SignalR worker extensions package already relies on SignalR management SDK, you don't need to refer to it by yourself.

Here is a code snippet to generate client access token:

#pragma warning disable CS0618 // Type or member is obsolete
var serviceManager = new ServiceManagerBuilder()
    .WithOptions(o =>
    {
        o.ConnectionString = "<connection-string>";
    })
    .Build(); // serviceManager instance can be a global one.
var token = serviceManager.GenerateClientAccessToken(hubName, user);
var clientEndpoint = serviceManager.GetClientEndpoint(hubName);
return  new
            {
                url = endpoint,
                accessToken = token
            };

The ServiceManagerBuilder.Build method is obsolete but we don't have plans to remove it, so it's safe to use and more suitable for you if you have many hubs. Here is the management SDK doc for most users who only have a fixed number of hubs: https://learn.microsoft.com/en-us/azure/azure-signalr/signalr-howto-use-management-sdk#negotiation

microsoft-github-policy-service[bot] commented 3 weeks ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

If you are not the original author (agerasko) and believe this issue is not stale, please comment with /bot not-stale and I will not close it.

microsoft-github-policy-service[bot] commented 3 weeks ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.