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..95c586936 100644 --- a/src/runtime/StateSerialization/RuntimeData.cs +++ b/src/runtime/StateSerialization/RuntimeData.cs @@ -15,16 +15,41 @@ 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(); } };