Created
October 18, 2018 13:37
-
-
Save thaianhduc/e984557974a625e2ec4f06f9e09d00e4 to your computer and use it in GitHub Desktop.
An improved version with LargeObject to prevent the serialization cose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class BinaryDataContractSerializer | |
{ | |
public static byte[] Serialize<T>(T obj) where T : class | |
{ | |
if (obj == null) | |
return null; | |
var serializer = new DataContractSerializer(typeof(T)); | |
using (var memStream = new MemoryStream()) | |
{ | |
using (var writer = XmlDictionaryWriter.CreateBinaryWriter(memStream)) | |
{ | |
serializer.WriteObject(writer, obj); | |
} | |
return memStream.ToArray(); | |
} | |
} | |
public static T Deserialize<T>(Response response) where T : class | |
{ | |
if (response == null) | |
return default(T); | |
if (response.LargeObject != null) | |
return response.LargeObject as T; | |
var serializer = new DataContractSerializer(typeof(T)); | |
using (var stream = new MemoryStream(response.SerializedData)) | |
{ | |
using (var reader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max)) | |
{ | |
return (T)serializer.ReadObject(reader); | |
} | |
} | |
} | |
} | |
[DataContract] | |
public class Request | |
{ | |
[DataMember] | |
public string Payload { get; set; } | |
} | |
[DataContract] | |
public class DownloadFileResponse : LargeObject | |
{ | |
[DataMember] | |
public byte[] Data { get; set; } | |
} | |
[DataContract] | |
public class Response | |
{ | |
[DataMember] | |
public byte[] SerializedData { get; set; } | |
[DataMember] | |
public string QualifiedAssemblyDataType { get; set; } | |
[DataMember] | |
public LargeObject LargeObject { get; set; } | |
} | |
/// <summary> | |
/// Contracts inherit from it will not pay the serialization cost. | |
/// </summary> | |
[DataContract] | |
[KnownType(typeof(DownloadFileResponse))] | |
public abstract class LargeObject | |
{ | |
} | |
public class ServiceImpl | |
{ | |
public Response Execute(Request request) | |
{ | |
// Assuming the request is to read data from a file | |
byte[] data = File.ReadAllBytes(request.Payload); | |
var responseData = new DownloadFileResponse | |
{ | |
Data = data | |
}; | |
var lo = responseData as LargeObject; | |
if (lo != null) | |
{ | |
return new Response{LargeObject = lo}; | |
} | |
return new Response | |
{ | |
QualifiedAssemblyDataType = typeof(DownloadFileResponse).AssemblyQualifiedName, | |
SerializedData = BinaryDataContractSerializer.Serialize(responseData) | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment