いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

BinaryFormatterが非推奨として警告されるようになっていた (.NET 5)

概要

以下、自分用メモです。

久しぶりに、自分のC#サンプル リポジトリをコンパイルしてみたら

github.com

以下のような警告がブワーって出てびっくり。

gitpod /workspace/try-csharp $ make run
dotnet clean --nologo -v q

Welcome to .NET 5.0!
---------------------
SDK Version: 5.0.202

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------


dotnet restore -v q
dotnet run --project TryCSharp.Tools.Cui/TryCSharp.Tools.Cui.csproj --onetime
/workspace/try-csharp/TryCSharp.Samples/Advanced/RuntimeHelpersSamples02.cs(73,14): warning SYSLIB0004: 'ReliabilityContractAttribute' is obsolete: 'The Constrained Execution Region (CER) feature is not supported.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/RuntimeHelpersSamples02.cs(73,34): warning SYSLIB0004: 'Consistency' is obsolete: 'The Constrained Execution Region (CER) feature is not supported.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/RuntimeHelpersSamples02.cs(73,67): warning SYSLIB0004: 'Cer' is obsolete: 'The Constrained Execution Region (CER) feature is not supported.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/RuntimeHelpersSamples03.cs(61,14): warning SYSLIB0004: 'ReliabilityContractAttribute' is obsolete: 'The Constrained Execution Region (CER) feature is not supported.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/RuntimeHelpersSamples03.cs(61,34): warning SYSLIB0004: 'Consistency' is obsolete: 'The Constrained Execution Region (CER) feature is not supported.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/RuntimeHelpersSamples03.cs(61,67): warning SYSLIB0004: 'Cer' is obsolete: 'The Constrained Execution Region (CER) feature is not supported.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/SerializationSurrogateSamples01.cs(29,17): warning SYSLIB0011: 'BinaryFormatter.Serialize(Stream, object)' is obsolete: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/SerializationSurrogateSamples01.cs(32,34): warning SYSLIB0011: 'BinaryFormatter.Deserialize(Stream)' is obsolete: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/SerializationSurrogateSamples01.cs(47,21): warning SYSLIB0011: 'BinaryFormatter.Serialize(Stream, object)' is obsolete: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/SerializationSurrogateSamples01.cs(50,38): warning SYSLIB0011: 'BinaryFormatter.Deserialize(Stream)' is obsolete: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/SerializationSurrogateSamples01.cs(89,21): warning SYSLIB0011: 'BinaryFormatter.Serialize(Stream, object)' is obsolete: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/SerializationSurrogateSamples01.cs(92,38): warning SYSLIB0011: 'BinaryFormatter.Deserialize(Stream)' is obsolete: 'BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/RuntimeHelpersSamples03.cs(30,13): warning SYSLIB0004: 'RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(RuntimeHelpers.TryCode, RuntimeHelpers.CleanupCode, object?)' is obsolete: 'The Constrained Execution Region (CER) feature is not supported.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]
/workspace/try-csharp/TryCSharp.Samples/Advanced/RuntimeHelpersSamples02.cs(30,13): warning SYSLIB0004: 'RuntimeHelpers.PrepareConstrainedRegions()' is obsolete: 'The Constrained Execution Region (CER) feature is not supported.' [/workspace/try-csharp/TryCSharp.Samples/TryCSharp.Samples.csproj]

表示されている内容を見てみると、昔に書いたサンプルでCERを使っているところと、BinaryFormatterを使っている部分で出ている模様。.NET 5 になる前はこんなに出てなかったんですけどね。。。

で、ドキュメント見てみると、.NET 5 から警告を出すように変わっていたんですね。

docs.microsoft.com

CERの部分で、出ている警告はこれ。(SYSLIB0004) まあ、いまさらCERを使うことなんてないですが。

docs.microsoft.com

BinaryFormatterの部分で、出ている警告はこれ。(SYSLIB0011) BinaryFormatterは、今時ほぼ利用しないと思いますが、古いソースとかでは結構まだ出てきたりします。オブジェクトを簡易deep-copyしたい場合とか。

docs.microsoft.com

BinaryFormatterに関しては、セキュリティガイドも出ていますね。

docs.microsoft.com

要は「使うな」ってことが記載されています。

警告を抑制する方法

とはいえ、事情により使用しないといけないので、警告を抑制したい場合などは現実世界ではちょこちょこあったりします。

その場合、2通りの方法がある模様。

コード上で pragma つけて制御

// 抑制
#pragma warning disable SYSLIB0011


// 抑制解除
#pragma warning restore SYSLIB0011

プロジェクトファイルで指定

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   <TargetFramework>net5.0</TargetFramework>
   <!-- プロジェクト全体で SYSLIB0004 と SYSLIB0011 を抑制 -->
   <NoWarn>$(NoWarn);SYSLIB0004;SYSLIB0011</NoWarn>
  </PropertyGroup>
</Project>

参考資料

docs.microsoft.com

docs.microsoft.com


過去の記事については、以下のページからご参照下さい。

  • いろいろ備忘録日記まとめ

devlights.github.io

サンプルコードは、以下の場所で公開しています。

  • いろいろ備忘録日記サンプルソース置き場

github.com

github.com

github.com