Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BinaryFormatter serialization and deserialization warning which throws an error in .net8 #2282

Open
@RobertClapham

Description

@RobertClapham

Environment

  • Pythonnet version: 3.0.3
  • Python version: 3.9
  • Operating System: Windows 11
  • .NET Runtime: .net8 (release version 16/11/23)

Details

  • When shutting down the the PythonEngine via a Dispose method on the impermentation class, the error occurs at the PythonEngine.Shutdown(); method and throws the error System.NotSupportedException: BinaryFormatter serialization and deserialization are disabled within this application (see trace below). The same code worked in .net7 without any issues, the error message leads to this description

  • https://learn.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide

  • Proir to the Shutdown error the pythonnet works correctly and calls python functions and returns the expected results

  • The resolution is to add true to the .csproj file

  • <PropertyGroup>
      <TargetFramework>net8.0</TargetFramework>
      <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
     </PropertyGroup>
    

    TODO

  • _logger.Log is to to trace the point of failure

  • _logger.Log(LogLevel.Error, "Shutting down Python engine"); runs

  • it crashes at the PythonEngine.Shutdown(); method

  • _logger.Log(LogLevel.Error, "Python engine shut down"); does not run

    private void Dispose(bool disposing)
    {
    _logger.Log(LogLevel.Error, "Dispose");
    if (_disposed) return;
    if (disposing)
    {
    _logger.Log(LogLevel.Error, "Shutting down Python engine");
    PythonEngine.Shutdown();
    _logger.Log(LogLevel.Error, "Python engine shut down");
    }
    _disposed = true;
    }

    print('TODO')
  • System.NotSupportedException: BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatt...

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 Python.Runtime.RuntimeData.Stash()
at Python.Runtime.Runtime.Shutdown()
at Python.Runtime.PythonEngine.Shutdown()
at EcgAi.Training.Infrastructure.Python.PythonEnvironment.Dispose(Boolean disposing) in C:\Data\Rider\EcgAi.Training\Src\EcgAi.Training.Infrastructure.Python\PythonEnvironment.cs:line 113
at EcgAi.Training.Infrastructure.Python.PythonEnvironment.Dispose() in xxxxxxxxxxxxxxxxxxxxxxxxx

    print('TODO')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions