silverhack/monkey365

[Bug]: BinaryFormatter.Serialize in Copy-psObject causes exception to be thrown when running under .NET 8

dom34 opened this issue · 2 comments

dom34 commented

What happened?

When I run Monkey365 using .NET 8, several exceptions are raised. The error message is:

[Copy-psObject] - System.Management.Automation.MethodInvocationException: Exception calling "Serialize" with "2" argument(s): "BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatter for more information."

This is a breaking change in .NET 8

https://learn.microsoft.com/en-us/dotnet/core/compatibility/serialization/8.0/binaryformatter-disabled

Previous behavior
In .NET 7, the BinaryFormatter.Serialize(Stream, Object) and BinaryFormatter.Deserialize(Stream) methods were marked obsolete and raised an error at compile time. However, if your application suppressed the obsoletion, it could still call the methods and they functioned properly in most project types (excluding ASP.NET, WASM, and MAUI). For example, the APIs functioned correctly in a console app.

New behavior
Starting in .NET 8, the affected methods throw a NotSupportedException at run time across all project types except Windows Forms and WPF. The APIs continue to remain obsolete (as error) across all project types, including Windows Forms and WPF.

How to reproduce it

Make sure your instance of Powershell is using the .NET 8 runtime:

user@debian12$ pwsh
PowerShell 7.4.0
PS /home/user> [System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription
.NET 8.0.0

then import and invoke the module in the usual way. The exceptions will be logged.

Expected behavior

No exceptions

Screenshots or Logs

[14:52:21:537] - [Copy-psObject] - System.Management.Automation.MethodInvocationException: Exception calling "Serialize" with "2" argument(s): "BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatter for more information."
 ---> System.NotSupportedException: BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatter for more information.
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
   at CallSite.Target(Closure, CallSite, Object, Object, Object)
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame). LineNumber:  - exception - debian12 -

From where are you running Monkey365?

Please, complete the following information:

  • Resource: Workstation
  • OS: Debian 12 Linux
  • PowerShell Version:
PS /home/user> $PsVersionTable

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Debian GNU/Linux 12 (bookworm)
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
  • Monkey365 Version: Monkey365 v0.91.1-beta

Additional context
I am using the PowerShell snap for Linux - the full package has not been fully tested on Debian 12

Hi @dom34,

Good catch! And thanks for submitting this issue, I'll fix it shortly.

Fixed in main branch.

Cheers,