Skip to content

Commit

Permalink
Set InvariantGlobalization in api, webapi, grpc, and worker templates (
Browse files Browse the repository at this point in the history
…dotnet#47066)

* Set InvariantGlobalization in api, webapi, grpc, and worker templates

API server applications do not typically need culture aware behavior. A better default is to use invariant globalization mode for API apps.

This allows for the globalization code to be trimmed away, making the final executable size smaller. It also reduces startup and memory consumption because ICU isn't loaded.

This also allows the apps to run with consistent behavior on container images that don't include ICU, making the container image smaller as well.

Fix dotnet#47029

* Add template tests to ensure InvariantGlobalization is set.
  • Loading branch information
eerhardt authored Mar 10, 2023
1 parent 42905b0 commit 53e77ee
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/ProjectTemplates/Shared/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ public async Task VerifyLaunchSettings(string[] expectedLaunchProfileNames)
}
}

public async Task VerifyHasProperty(string propertyName, string expectedValue)
{
var projectFile = Directory.EnumerateFiles(TemplateOutputDir, "*proj").FirstOrDefault();

Assert.NotNull(projectFile);

var projectFileContents = await File.ReadAllTextAsync(projectFile);
Assert.Contains($"<{propertyName}>{expectedValue}</{propertyName}>", projectFileContents);
}

public string ReadFile(string path)
{
AssertFileExists(path, shouldExist: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<NoDefaultLaunchSettingsFile Condition="'$(ExcludeLaunchSettings)' == 'True'">true</NoDefaultLaunchSettingsFile>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.WebApplication1</RootNamespace>
<ServerGarbageCollection>false</ServerGarbageCollection>
<InvariantGlobalization>true</InvariantGlobalization>
<!--#if (NativeAot) -->
<PublishAot>true</PublishAot>
<!--#endif -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>${DefaultNetCoreTargetFramework}</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<!--#if (NativeAot) -->
<PublishAot>true</PublishAot>
<!--#endif -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>${DefaultNetCoreTargetFramework}</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<UserSecretsId Condition="'$(IndividualAuth)' == 'True' OR '$(OrganizationalAuth)' == 'True'">aspnet-Company.WebApplication1-53bc9b9d-9d6a-45d4-8429-2a2761773502</UserSecretsId>
<NoDefaultLaunchSettingsFile Condition="'$(ExcludeLaunchSettings)' == 'True'">True</NoDefaultLaunchSettingsFile>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.WebApplication1</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>${DefaultNetCoreTargetFramework}</TargetFramework>
<InvariantGlobalization>true</InvariantGlobalization>
<NoDefaultLaunchSettingsFile Condition="'$(ExcludeLaunchSettings)' == 'True'">True</NoDefaultLaunchSettingsFile>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.WebApplication1</RootNamespace>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>${DefaultNetCoreTargetFramework}</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
<UserSecretsId>dotnet-Company.Application1-53bc9b9d-9d6a-45d4-8429-2a2761773502</UserSecretsId>
<NoDefaultLaunchSettingsFile Condition="'$(ExcludeLaunchSettings)' == 'True'">True</NoDefaultLaunchSettingsFile>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.Application1</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFramework>${DefaultNetCoreTargetFramework}</TargetFramework>
<UserSecretsId>dotnet-Company.Application1-53bc9b9d-9d6a-45d4-8429-2a2761773502</UserSecretsId>
<InvariantGlobalization>true</InvariantGlobalization>
<NoDefaultLaunchSettingsFile Condition="'$(ExcludeLaunchSettings)' == 'True'">True</NoDefaultLaunchSettingsFile>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.Application1</RootNamespace>
</PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/ProjectTemplates/test/Templates.Tests/ApiTemplateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ private async Task ApiTemplateCore(string languageOverride, string[] args = null
: new[] { "http", "IIS Express" };
await project.VerifyLaunchSettings(expectedLaunchProfileNames);

await project.VerifyHasProperty("InvariantGlobalization", "true");

// Avoid the F# compiler. See https://github.com/dotnet/aspnetcore/issues/14022
if (languageOverride != null)
{
Expand Down
2 changes: 2 additions & 0 deletions src/ProjectTemplates/test/Templates.Tests/GrpcTemplateTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ private async Task GrpcTemplateCore(string[] args = null)
var expectedLaunchProfileNames = new[] { "http", "https" };
await project.VerifyLaunchSettings(expectedLaunchProfileNames);

await project.VerifyHasProperty("InvariantGlobalization", "true");

await project.RunDotNetPublishAsync();

await project.RunDotNetBuildAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public async Task WorkerTemplateAsync(string language, string[] args)

await project.RunDotNetNewAsync("worker", language: language, args: args);

await project.VerifyHasProperty("InvariantGlobalization", "true");

await project.RunDotNetPublishAsync();

// Run dotnet build after publish. The reason is that one uses Config = Debug and the other uses Config = Release
Expand Down

0 comments on commit 53e77ee

Please sign in to comment.