-
Notifications
You must be signed in to change notification settings - Fork 25k
Configure SignalR hub URL endpoint #36769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,6 +124,44 @@ The WebSocket transport has additional options that can be configured using the | |
|
|
||
| Client options can be configured on the `HubConnectionBuilder` type (available in the .NET and JavaScript clients). It's also available in the Java client, but the `HttpHubConnectionBuilder` subclass is what contains the builder configuration options, as well as on the `HubConnection` itself. | ||
|
|
||
| ### Configure the hub URL endpoint | ||
|
|
||
| To set a static hub URL endpoint, pass the URL to <xref:Microsoft.AspNetCore.SignalR.Client.HubConnectionBuilderHttpExtensions.WithUrl%2A>. The following example sets the URL to `https://contoso.com/chathub`: | ||
|
|
||
| ```csharp | ||
| var connection = new HubConnectionBuilder() | ||
| .WithUrl("https://contoso.com/chathub") | ||
| .Build(); | ||
| ``` | ||
|
|
||
| To configure the endpoint in code, use <xref:System.UriBuilder> to build the URL and pass it to <xref:Microsoft.AspNetCore.SignalR.Client.HubConnectionBuilderHttpExtensions.WithUrl%2A>. The following example configures a custom hub URL for production on a target IIS server: | ||
|
|
||
| ```csharp | ||
| #if DEBUG | ||
| // IIS Express development environment (development build) | ||
| hubConnection = new HubConnectionBuilder() | ||
| .WithUrl(Navigation.ToAbsoluteUri("/chathub")) | ||
| .WithAutomaticReconnect() | ||
| .Build(); | ||
| #else | ||
| // IIS server production environment (release build) | ||
| var hostName = Environment.MachineName; | ||
| var addresses = Dns.GetHostAddresses(hostName); | ||
| var ipv4 = addresses.FirstOrDefault(addresses => | ||
| addresses.AddressFamily == AddressFamily.InterNetwork)?.ToString(); | ||
| var uri = | ||
| new UriBuilder(Navigation.ToAbsoluteUri("/chathub")) | ||
| { | ||
| Scheme = "http", Port = 80, Host = ipv4 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might need a note/context explaining why a hardcoded port and "http" are set here: SignalR hub connection loops back directly to the local IIS server to bypass a load balancer/proxy and avoid the credential problem. I could see folks dropping this code in, as-is with
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps, so. I'm 👂 for how deep into the scenario for the example Brennan wants to get.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we end up making the example more generic, we can do it this way with placeholders ... Scheme = "{SCHEME}", Port = {PORT}, Host = ipv4 |
||
| }.ToString(); | ||
|
|
||
| hubConnection = new HubConnectionBuilder() | ||
| .WithUrl(Navigation.ToAbsoluteUri(uri)) | ||
| .WithAutomaticReconnect() | ||
| .Build(); | ||
| #endif | ||
| ``` | ||
|
|
||
| ### Configure logging | ||
|
|
||
| Logging is configured in the .NET Client using the `ConfigureLogging` method. Logging providers and filters can be registered in the same way as they are on the server. See the [Logging in ASP.NET Core](xref:fundamentals/logging/index) documentation for more information. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At this point it is already an absolute URI string I think. Do we need to use Navigation.ToAbsoluteUri() again? Maybe should be just
.WithUrl(uri)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took this directly from the PU issue, and it wasn't remarked on there.
It's a string due to the
ToString, so I'm thinking this would be better as ...That way, there's the
UriBuildertype, which can then supply auriBuilder.UritoWithUrl.Let's see if Brennan agrees with that change.