Skip to content

Commit 501d512

Browse files
TodosApi updates (#1836)
* Add launchSettings.json for all BenchmarksApps projects * TodosApi: Bind AppSettings from configuration * TodosApi: Update yml scenarios for AppSettings * Update TodosApi.http * Enable OpenAPI doc generation at build time * Add OpenAPI runtime feature to trim related code in Release builds * Don't require database config/init if run as part of the build * Add schema to launchSettings.json files
1 parent c377cb2 commit 501d512

File tree

36 files changed

+844
-84
lines changed

36 files changed

+844
-84
lines changed

scenarios/goldilocks.benchmarks.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ scenarios:
111111
- "/p:PublishAot=true"
112112
- "/p:StripSymbols=true"
113113
environmentVariables:
114-
CONNECTIONSTRINGS__TODODB: Server={{databaseServer}};Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4
114+
APPSETTINGS__CONNECTIONSTRING: Server={{databaseServer}};Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4
115115
load:
116116
job: wrk
117117
variables:
@@ -131,7 +131,7 @@ scenarios:
131131
- "/p:TrimMode=full"
132132
- "/p:EnableRequestDelegateGenerator=true"
133133
environmentVariables:
134-
CONNECTIONSTRINGS__TODODB: Server={{databaseServer}};Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4
134+
APPSETTINGS__CONNECTIONSTRING: Server={{databaseServer}};Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4
135135
load:
136136
job: wrk
137137
variables:
@@ -147,7 +147,7 @@ scenarios:
147147
- "/p:PublishAot=false"
148148
- "/p:EnableRequestDelegateGenerator=false"
149149
environmentVariables:
150-
CONNECTIONSTRINGS__TODODB: Server={{databaseServer}};Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4
150+
APPSETTINGS__CONNECTIONSTRING: Server={{databaseServer}};Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;Maximum Pool Size=256;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4
151151
load:
152152
job: wrk
153153
variables:

src/BenchmarksApps.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlazorUnited", "BenchmarksA
5555
EndProject
5656
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TodosApi", "BenchmarksApps\TodosApi\TodosApi.csproj", "{8E1A1F61-43E4-4629-A25B-7E5FA82697D0}"
5757
EndProject
58+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCore.OpenApi", "BenchmarksApps\AspNetCore.OpenApi\AspNetCore.OpenApi.csproj", "{28F432A6-1328-4996-91DD-BB1C87F45BF2}"
59+
EndProject
5860
Global
5961
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6062
Debug_Database|Any CPU = Debug_Database|Any CPU
@@ -207,6 +209,14 @@ Global
207209
{8E1A1F61-43E4-4629-A25B-7E5FA82697D0}.Release_Database|Any CPU.Build.0 = Release_Database|Any CPU
208210
{8E1A1F61-43E4-4629-A25B-7E5FA82697D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
209211
{8E1A1F61-43E4-4629-A25B-7E5FA82697D0}.Release|Any CPU.Build.0 = Release|Any CPU
212+
{28F432A6-1328-4996-91DD-BB1C87F45BF2}.Debug_Database|Any CPU.ActiveCfg = Debug_Database|Any CPU
213+
{28F432A6-1328-4996-91DD-BB1C87F45BF2}.Debug_Database|Any CPU.Build.0 = Debug_Database|Any CPU
214+
{28F432A6-1328-4996-91DD-BB1C87F45BF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
215+
{28F432A6-1328-4996-91DD-BB1C87F45BF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
216+
{28F432A6-1328-4996-91DD-BB1C87F45BF2}.Release_Database|Any CPU.ActiveCfg = Release_Database|Any CPU
217+
{28F432A6-1328-4996-91DD-BB1C87F45BF2}.Release_Database|Any CPU.Build.0 = Release_Database|Any CPU
218+
{28F432A6-1328-4996-91DD-BB1C87F45BF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
219+
{28F432A6-1328-4996-91DD-BB1C87F45BF2}.Release|Any CPU.Build.0 = Release|Any CPU
210220
EndGlobalSection
211221
GlobalSection(SolutionProperties) = preSolution
212222
HideSolutionNode = FALSE
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0-preview.4.*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<EmbeddedResource Include="ILLink.Substitutions.xml" LogicalName="ILLink.Substitutions.xml" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<linker>
2+
<assembly fullname="AspNetCore.OpenApi">
3+
<type fullname="Microsoft.AspNetCore.OpenApi.OpenApiFeature">
4+
<method signature="System.Boolean get_IsEnabled()" body="stub" value="false"
5+
feature="Microsoft.AspNetCore.OpenApi.OpenApiFeature.IsEnabled" featurevalue="false"/>
6+
</type>
7+
</assembly>
8+
</linker>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Microsoft.AspNetCore.OpenApi;
2+
3+
public static class OpenApiFeature
4+
{
5+
/// <summary>
6+
/// Indicates whether APIs related to OpenAPI/Swagger functionality are enabled.
7+
/// </summary>
8+
/// <remarks>
9+
/// The value of the property is backed by the "Microsoft.AspNetCore.OpenApi.OpenApiFeature.IsEnabled"
10+
/// <see cref="AppContext"/> setting and defaults to <see langword="true"/> if unset.
11+
/// </remarks>
12+
public static bool IsEnabled { get; } =
13+
AppContext.TryGetSwitch(
14+
switchName: "Microsoft.AspNetCore.OpenApi.OpenApiFeature.IsEnabled",
15+
isEnabled: out var value)
16+
? value : true;
17+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<OpenApiEnabled Condition=" '$(OpenApiEnabled)' == '' and $(Configuration.StartsWith('Release')) and ('$(OpenApiGenerateDocuments)' != 'true') ">false</OpenApiEnabled>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<RuntimeHostConfigurationOption Include="Microsoft.AspNetCore.OpenApi.OpenApiFeature.IsEnabled"
9+
Condition=" '$(OpenApiEnabled)' != '' "
10+
Value="$(OpenApiEnabled)"
11+
Trim="true" />
12+
</ItemGroup>
13+
14+
</Project>

src/BenchmarksApps/BasicMinimalApi/Properties/launchSettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
23
"profiles": {
34
"BasicMinimalApi": {
45
"commandName": "Project",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"DistributedCache": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53684;http://localhost:53687"
11+
}
12+
}
13+
}

src/BenchmarksApps/Grpc/BasicGrpc/Properties/launchSettings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
23
"profiles": {
34
"BasicGrpc": {
45
"commandName": "Project",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"Server": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53695;http://localhost:53696"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"HelloWorldMiddleware": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53681;http://localhost:53682"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"HelloWorldMvc": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53675;http://localhost:53678"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"MapAction": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53676;http://localhost:53679"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"Mvc": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53683;http://localhost:53685"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"BenchmarkServer": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53697;http://localhost:53698"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"StaticFiles": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53686;http://localhost:53688"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"TcpEcho": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53701;http://localhost:53702"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"BlazorUnited": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53703;http://localhost:53704"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"Minimal": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53691;http://localhost:53692"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"Mvc": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53693;http://localhost:53694"
11+
}
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"PlatformBenchmarks": {
5+
"commandName": "Project",
6+
"launchBrowser": true,
7+
"environmentVariables": {
8+
"ASPNETCORE_ENVIRONMENT": "Development"
9+
},
10+
"applicationUrl": "https://localhost:53705;http://localhost:53706"
11+
}
12+
}
13+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Microsoft.Extensions.Options;
2+
3+
namespace TodosApi;
4+
5+
internal class AppSettings
6+
{
7+
public required string ConnectionString { get; set; }
8+
9+
public string? JwtSigningKey { get; set; }
10+
11+
public bool SuppressDbInitialization { get; set; }
12+
}
13+
14+
// Change to using ValidateDataAnnotations once https://github.com/dotnet/runtime/issues/77412 is complete
15+
internal class AppSettingsValidator : IValidateOptions<AppSettings>
16+
{
17+
public ValidateOptionsResult Validate(string? name, AppSettings options)
18+
{
19+
if (string.IsNullOrEmpty(options.ConnectionString))
20+
{
21+
return ValidateOptionsResult.Fail("""
22+
Connection string not found.
23+
If running locally, set the connection string in user secrets for key 'AppSettings:ConnectionString'.
24+
If running after deployment, set the connection string via the environment variable 'APPSETTINGS__CONNECTIONSTRING'.
25+
""");
26+
}
27+
28+
return ValidateOptionsResult.Success;
29+
}
30+
}
31+
32+
internal static class AppSettingsExtensions
33+
{
34+
public static IServiceCollection ConfigureAppSettings(this IServiceCollection services, IConfigurationRoot configurationRoot, IHostEnvironment hostEnvironment)
35+
{
36+
// Can't use the configuration binding source generator due to bug where it emits non-compiling code right now
37+
// https://github.com/dotnet/runtime/issues/83600
38+
var optionsBuilder = services.Configure<AppSettings>(configurationRoot.GetSection(nameof(AppSettings)))
39+
.AddOptions<AppSettings>();
40+
41+
if (!hostEnvironment.IsBuild())
42+
{
43+
services.AddSingleton<IValidateOptions<AppSettings>, AppSettingsValidator>();
44+
optionsBuilder.ValidateOnStart();
45+
}
46+
47+
// Change to using BindConfiguration once https://github.com/dotnet/runtime/issues/83600 is complete
48+
//services.AddSingleton<IValidateOptions<AppSettings>, AppSettingsValidator>()
49+
// .AddOptions<AppSettings>()
50+
// .BindConfiguration(nameof(AppSettings))
51+
// .ValidateOnStart();
52+
53+
// Change to using ValidateDataAnnotations once https://github.com/dotnet/runtime/issues/77412 is complete
54+
//services.AddOptions<AppSettings>()
55+
// .BindConfiguration(nameof(AppSettings))
56+
// .ValidateDataAnnotations()
57+
// .ValidateOnStart();
58+
59+
return services;
60+
}
61+
}

src/BenchmarksApps/TodosApi/DataExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
using System.Data;
22
using System.Runtime.CompilerServices;
3+
using Microsoft.Extensions.Options;
4+
using TodosApi;
35

46
namespace Npgsql;
57

8+
// Replace this with https://www.nuget.org/packages/Nanorm once it's ready
9+
610
internal static class DataExtensions
711
{
812
public static async Task<int> ExecuteAsync(this NpgsqlDataSource dataSource, string commandText, CancellationToken cancellationToken = default)

0 commit comments

Comments
 (0)