Title: BinaryFormatter deprecation issue while using PythonEngine.Shutdown() · Issue #2469 · pythonnet/pythonnet · GitHub
Open Graph Title: BinaryFormatter deprecation issue while using PythonEngine.Shutdown() · Issue #2469 · pythonnet/pythonnet
X Title: BinaryFormatter deprecation issue while using PythonEngine.Shutdown() · Issue #2469 · pythonnet/pythonnet
Description: 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 aro...
Open Graph Description: 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 sh...
X Description: 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 sh...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/2469
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"BinaryFormatter deprecation issue while using PythonEngine.Shutdown()","articleBody":"### Environment\r\n\r\n- Pythonnet version: 3.0.4\r\n- Python version: 3.11\r\n- Operating System: Windows 10\r\n- .NET Runtime: .NET8/netstandard 2.0\r\n\r\n### Details\r\n\r\nHi, 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. \r\nHere the exception I am getting:\r\n\r\nSystem.NotSupportedException: 'BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatter for more information.'\r\n\r\nI 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.\r\n\r\nSince .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.\r\n\r\nHere follows the code I am using (in a project that targets netstandard2.0, while it is called by a .NET8 project). \r\n\r\n```csharp\r\nusing Python.Runtime;\r\nusing System;\r\nusing System.IO;\r\n\r\nnamespace ExecutePython\r\n{\r\n public class Class1\r\n {\r\n string filePath;\r\n \r\n public Class1(string FilePath) \r\n { \r\n filePath = FilePath;\r\n Initialize();\r\n }\r\n\r\n public void Initialize()\r\n {\r\n string pythonDll = @\"C:\\Users\\MyUser\\AppData\\Local\\Programs\\Python\\Python311\\python311.dll\";\r\n Environment.SetEnvironmentVariable(\"PYTHONNET_PYDLL\", pythonDll);\r\n PythonEngine.Initialize();\r\n }\r\n public void ExecutePython()\r\n {\r\n string pyCode = File.ReadAllText(filePath);\r\n\r\n using (Py.GIL())\r\n {\r\n try\r\n {\r\n PythonEngine.Exec(pyCode);\r\n }\r\n catch (Exception ex)\r\n {\r\n //log error\r\n }\r\n }\r\n \r\n try\r\n {\r\n //AppContext.SetSwitch(\"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization\", true);\r\n PythonEngine.Shutdown(); \r\n //AppContext.SetSwitch(\"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization\", false);\r\n }\r\n catch (Exception e)\r\n { }\r\n }\r\n }\r\n}\r\n```\r\n\r\nThe 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","author":{"url":"https://github.com/Kaz8939","@type":"Person","name":"Kaz8939"},"datePublished":"2024-10-01T09:58:21.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/2469/pythonnet/issues/2469"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:04641ab8-4e9f-b0d7-8537-c88533361f85 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EB90:2735DF:101EA4:16AF03:6970A071 |
| html-safe-nonce | 1fe727ca1b50048b5aedba6d0056bd35d9d3da0cddd912aa8cbab2bf34d2edaf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjkwOjI3MzVERjoxMDFFQTQ6MTZBRjAzOjY5NzBBMDcxIiwidmlzaXRvcl9pZCI6IjM0MzYwNDg5NjYxMzY5MzA0MTciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 00ce4502008a7c538d6c05ab3226536ea874dfd8e30dce8e1c536ef35f09148c |
| hovercard-subject-tag | issue:2558753669 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/2469/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b0a7f684eedfaa427250367b48ff017cfab39217d74559f3fd4b01dcf79b5aa4/pythonnet/pythonnet/issues/2469 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b0a7f684eedfaa427250367b48ff017cfab39217d74559f3fd4b01dcf79b5aa4/pythonnet/pythonnet/issues/2469 |
| og:image:alt | 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 sh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Kaz8939 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b06a4c45c45fd0bb038b3759265ea6e38211f45d18130bc65261990be6b5972a |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 0e1c4964831785bd64cb22d82e7cf2391ae01f45 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width