Skip to content

Commit ccfcbe8

Browse files
Disable BinaryFormatter across most .NET 8 project types (#31591)
1 parent 6843e58 commit ccfcbe8

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CSharp.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Copyright (c) .NET Foundation. All rights reserved.
4444
When https://github.com/Microsoft/visualfsharp/issues/3207 is fixed,
4545
remove the block below and move it into the shared .targets file.
4646
-->
47-
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '7.0'))">
48-
<WarningsAsErrors Condition="'$(EnableUnsafeBinaryFormatterSerialization)' != 'true'">$(WarningsAsErrors);SYSLIB0011</WarningsAsErrors>
47+
<PropertyGroup>
48+
<WarningsAsErrors Condition="'$(_BinaryFormatterObsoleteAsError)' == 'true'">$(WarningsAsErrors);SYSLIB0011</WarningsAsErrors>
4949
</PropertyGroup>
5050
</Project>

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.VisualBasic.targets

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ Copyright (c) .NET Foundation. All rights reserved.
130130
When https://github.com/Microsoft/visualfsharp/issues/3207 is fixed,
131131
remove the block below and move it into the shared .targets file.
132132
-->
133-
<PropertyGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '7.0'))">
134-
<WarningsAsErrors Condition="'$(EnableUnsafeBinaryFormatterSerialization)' != 'true'">$(WarningsAsErrors);SYSLIB0011</WarningsAsErrors>
133+
<PropertyGroup>
134+
<WarningsAsErrors Condition="'$(_BinaryFormatterObsoleteAsError)' == 'true'">$(WarningsAsErrors);SYSLIB0011</WarningsAsErrors>
135135
</PropertyGroup>
136136

137137
<!--

src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,44 @@ Copyright (c) .NET Foundation. All rights reserved.
130130
<IncludeProjectsNotInAssetsFileInDepsFile Condition="'$(IncludeProjectsNotInAssetsFileInDepsFile)' == ''">true</IncludeProjectsNotInAssetsFileInDepsFile>
131131
</PropertyGroup>
132132

133+
<!--
134+
BinaryFormatter Disabling
135+
===========================
136+
137+
The EnableUnsafeBinaryFormatterSerialization setting controls both runtime and compile-time behavior.
138+
The behavior is slightly different depending on the project type and target runtime.
139+
140+
If the property is explicitly set to TRUE:
141+
- The APIs are obsolete as warning, and calls to the APIs will succeed at runtime.
142+
143+
If the property is explicitly set to FALSE:
144+
- On .NET 5 & 6, the APIs are obsolete as warning, and calls to the APIs will fail at runtime.
145+
- On .NET 7+, the APIs are obsolete as error, and calls to the APIs will fail at runtime.
146+
147+
If the property is not explicitly TRUE or FALSE:
148+
- On .NET 5 & 6, the APIs are obsolete as warning, and calls to the APIs will succeed at runtime.
149+
- On .NET 7, the APIs are obsolete as error, but calls to the APIs will succeed at runtime.
150+
- On .NET 8+, the APIs are obsolete as error, and calls to the APIs will fail at runtime
151+
unless the SDK has opted in to keeping legacy BinaryFormatter behavior around.
152+
153+
n.b. The APIs are already marked obsolete (as warning) in .NET 5, so we don't need to special-case
154+
them unless we want to upgrade them to warn-as-error.
155+
-->
156+
157+
<PropertyGroup Condition="'$(EnableUnsafeBinaryFormatterSerialization)' != 'true' AND '$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '7.0'))">
158+
<!--
159+
Certain project types need to re-enable BinaryFormatter by default; it will still be warn-as-error but will work at runtime.
160+
WinForms: enabled through 8.0 (but not after)
161+
WPF: enabled through 8.0 (but not after)
162+
-->
163+
<_ProjectTypeRequiresBinaryFormatter Condition="'$(UseWindowsForms)' == 'true' AND $([MSBuild]::VersionLessThanOrEquals($(TargetFrameworkVersion), '8.0'))">true</_ProjectTypeRequiresBinaryFormatter>
164+
<_ProjectTypeRequiresBinaryFormatter Condition="'$(UseWPF)' == 'true' AND $([MSBuild]::VersionLessThanOrEquals($(TargetFrameworkVersion), '8.0'))">true</_ProjectTypeRequiresBinaryFormatter>
165+
<!-- controls warn as error -->
166+
<_BinaryFormatterObsoleteAsError>true</_BinaryFormatterObsoleteAsError>
167+
<!-- controls runtime behavior (AppContext & trimming) -->
168+
<EnableUnsafeBinaryFormatterSerialization Condition="$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '8.0')) AND '$(_ProjectTypeRequiresBinaryFormatter)' != 'true'">false</EnableUnsafeBinaryFormatterSerialization>
169+
</PropertyGroup>
170+
133171
<PropertyGroup>
134172
<CoreBuildDependsOn>
135173
_CheckForBuildWithNoBuild;

src/WebSdk/ProjectSystem/Targets/Microsoft.NET.Sdk.Web.ProjectSystem.targets

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ Copyright (c) .NET Foundation. All rights reserved.
3737

3838
<PropertyGroup>
3939
<!--
40-
ASP.NET 5.0 explicitly disables this functionality by default, requiring the app author
40+
ASP.NET 5.0+ explicitly disables this functionality by default, requiring the app author
4141
to opt-in to re-enabling the feature. The way to opt back in is to specify the below
42-
element with the value "true". It is anticipated that future releases of .NET will
43-
disable this functionality by default across all project types, at which point this logic
44-
can be removed from the ASP.NET-specific SDK.
42+
element with the value "true". This functionality is disabled across most project types
43+
by default in .NET 8; however, we need to keep the line below since it's not conditioned
44+
on a version check like the normal SDK targets logic is.
4545
-->
4646
<EnableUnsafeBinaryFormatterSerialization Condition="'$(EnableUnsafeBinaryFormatterSerialization)' == ''">false</EnableUnsafeBinaryFormatterSerialization>
4747
</PropertyGroup>

0 commit comments

Comments
 (0)