November Happy Hour will be moved to Thursday December 5th.

[.NET 9] PlatformNotSupportedException: BinaryFormatter serialization and deserialization have been removed

Vote:
 

Hello Guys,

As you guys aware, .NET 9 has been released yesterday. We tried upgrading our client projects and were able to run without any issues until I was on older version of CMS (12.21.7) and Commerce (14.23.0). As soon as I upgraded to latest version of Episerver packages it throws following error. 

EPiServer.Framework.Initialization.InitializationException
  HResult=0x80131500
  Message=Initialize action failed for Initialize on class EPiServer.Initialization.Internal.ModelSyncInitialization, EPiServer, Version=12.22.0.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7
  Source=EPiServer.Framework
  StackTrace:
   at EPiServer.Framework.Initialization.InitializationEngine.InitializeModules()
   at EPiServer.Framework.Initialization.InitializationEngine.ExecuteTransition(Boolean continueTransitions)
   at EPiServer.Framework.Initialization.InitializationEngine.Initialize()
   at EPiServer.Hosting.Internal.EPiServerFrameworkHost.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.<<StartAsync>b__14_1>d.MoveNext()
   at Microsoft.Extensions.Hosting.Internal.Host.<ForeachService>d__17`1.MoveNext()
   at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__14.MoveNext()
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at Dolby.Web.Website.Program.Main(String[] args) in C:\Data\Projects\**********\Program.cs:line 55

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
PlatformNotSupportedException: BinaryFormatter serialization and deserialization have been removed. See https://aka.ms/binaryformatter for more information.
#332860
Edited, Nov 13, 2024 21:30
Vote:
 

Update: I restarted the upgrade and followed the sequence below and was able to make it work until I upgraded CMS but as soon as I upgraded Commerce, it started throwing error. So the issue is not with EPiserver Framework version but Commerce version.

  • Upgrade EPiServer Find related packages - No issue
  • Upgrade TinyMce and EPiServer Forms - No issue
  • Upgrade EPiServer ServiceAPI and ImageSharp - No issue
  • Upgrade EPiServer and AspnetCore packages from 12.21.7 to 12.22.0 - No issue
  • Upgrade Commerce.UI+Personalization.Commerce+Tracking.Commerce from 14.23 to 14.28 - No issue
  • Upgrade Commerce 14.28.0 to 14.29.0 = Failed with above error
#332910
Edited, Nov 14, 2024 4:26
Vote:
 

Thanks for the report. We are aware Commerce has an old dependency to BinaryFormatter. We know how to remove the dependency but it is some work, especially doing it without breaking changes. I have brought this up for prioritization again, but for now Commerce can only run on .NET 8.

#332911
Nov 14, 2024 7:36
Vote:
 

Thanks @Magnus. 

After failing to make it work with 14.29.0 I tested following two scenarios. Sharing in case someone else encounters and it may help them.

  • If you upgrade from 14.29.0 to 14.30.0 the solution loads without any error.
  • If you upgrade directly from older version of Commerce to 14.30.0, it fails to load the site with expected BinaryFormatter error. 

Conclusion: We will use .NET 8 for the time being. 

#332917
Edited, Nov 14, 2024 15:27
Vote:
 

I noticed too that I sometimes got an error already in startup, in another project I didn't. But you will get an error as soon as you publish a product update (or price update etc). So it is not currently usable on .NET 9.

#332918
Nov 14, 2024 15:29
Vote:
 

There is an official compatibility package available on NuGet: https://www.nuget.org/packages/System.Runtime.Serialization.Formatters

It enabled us to use CMS 12 with .NET 9. But it's great to hear that Optimizely is working on replacing BinaryFormatter with something modern and more secure!

#332980
Nov 15, 2024 13:06
Sujit Senapati - Nov 15, 2024 14:53
You are right. However, Microsoft recommended against it as BinaryFormatter has serious security risks and it is an unsupported package. If someone is upgrading to .NET 9 and cant wait then it can be beneficial but if you can wait, it is recommended to not use the unsupported package.
Vote:
 

Oh, really? Surprised it works as a drop-in replacement without any changes to our code. Thanks for the tip!

We're going to rip it out anyway I think, it isn't actually needed for anything, it is a legacy to send remote events from the time when there was a separate Content Manager application. We can solve the same thing using regular in-process events now.

#332981
Nov 15, 2024 13:09
Vote:
 

Hi

Has anyone tested with placing an order? I received "Type provided must be an Enum" exception from calling OrderRepository Save method

  System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
       ---> System.ArgumentException: Type provided must be an Enum.
         at System.ComponentModel.EnumConverter..ctor(Type type)
         at Mediachase.Commerce.Shared.ResourceEnumConverter..ctor(Type type)
         at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
         at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
         --- End of inner exception stack trace ---
         at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
         at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
         at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
         at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
         at System.ComponentModel.ReflectTypeDescriptionProvider.CreateInstance(Type objectType, Type callingType)
         at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectedTypeData.GetConverter(Object instance, Boolean verifyIsRegisteredType)
         at System.ComponentModel.TypeDescriptor.DefaultTypeDescriptor.GetConverter()
         at System.ComponentModel.TypeDescriptor.GetConverter(Type type)
         at Newtonsoft.Json.Serialization.JsonTypeReflector.CanTypeDescriptorConvertString(Type type, TypeConverter& typeConverter)
         at Newtonsoft.Json.Serialization.DefaultContractResolver.CanConvertToString(Type type)
         at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
         at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
         at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
         at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
         at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
         at EPiServer.Commerce.Order.Internal.DataAccess.SerializableCartDB.Save(SerializableCart cart)
         at EPiServer.Commerce.Order.Internal.SerializableCartProvider.Save(ICart cart)
         at EPiServer.Commerce.Order.Internal.DefaultOrderRepository.Save(IOrderGroup order)
#333335
Nov 23, 2024 4:01
Sujit Senapati - Nov 23, 2024 4:06
With .NET 9 there has been change in how Enum Flags work. Here is the link. https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/9.0/enumconverter

Wait for Opti team to officially support the version until you use .NET 9.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.