Skip to content
代码片段 群组 项目
提交 88cda275 编辑于 作者: Stafford Williams's avatar Stafford Williams 提交者: Stephen Halter
浏览文件

Crankier: Connect to Azure SignalR (#13606)

上级 877e5faf
No related branches found
No related tags found
无相关合并请求
...@@ -23,6 +23,7 @@ and are generated based on the last package release. ...@@ -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.Mvc.Razor.Extensions" Version="$(MicrosoftAspNetCoreMvcRazorExtensionsPackageVersion)" />
<LatestPackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="$(MicrosoftAspNetCoreRazorLanguagePackageVersion)" /> <LatestPackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="$(MicrosoftAspNetCoreRazorLanguagePackageVersion)" />
<LatestPackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="$(MicrosoftBclAsyncInterfacesPackageVersion)" /> <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.Common" Version="$(MicrosoftCodeAnalysisCommonPackageVersion)" />
<LatestPackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion)" /> <LatestPackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion)" />
<LatestPackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisCSharpPackageVersion)" /> <LatestPackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisCSharpPackageVersion)" />
......
...@@ -206,6 +206,7 @@ ...@@ -206,6 +206,7 @@
<MicrosoftAzureKeyVaultPackageVersion>2.3.2</MicrosoftAzureKeyVaultPackageVersion> <MicrosoftAzureKeyVaultPackageVersion>2.3.2</MicrosoftAzureKeyVaultPackageVersion>
<MicrosoftAzureStorageBlobPackageVersion>10.0.1</MicrosoftAzureStorageBlobPackageVersion> <MicrosoftAzureStorageBlobPackageVersion>10.0.1</MicrosoftAzureStorageBlobPackageVersion>
<MicrosoftBuildPackageVersion>15.8.166</MicrosoftBuildPackageVersion> <MicrosoftBuildPackageVersion>15.8.166</MicrosoftBuildPackageVersion>
<MicrosoftAzureSignalRPackageVersion>1.2.0</MicrosoftAzureSignalRPackageVersion>
<MicrosoftBuildFrameworkPackageVersion>15.8.166</MicrosoftBuildFrameworkPackageVersion> <MicrosoftBuildFrameworkPackageVersion>15.8.166</MicrosoftBuildFrameworkPackageVersion>
<MicrosoftBuildLocatorPackageVersion>1.2.6</MicrosoftBuildLocatorPackageVersion> <MicrosoftBuildLocatorPackageVersion>1.2.6</MicrosoftBuildLocatorPackageVersion>
<MicrosoftBuildUtilitiesCorePackageVersion>15.8.166</MicrosoftBuildUtilitiesCorePackageVersion> <MicrosoftBuildUtilitiesCorePackageVersion>15.8.166</MicrosoftBuildUtilitiesCorePackageVersion>
......
...@@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Hosting; ...@@ -11,6 +11,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.SignalR.Crankier.Server; using Microsoft.AspNetCore.SignalR.Crankier.Server;
using System.Collections.Generic;
namespace Microsoft.AspNetCore.SignalR.Crankier.Commands namespace Microsoft.AspNetCore.SignalR.Crankier.Commands
{ {
...@@ -21,6 +22,7 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Commands ...@@ -21,6 +22,7 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Commands
app.Command("server", cmd => app.Command("server", cmd =>
{ {
var logLevelOption = cmd.Option("--log <LOG_LEVEL>", "The LogLevel to use.", CommandOptionType.SingleValue); 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(() => cmd.OnExecute(() =>
{ {
...@@ -30,18 +32,31 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Commands ...@@ -30,18 +32,31 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Commands
{ {
return InvalidArg(logLevelOption); 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}"); Console.WriteLine($"Process ID: {Process.GetCurrentProcess().Id}");
var config = new ConfigurationBuilder() var configBuilder = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_") .AddEnvironmentVariables(prefix: "ASPNETCORE_");
.Build();
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() var host = new WebHostBuilder()
.UseConfiguration(config) .UseConfiguration(config)
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework> <TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<RootNamespace>Microsoft.AspNetCore.SignalR.CranksRevenge</RootNamespace> <RootNamespace>Microsoft.AspNetCore.SignalR.CranksRevenge</RootNamespace>
<DisableTransitiveFrameworkReferences>true</DisableTransitiveFrameworkReferences>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
...@@ -15,6 +16,7 @@ ...@@ -15,6 +16,7 @@
<Reference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" /> <Reference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" />
<Reference Include="Microsoft.Extensions.Logging.Console" /> <Reference Include="Microsoft.Extensions.Logging.Console" />
<Reference Include="Newtonsoft.Json" /> <Reference Include="Newtonsoft.Json" />
<Reference Include="Microsoft.Azure.SignalR" />
</ItemGroup> </ItemGroup>
</Project> </Project>
...@@ -14,7 +14,9 @@ The `server` command runs a web host exposing a single SignalR `Hub` endpoint on ...@@ -14,7 +14,9 @@ The `server` command runs a web host exposing a single SignalR `Hub` endpoint on
Usage: server [options] Usage: server [options]
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: Notes:
...@@ -55,6 +57,12 @@ Run the server: ...@@ -55,6 +57,12 @@ Run the server:
dotnet run -- 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: Attempt to make 10,000 connections to the server using WebSockets and 10 workers:
``` ```
......
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
...@@ -11,15 +12,23 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Server ...@@ -11,15 +12,23 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Server
public class Startup public class Startup
{ {
private readonly IConfiguration _config; private readonly IConfiguration _config;
private readonly string _azureSignalrConnectionString;
public Startup(IConfiguration configuration) public Startup(IConfiguration configuration)
{ {
_config = configuration; _config = configuration;
_azureSignalrConnectionString = configuration.GetSection("Azure:SignalR").GetValue<string>("ConnectionString", null);
} }
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
var signalrBuilder = services.AddSignalR() var signalrBuilder = services.AddSignalR();
.AddMessagePackProtocol();
if (_azureSignalrConnectionString != null)
{
signalrBuilder.AddAzureSignalR();
}
signalrBuilder.AddMessagePackProtocol();
services.AddSingleton<ConnectionCounter>(); services.AddSingleton<ConnectionCounter>();
...@@ -30,10 +39,19 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Server ...@@ -30,10 +39,19 @@ namespace Microsoft.AspNetCore.SignalR.Crankier.Server
{ {
app.UseRouting(); app.UseRouting();
app.UseEndpoints(endpoints => if (_azureSignalrConnectionString != null)
{
app.UseAzureSignalR(routes => {
routes.MapHub<EchoHub>("/echo");
});
}
else
{ {
endpoints.MapHub<EchoHub>("/echo"); app.UseEndpoints(endpoints =>
}); {
endpoints.MapHub<EchoHub>("/echo");
});
}
} }
} }
} }
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册