From 50752e88d4fc014e764c7a32bab8a1d133239ecd Mon Sep 17 00:00:00 2001 From: Legion-ale <73187779+Legion-ale@users.noreply.github.com> Date: Tue, 23 Sep 2025 15:53:45 +0200 Subject: [PATCH 1/2] Non-breaking workaround for BinaryFormatter deprecation, issue #2469 --- .../StateSerialization/NoopFormatter.cs | 6 +++- src/runtime/StateSerialization/RuntimeData.cs | 29 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/runtime/StateSerialization/NoopFormatter.cs b/src/runtime/StateSerialization/NoopFormatter.cs index f05b7ebb2..7ed7f9f13 100644 --- a/src/runtime/StateSerialization/NoopFormatter.cs +++ b/src/runtime/StateSerialization/NoopFormatter.cs @@ -5,7 +5,11 @@ namespace Python.Runtime; public class NoopFormatter : IFormatter { - public object Deserialize(Stream s) => throw new NotImplementedException(); + + public object Deserialize(Stream s) => throw new NotImplementedException( + "Cannot deserialize using 'NoopFormatter', implement 'Python.Runtime.RuntimeData.FormatterType' if needed" + ); + public void Serialize(Stream s, object o) {} public SerializationBinder? Binder { get; set; } diff --git a/src/runtime/StateSerialization/RuntimeData.cs b/src/runtime/StateSerialization/RuntimeData.cs index 8eda9ce0b..c0e7e2604 100644 --- a/src/runtime/StateSerialization/RuntimeData.cs +++ b/src/runtime/StateSerialization/RuntimeData.cs @@ -15,18 +15,43 @@ namespace Python.Runtime { public static class RuntimeData { + //TODO: put it in a utility class instead of inside the RuntimeData Class. + private static Version? GetNetVersion(string verToCheck) + { + // Examples: + // ".NET 8.0.0" + // ".NET 9.0.0" + // ".NET Framework 4.8.1" + // ".NET Core 3.1.32" + string desc = RuntimeInformation.FrameworkDescription; + if (desc.StartsWith(verToCheck, StringComparison.OrdinalIgnoreCase)) + { + var verStr = desc.Split(' ').Last(); + if (Version.TryParse(verStr, out var ver)) + return ver; + } + return null; + } public readonly static Func DefaultFormatterFactory = () => { - try + //The issue here is that just instancing BynaryFormatter doesn't solve the problem. + //you need to use it to generate the exception. + /*try { return new BinaryFormatter(); } catch { return new NoopFormatter(); + }*/ + var ver = GetNetVersion(".NET"); + if(ver != null && ver.Major >= 9) { + return new NoopFormatter(); + } else { + return new BinaryFormatter(); } - }; + } private static Func _formatterFactory { get; set; } = DefaultFormatterFactory; From f144d37bdc765211eff2f2a3f777aeb620ff7dfe Mon Sep 17 00:00:00 2001 From: Legion-ale <73187779+Legion-ale@users.noreply.github.com> Date: Tue, 30 Sep 2025 09:20:19 +0200 Subject: [PATCH 2/2] Update RuntimeData.cs Missing ; --- src/runtime/StateSerialization/RuntimeData.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/StateSerialization/RuntimeData.cs b/src/runtime/StateSerialization/RuntimeData.cs index c0e7e2604..95c586936 100644 --- a/src/runtime/StateSerialization/RuntimeData.cs +++ b/src/runtime/StateSerialization/RuntimeData.cs @@ -51,7 +51,7 @@ public static class RuntimeData } else { return new BinaryFormatter(); } - } + }; private static Func _formatterFactory { get; set; } = DefaultFormatterFactory;