Crankier: Connect to Azure SignalR (#13606)

This commit is contained in:
Stafford Williams 2019-11-22 13:28:01 +11:00 committed by Stephen Halter
parent 877e5fafef
commit 88cda2751a
6 changed files with 56 additions and 11 deletions

View File

@ -23,6 +23,7 @@ and are generated based on the last package release.
<LatestPackageReference Include="Microsoft.AspNetCore.Mvc.Razor.Extensions" Version="$(MicrosoftAspNetCoreMvcRazorExtensionsPackageVersion)" />
<LatestPackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="$(MicrosoftAspNetCoreRazorLanguagePackageVersion)" />
<LatestPackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="$(MicrosoftBclAsyncInterfacesPackageVersion)" />
<LatestPackageReference Include="Microsoft.Azure.SignalR" Version="$(MicrosoftAzureSignalRPackageVersion)" />
<LatestPackageReference Include="Microsoft.CodeAnalysis.Common" Version="$(MicrosoftCodeAnalysisCommonPackageVersion)" />
<LatestPackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion)" />
<LatestPackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisCSharpPackageVersion)" />

View File

@ -206,6 +206,7 @@
<MicrosoftAzureKeyVaultPackageVersion>2.3.2</MicrosoftAzureKeyVaultPackageVersion>
<MicrosoftAzureStorageBlobPackageVersion>10.0.1</MicrosoftAzureStorageBlobPackageVersion>
<MicrosoftBuildPackageVersion>15.8.166</MicrosoftBuildPackageVersion>
<MicrosoftAzureSignalRPackageVersion>1.2.0</MicrosoftAzureSignalRPackageVersion>
<MicrosoftBuildFrameworkPackageVersion>15.8.166</MicrosoftBuildFrameworkPackageVersion>
<MicrosoftBuildLocatorPackageVersion>1.2.6</MicrosoftBuildLocatorPackageVersion>
<MicrosoftBuildUtilitiesCorePackageVersion>15.8.166</MicrosoftBuildUtilitiesCorePackageVersion>

View File

@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.SignalR.Crankier.Server;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.SignalR.Crankier.Commands
{
@ -21,6 +22,7 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Commands
app.Command("server", cmd =>
{
var logLevelOption = cmd.Option("--log <LOG_LEVEL>", "The LogLevel to use.", CommandOptionType.SingleValue);
var azureSignalRConnectionString = cmd.Option("--azure-signalr-connectionstring <CONNECTION_STRING>", "Azure SignalR Connection string to use", CommandOptionType.SingleValue);
cmd.OnExecute(() =>
{
@ -30,18 +32,31 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Commands
{
return InvalidArg(logLevelOption);
}
return Execute(logLevel);
if (azureSignalRConnectionString.HasValue() && string.IsNullOrWhiteSpace(azureSignalRConnectionString.Value()))
{
return InvalidArg(azureSignalRConnectionString);
}
return Execute(logLevel, azureSignalRConnectionString.Value());
});
});
}
private static int Execute(LogLevel logLevel)
private static int Execute(LogLevel logLevel, string azureSignalRConnectionString)
{
Console.WriteLine($"Process ID: {Process.GetCurrentProcess().Id}");
var config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.Build();
var configBuilder = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_");
if (azureSignalRConnectionString != null)
{
configBuilder.AddInMemoryCollection(new [] { new KeyValuePair<string, string>("Azure:SignalR:ConnectionString", azureSignalRConnectionString) });
Console.WriteLine("Using Azure SignalR");
}
var config = configBuilder.Build();
var host = new WebHostBuilder()
.UseConfiguration(config)

View File

@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<RootNamespace>Microsoft.AspNetCore.SignalR.CranksRevenge</RootNamespace>
<DisableTransitiveFrameworkReferences>true</DisableTransitiveFrameworkReferences>
</PropertyGroup>
<ItemGroup>
@ -15,6 +16,7 @@
<Reference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" />
<Reference Include="Microsoft.Extensions.Logging.Console" />
<Reference Include="Newtonsoft.Json" />
<Reference Include="Microsoft.Azure.SignalR" />
</ItemGroup>
</Project>

View File

@ -14,7 +14,9 @@ The `server` command runs a web host exposing a single SignalR `Hub` endpoint on
Usage: server [options]
Options:
--log <LOG_LEVEL> The LogLevel to use.
--log <LOG_LEVEL> The LogLevel to use.
--azure-signalr-connectionstring <CONNECTION_STRING> Azure SignalR Connection string to use
```
Notes:
@ -55,6 +57,12 @@ Run the server:
dotnet run -- server
```
Run the server using Azure SignalR:
```
dotnet run -- server --azure-signalr-connectionstring Endpoint=https://your-url.service.signalr.net;AccessKey=yourAccessKey;Version=1.0;
```
Attempt to make 10,000 connections to the server using WebSockets and 10 workers:
```

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
@ -11,15 +12,23 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Server
public class Startup
{
private readonly IConfiguration _config;
private readonly string _azureSignalrConnectionString;
public Startup(IConfiguration configuration)
{
_config = configuration;
_azureSignalrConnectionString = configuration.GetSection("Azure:SignalR").GetValue<string>("ConnectionString", null);
}
public void ConfigureServices(IServiceCollection services)
{
var signalrBuilder = services.AddSignalR()
.AddMessagePackProtocol();
var signalrBuilder = services.AddSignalR();
if (_azureSignalrConnectionString != null)
{
signalrBuilder.AddAzureSignalR();
}
signalrBuilder.AddMessagePackProtocol();
services.AddSingleton<ConnectionCounter>();
@ -30,10 +39,19 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Server
{
app.UseRouting();
app.UseEndpoints(endpoints =>
if (_azureSignalrConnectionString != null)
{
endpoints.MapHub<EchoHub>("/echo");
});
app.UseAzureSignalR(routes => {
routes.MapHub<EchoHub>("/echo");
});
}
else
{
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<EchoHub>("/echo");
});
}
}
}
}