SYSLIB0043: ECDiffieHellmanPublicKey.ToByteArray is obsolete

The following methods are obsolete, starting in .NET 7. Using them in code generates warning SYSLIB0043 at compile time.

The ECDiffieHellmanPublicKey.ToByteArray() method does not have an implied file format. Also, for the built-in implementations, it throws PlatformNotSupportedException on all non-Windows operating systems. Since ECDiffieHellmanPublicKey also has a standard format export (via the ExportSubjectPublicKeyInfo() method), the older member has been obsoleted.

Workaround

If you're exporting the public key value, use the ExportSubjectPublicKeyInfo() method instead.

For new derived types (or existing derived types that don't currently call the ECDiffieHellmanPublicKey(Byte[]) constructor), don't call the protected ECDiffieHellmanPublicKey(Byte[]) constructor, and either override ToByteArray() to throw an exception, or accept the default behavior of returning an empty array.

For existing derived types that already call the protected ECDiffieHellmanPublicKey(Byte[]) constructor, continue calling the constructor and suppress the SYSLIB0043 warning.

Suppress a warning

If you must use the obsolete APIs, you can suppress the warning in code or in your project file.

To suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the warning.

// Disable the warning.
#pragma warning disable SYSLIB0043

// Code that uses obsolete API.
// ...

// Re-enable the warning.
#pragma warning restore SYSLIB0043

To suppress all the SYSLIB0043 warnings in your project, add a <NoWarn> property to your project file.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   ...
   <NoWarn>$(NoWarn);SYSLIB0043</NoWarn>
  </PropertyGroup>
</Project>

For more information, see Suppress warnings.