.NET 5.0 BinaryFormatter

Marc Al 206 Reputation points
2021-01-19T05:22:36.133+00:00

Hello, I have a dataset with some datas (60 tables 30 relations) and that I must transfer it to another application (it is a cache file made when connected on a server and used locally when there is no connection possible). With .NET 4.6 (and so on) all is fine.
The dataset loads in less than 3 seconds. In .NET core 5 it is not possible.
so I have tried to use the WriteXML (with schema) bit it takes 28 seconds (really too much to be usable)
I have seen that there is a flag that can be put in the project <EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization> but when trying so serialize I have the error

Type 'System.Data.SimpleType' in Assembly 'System.Data.Common, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

Is there something else I am missing? I can't find any serializer allowing me to serialize a complete dataset.
Using the EnforceConstraints False or importing the schema first and using ReadXML without the infer doesn't change anything with the speed (still around 28 sec.) Thank you for your help Marc

The serialize code

ds.RemotingFormat = SerializationFormat.Binary; 
IFormatter myFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 
var myStream = new MemoryStream(); try { myFormatter.Serialize(myStream, ds); 
Stream myStreamFina = new FileStream(FileName, FileMode.Create, FileAccess.Write); 
var ZipStream = new GZipStream(myStreamFina, CompressionMode.Compress, false);
 ZipStream.Write(myStream.GetBuffer(), 0, (int)myStream.Length); 
ZipStream.Flush(); 
// Close the stream. 
myStreamFina.Flush(); 
ZipStream.Close(); 
myStreamFina.Close(); 
} catch (Exception Ex) { ......... }

The ReadXML of the dataset.

var ds = new DataSet(); 
try { using (FileStream fs = new FileStream(FileName, FileMode.Open)) 
{ Stream s = new GZipStream(fs, CompressionMode.Decompress); 
//Stream s = new GZipStream(fs, CompressionMode.Compress);
 ds.ReadXml(s); s.Close(); }
 } catch (Exception) { }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,907 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 76,391 Reputation points Microsoft Vendor
    2021-01-19T07:50:34.727+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    I can't find any serializer allowing me to serialize a complete dataset.

    If You want to serialize dataset to xml, you can serialize it directly DataTable myTable = new DataTable(); myTable.WriteXml(@"c:\myfile");

    If You want to serialize dataset to string, you can serialize it by string temp = JsonConvert.SerializeObject(dt); if you want to convert this json to a list of objects (it could be your EF table) then use the below:

       dbContext db = new dbContext();  
       List<Object> jsonList = (List<Object>)JsonConvert.DeserializeObject(temp, typeof(List<Object>));  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Marc Al 206 Reputation points
    2021-01-19T08:45:45.043+00:00

    Hello,

    Thank you for the time you took to answer me with more ideas.
    I have the following problem :

    • Dataset.WriteXML works but Dataset.ReadXML is slow (about 28 sec. for my Dataset)

    For the Newtonsoft.Json perhaps it is not compatible anymore for the Dataset ?

    using Newtonsoft.Json;

    string str_Tmp = JsonConvert.SerializeObject(ds);

    DataSet TEST= (DataSet)JsonConvert.DeserializeObject(str_Tmp);

    EDIT : I have changed to DataSet TEST = (DataSet)JsonConvert.DeserializeObject<DataSet>(str_Tmp);
    And it is working. For the dataset I need 16,4 sec.
    But I lose the relations that must be created after that. That is still really slow compared to the 2.4 sec. of the binary formatter.
    Do you have any other clue?

    EDIT2 : Using Dataset.WriteSchema in the exportation and reading the Schema after the deserialization isn't working.
    "Invalid 'Key' node inside constraint named: Relation2."

    EDIT3 : if a table is empty, there is no column in the table (and so a merge won't work later)(and adding relations manually won't work neither.

    Thank you
    Marc


  3. Marc Al 206 Reputation points
    2021-01-21T14:57:42.647+00:00

    Hello,

    OK I will post a question about the speed of Dataset.ReadXML on this forum .

    Thanks again
    Marc


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.