Skip to content

Commit 7814398

Browse files
authored
Recommend setting publish options in project file (#24740)
1 parent 753ed6b commit 7814398

File tree

4 files changed

+114
-48
lines changed

4 files changed

+114
-48
lines changed

docs/core/deploying/single-file.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ Here's a sample project file that specifies single-file publishing:
123123

124124
These properties have the following functions:
125125

126-
* `PublishSingleFile` - Enables single-file publishing.
126+
* `PublishSingleFile` - Enables single-file publishing. Also enables single-file warnings during `dotnet build`.
127127
* `SelfContained` - Determines whether the app will be self-contained or framework-dependent.
128-
* `RuntimeIdentifier` - Specifies the [OS and CPU type](../rid-catalog.md) you are targeting.
128+
* `RuntimeIdentifier` - Specifies the [OS and CPU type](../rid-catalog.md) you are targeting. Also sets `<SelfContained>true</SelfContained>` by default.
129129
* `PublishTrimmed` - Enables use of [assembly trimming](trim-self-contained.md), which is only supported for self-contained apps.
130130
* `PublishReadyToRun` - Enables [ahead-of-time (AOT) compilation](ready-to-run.md).
131131

@@ -136,18 +136,29 @@ These properties have the following functions:
136136

137137
## Publish a single file app - CLI
138138

139-
Publish a single file application using the [dotnet publish](../tools/dotnet-publish.md) command. When you publish your app, set the following properties:
139+
Publish a single file application using the [dotnet publish](../tools/dotnet-publish.md) command.
140140

141-
- Publish for a specific runtime: `-r win-x64`
142-
- Publish as a single-file: `-p:PublishSingleFile=true`
141+
01. Add `<PublishSingleFile>true</PublishSingleFile>` to your project file.
143142

144-
The following example publishes an app for Windows as a self-contained single file application.
143+
This will produce a single-file app on self-contained publish. It also shows single-file compatibility warnings during build.
145144

146-
```dotnetcli
147-
dotnet publish -r win-x64 -p:PublishSingleFile=true --self-contained true
148-
```
145+
```xml
146+
<PropertyGroup>
147+
<PublishSingleFile>true</PublishSingleFile>
148+
</PropertyGroup>
149+
```
150+
151+
01. Publish the app as for a specific runtime identifier using `dotnet publish -r <RID>`
152+
153+
The following example publishes the app for Windows as a self-contained single file application.
149154

150-
The following example publishes an app for Linux as a framework dependent single file application.
155+
`dotnet publish -r win-x64`
156+
157+
The following example publishes the app for Linux as a framework dependent single file application.
158+
159+
`dotnet publish -r linux-x64 --self-contained false`
160+
161+
`<PublishSingleFile>` should be set in the project file to enable single-file analysis during build, but it is also possible to pass these options as `dotnet publish` arguments:
151162

152163
```dotnetcli
153164
dotnet publish -r linux-x64 -p:PublishSingleFile=true --self-contained false
@@ -159,6 +170,8 @@ For more information, see [Publish .NET Core apps with .NET Core CLI](deploy-wit
159170

160171
Visual Studio creates reusable publishing profiles that control how your application is published.
161172

173+
01. Add `<PublishSingleFile>true</PublishSingleFile>` to your project file.
174+
162175
01. On the **Solution Explorer** pane, right-click on the project you want to publish. Select **Publish**.
163176

164177
:::image type="content" source="media/single-file/visual-studio-solution-explorer.png" alt-text="Solution Explorer with a right-click menu highlighting the Publish option.":::

docs/core/deploying/trim-self-contained.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,48 @@ When the code is indirectly referencing an assembly through reflection, you can
3838

3939
## Trim your app - CLI
4040

41-
Trim your application using the [dotnet publish](../tools/dotnet-publish.md) command. When you publish your app, set the following properties:
41+
Trim your application using the [dotnet publish](../tools/dotnet-publish.md) command.
4242

43-
- Publish as a self-contained app for a specific runtime: `-r win-x64`
44-
- Enable trimming: `/p:PublishTrimmed=true`
43+
01. Add `<PublishTrimmed>true</PublishTrimmed>` to your project file.
4544

46-
The following example publishes an app for Windows as self-contained and trims the output.
45+
This will produce a trimmed app on self-contained publish. It also turns off trim-incompatible features and shows trim compatibility warnings during build.
4746

48-
```xml
49-
<PropertyGroup>
50-
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
51-
<PublishTrimmed>true</PublishTrimmed>
52-
</PropertyGroup>
53-
```
47+
```xml
48+
<PropertyGroup>
49+
<PublishTrimmed>true</PublishTrimmed>
50+
</PropertyGroup>
51+
```
52+
53+
01. Publish a self-contained app for a specific runtime identifier using `dotnet publish -r <RID>`
5454

55-
The following example publishes an app in the aggressive trim mode where unused code within assemblies will be trimmed and trimmer warnings enabled.
55+
The following example publishes the app for Windows as trimmed self-contained application.
56+
57+
`dotnet publish -r win-x64`
58+
59+
Note that trimming is only supported for self-contained apps.
60+
61+
The following example configures an app in the aggressive trim mode where unused code within assemblies will be trimmed and trimmer warnings enabled.
5662

5763
```xml
5864
<PropertyGroup>
59-
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
6065
<PublishTrimmed>true</PublishTrimmed>
6166
<TrimMode>link</TrimMode>
6267
<SuppressTrimAnalysisWarnings>false</SuppressTrimAnalysisWarnings>
6368
</PropertyGroup>
6469
```
6570

71+
`<PublishTrimmed>` should be set in the project file so that trim-incompatible features are disabled during `dotnet build`, but it is also possible to pass these options as `dotnet publish` arguments:
72+
73+
`dotnet publish -r win-x64 -p:PublishTrimmed=true`
74+
6675
For more information, see [Publish .NET Core apps with .NET Core CLI](deploy-with-cli.md).
6776

6877
## Trim your app - Visual Studio
6978

7079
Visual Studio creates reusable publishing profiles that control how your application is published.
7180

81+
01. Add `<PublishTrimmed>true</PublishTrimmed>` to your project file.
82+
7283
01. On the **Solution Explorer** pane, right-click on the project you want to publish. Select **Publish...**.
7384

7485
:::image type="content" source="media/trim-self-contained/visual-studio-solution-explorer.png" alt-text="Solution Explorer with a right-click menu highlighting the Publish option.":::

docs/core/deploying/trimming-options.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ Trimming with `PublishTrimmed` was introduced in .NET Core 3.0. The other option
1616

1717
- `<PublishTrimmed>true</PublishTrimmed>`
1818

19-
Enable trimming during publish, with the default settings defined by the SDK.
19+
Enable trimming during publish. This also turns off trim-incompatible features and enables [trim analysis](#roslyn-analyzer) during build.
2020

21-
This will trim any assemblies which have been configured for trimming. With `Microsoft.NET.Sdk` in .NET 6, this includes the any assemblies with `[AssemblyMetadata("IsTrimmable", "True")]`, which is the case for framework assemblies. In .NET 5, framework assemblies from the netcoreapp runtime pack are configured for trimming via `<IsTrimmable>` MSBuild metadata. Other SDKs may define different defaults.
21+
Place this in the project file to ensure that the setting applies during `dotnet build`, not just `dotnet publish`.
22+
23+
This will trim any assemblies that have been configured for trimming. With `Microsoft.NET.Sdk` in .NET 6, this includes the any assemblies with `[AssemblyMetadata("IsTrimmable", "True")]`, which is the case for framework assemblies. In .NET 5, framework assemblies from the netcoreapp runtime pack are configured for trimming via `<IsTrimmable>` MSBuild metadata. Other SDKs may define different defaults.
24+
25+
Starting in .NET 6, this setting also enables the trim compatibility [Roslyn analyzer](#roslyn-analyzer), and disables [features that are incompatible with trimming](#framework-features-disabled-when-trimming).
2226

2327
## Trimming granularity
2428

25-
The following granularity settings control how aggressively unused IL is discarded. This can be set as a property affecting all trimmer input assemblies, or as metadata on an [individual assembly](#trimmed-assemblies) which overrides the property setting.
29+
The following granularity settings control how aggressively unused IL is discarded. This can be set as a property affecting all trimmer input assemblies, or as metadata on an [individual assembly](#trimmed-assemblies), which overrides the property setting.
2630

2731
- `<TrimMode>link</TrimMode>`
2832

@@ -173,6 +177,10 @@ The SDK also makes it possible to disable debugger support using the property `D
173177

174178
Several feature areas of the framework libraries come with linker directives that make it possible to remove the code for disabled features.
175179

180+
- `<AutoreleasePoolSupport>false</AutoreleasePoolSupport>` (default)
181+
182+
Remove code that creates autorelease pools on supported platforms. See [AutoreleasePool for managed threads](../run-time-config/threading.md#autoreleasepool-for-managed-threads). This is the default for the .NET SDK.
183+
176184
- `<DebuggerSupport>false</DebuggerSupport>`
177185

178186
Remove code that enables better debugging experiences. This will also [remove symbols](#remove-symbols).
@@ -197,8 +205,43 @@ Several feature areas of the framework libraries come with linker directives tha
197205

198206
Remove globalization specific code and data. For more information, see [Invariant mode](../run-time-config/globalization.md#invariant-mode).
199207

208+
- `<MetadataUpdaterSupport>false</MetadataUpdaterSupport>`
209+
210+
Remove metadata update specific logic related to hot reload.
211+
212+
- `<UseNativeHttpHandler>true</UseNativeHttpHandler>`
213+
214+
Use the default platform implementation of HttpMessageHandler for Android/iOS and remove the managed implementation.
215+
200216
- `<UseSystemResourceKeys>true</UseSystemResourceKeys>`
201217

202218
Strip exception messages for `System.*` assemblies. When an exception is thrown from a `System.*` assembly, the message will be a simplified resource ID instead of the full message.
203219

204220
These properties will cause the related code to be trimmed and will also disable features via the [runtimeconfig](../run-time-config/index.md) file. For more information about these properties, including the corresponding runtimeconfig options, see [feature switches](https://github.com/dotnet/runtime/blob/main/docs/workflow/trimming/feature-switches.md). Some SDKs may have default values for these properties.
221+
222+
## Framework features disabled when trimming
223+
224+
The following features are incompatible with trimming because they require code that is not statically referenced. These are disabled by default in trimmed apps.
225+
226+
> [!WARNING]
227+
> Enable these features at your own risk. They are likely to break trimmed apps without extra work to preserve the dynamically referenced code.
228+
229+
- `<BuiltInComInteropSupport>`
230+
231+
Built-in COM support is disabled.
232+
233+
- `<CustomResourceTypesSupport>`
234+
235+
Use of custom resource types is not supported. ResourceManager code paths that use reflection for custom resource types is trimmed.
236+
237+
- `<EnableCppCLIHostActivation>`
238+
239+
C++/CLI host activation is disabled.
240+
241+
- `<EnableUnsafeBinaryFormatterInDesigntimeLicenseContextSerialization>`
242+
243+
[`DesigntimeLicenceContextSerializer`](https://docs.microsoft.com/dotnet/api/system.componentmodel.design.designtimelicensecontextserializer?view=net-5.0) use of BinaryFormatter serialization is disabled.
244+
245+
- `<StartupHookSupport>`
246+
247+
Running code before `Main` with `DOTNET_STARTUP_HOOKS` is not supported. For more information, see [host startup hook](https://github.com/dotnet/runtime/blob/main/docs/design/features/host-startup-hook.md).

docs/core/tools/dotnet-publish.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ dotnet publish [<PROJECT>|<SOLUTION>] [-c|--configuration <CONFIGURATION>]
1818
[-f|--framework <FRAMEWORK>] [--force] [--interactive]
1919
[--manifest <PATH_TO_MANIFEST_FILE>] [--no-build] [--no-dependencies]
2020
[--no-restore] [--nologo] [-o|--output <OUTPUT_DIRECTORY>]
21-
[-p:PublishReadyToRun=true] [-p:PublishSingleFile=true] [-p:PublishTrimmed=true]
2221
[-r|--runtime <RUNTIME_IDENTIFIER>] [--self-contained [true|false]]
2322
[--no-self-contained] [-v|--verbosity <LEVEL>]
2423
[--version-suffix <VERSION_SUFFIX>]
@@ -57,6 +56,28 @@ dotnet publish -p:PublishProfile=FolderProfile
5756

5857
The preceding example uses the *FolderProfile.pubxml* file that is found in the *\<project_folder>/Properties/PublishProfiles* folder. If you specify a path and file extension when setting the `PublishProfile` property, they are ignored. MSBuild by default looks in the *Properties/PublishProfiles* folder and assumes the *pubxml* file extension. To specify the path and filename including extension, set the `PublishProfileFullPath` property instead of the `PublishProfile` property.
5958

59+
The following MSBuild properties change the output of `dotnet publish`.
60+
61+
- `PublishReadyToRun`
62+
63+
Compiles application assemblies as ReadyToRun (R2R) format. R2R is a form of ahead-of-time (AOT) compilation. For more information, see [ReadyToRun images](../deploying/ready-to-run.md). Available since .NET Core 3.0 SDK.
64+
65+
To see warnings about missing dependencies that could cause runtime failures, use `PublishReadyToRunShowWarning=true`.
66+
67+
We recommend that you specify `PublishReadyToRun` in a publish profile rather than on the command line.
68+
69+
- `PublishSingleFile`
70+
71+
Packages the app into a platform-specific single-file executable. For more information about single-file publishing, see the [single-file bundler design document](https://github.com/dotnet/designs/blob/main/accepted/2020/single-file/design.md). Available since .NET Core 3.0 SDK.
72+
73+
We recommend that you specify this option in the project file rather than on the command line.
74+
75+
- `PublishTrimmed`
76+
77+
Trims unused libraries to reduce the deployment size of an app when publishing a self-contained executable. For more information, see [Trim self-contained deployments and executables](../deploying/trim-self-contained.md). Available since .NET Core 3.0 SDK as a preview feature.
78+
79+
We recommend that you specify this option in the project file rather than on the command line.
80+
6081
For more information, see the following resources:
6182

6283
- [MSBuild command-line reference](/visualstudio/msbuild/msbuild-command-line-reference)
@@ -139,28 +160,6 @@ For more information, see the following resources:
139160

140161
If you specify a relative path when publishing a solution, each project's output goes into a separate folder relative to the project file location. If you specify an absolute path when publishing a solution, all publish output for all projects goes into the specified folder.
141162

142-
- **`-p:PublishReadyToRun=true`**
143-
144-
Compiles application assemblies as ReadyToRun (R2R) format. R2R is a form of ahead-of-time (AOT) compilation. For more information, see [ReadyToRun images](../deploying/ready-to-run.md). Available since .NET Core 3.0 SDK.
145-
146-
To see warnings about missing dependencies that could cause runtime failures, use `-p:PublishReadyToRunShowWarnings=true`.
147-
148-
We recommend that you specify this option in a publish profile rather than on the command line. For more information, see [MSBuild](#msbuild).
149-
150-
- **`-p:PublishSingleFile=true`**
151-
152-
Packages the app into a platform-specific single-file executable. The executable is self-extracting and contains all dependencies (including native) that are required to run the app. When the app is first run, the application is extracted to a directory based on the app name and build identifier. Startup is faster when the application is run again. The application doesn't need to extract itself a second time unless a new version is used. Available since .NET Core 3.0 SDK.
153-
154-
For more information about single-file publishing, see the [single-file bundler design document](https://github.com/dotnet/designs/blob/main/accepted/2020/single-file/design.md).
155-
156-
We recommend that you specify this option in a publish profile rather than on the command line. For more information, see [MSBuild](#msbuild).
157-
158-
- **`-p:PublishTrimmed=true`**
159-
160-
Trims unused libraries to reduce the deployment size of an app when publishing a self-contained executable. For more information, see [Trim self-contained deployments and executables](../deploying/trim-self-contained.md). Available since .NET Core 3.0 SDK as a preview feature.
161-
162-
We recommend that you specify this option in a publish profile rather than on the command line. For more information, see [MSBuild](#msbuild).
163-
164163
- **`--self-contained [true|false]`**
165164

166165
Publishes the .NET runtime with your application so the runtime doesn't need to be installed on the target machine. Default is `true` if a runtime identifier is specified and the project is an executable project (not a library project). For more information, see [.NET application publishing](../deploying/index.md) and [Publish .NET apps with the .NET CLI](../deploying/deploy-with-cli.md).

0 commit comments

Comments
 (0)