The requirement is to migrate a WCF service using Datasets to a gRPC service keeping the metadata when sending the data from the client to the service to identify Row modifications so we can update the database accordingly.
I'm working on a PoC serializing the DataSet as shown next:
using (MemoryStream ms = new MemoryStream()) {
dataSet.RemotingFormat = SerializationFormat.Binary;
BinaryFormatter fmt = new BinaryFormatter();
fmt.Binder = DataSetSerializationBinder.Default;
fmt.Serialize(ms, dataSet);
ms.Flush();
return ms.ToArray();
}
Sending the byte array from the client to the service and then in the service deserializing using the following:
using (MemoryStream ms = new MemoryStream(datasetBytes)) {
ms.Position = 0;
BinaryFormatter fmt = new BinaryFormatter();
fmt.Binder = DataSetSerializationBinder.Default;
var des = fmt.Deserialize(ms);
DataSet ds = (DataSet)des;
ms.Close();
}
Overriding the SerializationBinder like this:
public override Type BindToType(string assemblyName, string typeName){
if (assemblyName.Equals("DSSerializer"))
return typeof(System.Data.DataSet);
else
return defaultBinder.BindToType(assemblyName, typeName);
}
public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
assemblyName = "DSSerializer";
typeName = serializedType.FullName;
}
The Serialize and Deserialize methods are in the same library referenced from the client and the server, but when trying to Deserialize it throws the exception: Member 'XmlSchema' was not found. And before adding the SerializationBinder, the exception was: BinaryFormatter.Deserialize: specified cast is not valid.
Note that I need to transfer both schema information and DataRow.RowState
information. When I tried to transfer my DataSet using XML as shown by this answer to Loading a datatable into a xml and xml back into a datatable:
dataSet.WriteXml(ms, XmlWriteMode.WriteSchema);
var dataSet = new DataSet();
dataSet.ReadXml(ms, XmlReadMode.ReadSchema);
I found that the latter seems not to be transferred. This is confirmed by Preserving DataRowState when serializing DataSet using DataContractSerializer.
I understand BinaryFormatter is not the best option, what would be a better way to do it, or is there a way to make it work?