Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

BinaryFormatter deprecation issue while using PythonEngine.Shutdown() #2469

Open
Labels
@Kaz8939

Description

@Kaz8939

Environment

  • Pythonnet version: 3.0.4
  • Python version: 3.11
  • Operating System: Windows 10
  • .NET Runtime: .NET8/netstandard 2.0

Details

Hi, I am facing a known issue with PythonEngine.Shutdown() that should have been solved. I saw looking around here that many others have had a problem while using the Shutdown function since it caused an exception due to the deprecation of BinaryFormatter in .NET8.
Here the exception I am getting:

System.NotSupportedException: 'BinaryFormatter serialization and deserialization are disabled within this application. Seehttps://aka.ms/binaryformatter for more information.'

I am aware of the workarounds already provided such as the management of "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization" parameter, if I manually manage this set the issue is not presenting anymore, but it seems to me that is more of a patch than a fix.

Since .NET9 will totally get rid of BinaryFormatter, and since I read in other threads that this issue should have been fixed, I was wondering if I was missing something or if the only way to manage this is by manually managing the serialization parameter.

Here follows the code I am using (in a project that targets netstandard2.0, while it is called by a .NET8 project).

usingPython.Runtime;usingSystem;usingSystem.IO;namespaceExecutePython{publicclassClass1{stringfilePath;publicClass1(stringFilePath){filePath=FilePath;Initialize();}publicvoidInitialize(){stringpythonDll=@"C:\Users\MyUser\AppData\Local\Programs\Python\Python311\python311.dll";Environment.SetEnvironmentVariable("PYTHONNET_PYDLL",pythonDll);PythonEngine.Initialize();}publicvoidExecutePython(){stringpyCode=File.ReadAllText(filePath);using(Py.GIL()){try{PythonEngine.Exec(pyCode);}catch(Exceptionex){//log error}}try{//AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", true);PythonEngine.Shutdown();//AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", false);}catch(Exceptione){}}}}

The code that is executing correctly in my python script is a sample code, in my case it just pops up a window to see that the script is executing correctly

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp