Skip to content

Commit 66b3a75

Browse files
Merge pull request #291192 from vicancy/patch-19
Update signalr-howto-scale-multi-instances.md
2 parents 7877f1b + 71eb4b4 commit 66b3a75

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

articles/azure-signalr/signalr-howto-scale-multi-instances.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,10 @@ The following example overrides the default negotiate behavior and selects the e
9696
private class CustomRouter : EndpointRouterDecorator
9797
{ public override ServiceEndpoint GetNegotiateEndpoint(HttpContext context, IEnumerable<ServiceEndpoint> endpoints)
9898
{
99-
// Override the negotiate behavior to get the endpoint from query string
100-
var endpointName = context.Request.Query["endpoint"];
101-
if (endpointName.Count == 0)
102-
{
103-
context.Response.StatusCode = 400;
104-
var response = Encoding.UTF8.GetBytes("Invalid request");
105-
context.Response.Body.Write(response, 0, response.Length);
106-
return null;
107-
}
108-
109-
return endpoints.FirstOrDefault(s => s.Name == endpointName && s.Online) // Get the endpoint with name matching the incoming request
99+
// Sample code showing how to choose endpoints based on the incoming request endpoint query
100+
var endpointName = context.Request.Query["endpoint"].FirstOrDefault() ?? "";
101+
// Select from the available endpoints, don't construct a new ServiceEndpoint object here
102+
return endpoints.FirstOrDefault(s => s.Name == endpointName && s.Online) // Get the endpoint with name matching the incoming request
110103
?? base.GetNegotiateEndpoint(context, endpoints); // Or fallback to the default behavior to randomly select one from primary endpoints, or fallback to secondary when no primary ones are online
111104
}
112105
}
@@ -129,6 +122,22 @@ services.AddSignalR()
129122
});
130123
```
131124

125+
`ServiceOptions.Endpoints` also supports hot-reload. The below sample code shows how to load connection strings from one configuration section and public URL exposed by [reverse proxies](./signalr-howto-reverse-proxy-overview.md) from another, and as long as configuration supports hot-reload, the endpoints could be updated on the fly.
126+
```cs
127+
services.Configure<ServiceOptions>(o =>
128+
{
129+
o.Endpoints = [
130+
new ServiceEndpoint(Configuration["ConnectionStrings:AzureSignalR:East"], name: "east")
131+
{
132+
ClientEndpoint = new Uri(Configuration.GetValue<string>("PublicClientEndpoints:East"))
133+
},
134+
new ServiceEndpoint(Configuration["ConnectionStrings:AzureSignalR:West"], name: "west")
135+
{
136+
ClientEndpoint = new Uri(Configuration.GetValue<string>("PublicClientEndpoints:West"))
137+
},
138+
];
139+
});
140+
```
132141
## For ASP.NET
133142

134143
### Add multiple endpoints from config
@@ -184,15 +193,9 @@ private class CustomRouter : EndpointRouterDecorator
184193
{
185194
public override ServiceEndpoint GetNegotiateEndpoint(IOwinContext context, IEnumerable<ServiceEndpoint> endpoints)
186195
{
187-
// Override the negotiate behavior to get the endpoint from query string
188-
var endpointName = context.Request.Query["endpoint"];
189-
if (string.IsNullOrEmpty(endpointName))
190-
{
191-
context.Response.StatusCode = 400;
192-
context.Response.Write("Invalid request.");
193-
return null;
194-
}
195-
196+
// Sample code showing how to choose endpoints based on the incoming request endpoint query
197+
var endpointName = context.Request.Query["endpoint"] ?? "";
198+
// Select from the available endpoints, don't construct a new ServiceEndpoint object here
196199
return endpoints.FirstOrDefault(s => s.Name == endpointName && s.Online) // Get the endpoint with name matching the incoming request
197200
?? base.GetNegotiateEndpoint(context, endpoints); // Or fallback to the default behavior to randomly select one from primary endpoints, or fallback to secondary when no primary ones are online
198201
}

0 commit comments

Comments
 (0)