0

I am trying to migrate WPF application to .NET core . In my project serialization is used heavily. Below function work well on .NET 4.7 framework but after migrating to .NET core 3.1 gives error ' Serializing delegates is not supported on this platform' .

public static T Clone<T>(T source)
    {
        if (ReferenceEquals(source, null))
            return default(T);

        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream)
        {
            // test 
            formatter.Serialize(stream, source);
            stream.Seek(0, SeekOrigin.Begin);
            return (T) formatter.Deserialize(stream);
        }
    }

I have changed serializable classes where ever => operator used or any property used delegates. After doing this part of code worked but some of module in project still throwing error - Serializing delegates is not supported on this platform.

Is there anyway to find which particular object or property is throwing this exception?

any hint or help appreciated. thanks.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
JSH
  • 119
  • 6
  • 2
    Don't use BinaryFormatter for any reason. If you did, you were already exposed to sever security vulnerabilities and your application could easily break for the slightest change. There's no if or but here, you have to remove it. [BinaryFormatter is insecure and can't be made secure](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatters.binary.binaryformatter?view=net-5.0#remarks). Its removal is planned for many years now and .NET 1.0 didn't even have it – Panagiotis Kanavos Dec 03 '20 at 14:46
  • `Is there anyway to find which particular object or property is throwing this exception?` all of them, since all of them seem to be using this insecure class. BTW what you posted here isn't serialization at all, it's a 'quick&dirty" deep clone operation that uses serialization instead of copying values – Panagiotis Kanavos Dec 03 '20 at 14:49
  • So you really have two problems: 1) how to do implement deep cloning and 2) how to perform binary serialization - do you really need binary serialization or was this a byproduct of the deep-clone code? If you do need binary serialization, the best option right now is to use protobuf, either through .NET Core's gRPC classes or projects like protobuf.net – Panagiotis Kanavos Dec 03 '20 at 14:51
  • As for deep cloning, [this answer](https://stackoverflow.com/a/11308879/134204) is 3 times faster than BinaryFormatter, works with any object and won't break due to BinaryFormatter's deprecation – Panagiotis Kanavos Dec 03 '20 at 14:53
  • Thanks for the comments . Binaryformatter is working as expected we will see for further assessments once code is successfully running for .NET core .But my actual problem is - if a class let's say have method and properties and marked serializable how can I check at runtime which of its child classes or its properties are serializable using .NET core runtime? As i said same class is able to serialize in .net 4.7.2 framework. – JSH Dec 04 '20 at 18:28
  • BinaryFormatter isn't working as expected, that's the actual problem. That's why it's on the chopping block for years. What you see is the axe falling - after an overly long transition period, the axe is starting to fall - no more fixes and gradual degradation to force even the last people using it to abandon it before it's removed completely – Panagiotis Kanavos Dec 04 '20 at 18:32
  • You don't even want BinaryFormatter in this case. You want a deep clone method. Something that can be built as an extension method over `object` and reused on all classes. You only need to change a single method – Panagiotis Kanavos Dec 04 '20 at 18:33
  • In case you haven't realized how serious Microsoft is about this check this Github issue [Scale back `Serializable` for .NET Core 2.0](https://github.com/dotnet/runtime/issues/21433). Support for what you try to use now was intentionally removed from the entire .NET BCL 3 years ago. So the answer to `how can I check at runtime which of its child classes or its properties are serializable using .NET core runtime` is `Assume none is, as the base classes won't be` – Panagiotis Kanavos Dec 04 '20 at 18:40
  • some of core migration issues I have resolved referring -[link](https://github.com/dotnet/runtime/issues/26625). Now I do see serialization with binaryformatter is crux of this. thanks @PanagiotisKanavos . I will try other serialization techniques – JSH Dec 04 '20 at 18:46

0 Answers0