public static void DCS_FileStreamSurrogate() { const string TestFileName = "Test.txt"; const string TestFileData = "Some data for data contract surrogate test"; // Create the serializer and specify the surrogate var dcs = new DataContractSerializer(typeof(MyFileStream)); dcs.SetSerializationSurrogateProvider(MyFileStreamSurrogateProvider.Singleton); // Create and initialize the stream byte[] serializedStream; // Serialize the stream using (MyFileStream stream1 = new MyFileStream(TestFileName)) { stream1.WriteLine(TestFileData); using (MemoryStream memoryStream = new MemoryStream()) { dcs.WriteObject(memoryStream, stream1); serializedStream = memoryStream.ToArray(); } } // Deserialize the stream using (MemoryStream stream = new MemoryStream(serializedStream)) { using (MyFileStream stream2 = (MyFileStream)dcs.ReadObject(stream)) { string fileData = stream2.ReadLine(); Assert.StrictEqual(TestFileData, fileData); } } }
public static DataContractSerializer GetDataContractSerializer <T>() { var serializer = new DataContractSerializer(typeof(T)); serializer.SetSerializationSurrogateProvider(new ImmutableSurrogateProvider()); return(serializer); }
public static void DCS_MyPersonSurrogate() { DataContractSerializer dcs = new DataContractSerializer(typeof(Family)); dcs.SetSerializationSurrogateProvider(new MyPersonSurrogateProvider()); MemoryStream ms = new MemoryStream(); Family myFamily = new Family { Members = new NonSerializablePerson[] { new NonSerializablePerson("John", 34), new NonSerializablePerson("Jane", 32), new NonSerializablePerson("Bob", 5), } }; dcs.WriteObject(ms, myFamily); ms.Position = 0; var newFamily = (Family)dcs.ReadObject(ms); Assert.StrictEqual(myFamily.Members.Length, newFamily.Members.Length); for (int i = 0; i < myFamily.Members.Length; ++i) { Assert.StrictEqual(myFamily.Members[i].Name, newFamily.Members[i].Name); } }
public static DataContractSerializer GetActorMessageSerializer(IEnumerable <Type> knownTypes) { var types = new List <Type>(knownTypes); types.AddRange(DefaultKnownTypes); #if DotNetCoreClr DataContractSerializer dataContractSerializer = new DataContractSerializer( typeof(ActorMessageBody), new DataContractSerializerSettings() { MaxItemsInObjectGraph = int.MaxValue, KnownTypes = types }); dataContractSerializer.SetSerializationSurrogateProvider(new ActorDataContractSurrogate()); #else var dataContractSerializer = new DataContractSerializer( typeof(ActorMessageBody), new DataContractSerializerSettings() { DataContractSurrogate = new ActorDataContractSurrogate(), MaxItemsInObjectGraph = int.MaxValue, KnownTypes = types }); #endif return(dataContractSerializer); }
public static void DCS_MyPersonSurrogate_Stress() { // This test is to verify a bug fix made in ObjectToIdCache.cs. // There was a bug with ObjectToIdCache.RemoveAt() method which might cause // one item be added into the cache more than once. // The issue could happen in the following scenario, // 1) ObjectToIdCache is used. ObjectToIdCache is used for DataContract types // marked with [DataContract(IsReference = true)]. // 2) ObjectToIdCache.RemoveAt() is called. The method is called only when one uses // DataContract Surrogate. // 3) There's hash-key collision in the cache and elements are deletes at certain position. // // The reason that I used multi-iterations here is because the issue does not always repro. // It was a matter of odds. The more iterations we run here the more likely we repro the issue. for (int iterations = 0; iterations < 5; iterations++) { int length = 2000; DataContractSerializer dcs = new DataContractSerializer(typeof(FamilyForStress)); dcs.SetSerializationSurrogateProvider(new MyPersonSurrogateProvider()); var members = new NonSerializablePersonForStress[2 * length]; for (int i = 0; i < length; i++) { var m = new NonSerializablePersonForStress("name", 30); // We put the same object at two different slots. Because the DataContract type // is marked with [DataContract(IsReference = true)], after serialization-deserialization, // the objects at this two places should be the same object to each other. members[i] = m; members[i + length] = m; } MemoryStream ms = new MemoryStream(); FamilyForStress myFamily = new FamilyForStress { Members = members }; dcs.WriteObject(ms, myFamily); ms.Position = 0; var newFamily = (FamilyForStress)dcs.ReadObject(ms); Assert.StrictEqual(myFamily.Members.Length, newFamily.Members.Length); for (int i = 0; i < myFamily.Members.Length; ++i) { Assert.Equal(myFamily.Members[i].Name, newFamily.Members[i].Name); } var resultMembers = newFamily.Members; for (int i = 0; i < length; i++) { Assert.Equal(resultMembers[i], resultMembers[i + length]); } } }
public static void DCS_MyPersonSurrogate_Stress() { // This test is to verify a bug fix made in ObjectToIdCache.cs. // There was a bug with ObjectToIdCache.RemoveAt() method which might cause // one item be added into the cache more than once. // The issue could happen in the following scenario, // 1) ObjectToIdCache is used. ObjectToIdCache is used for DataContract types // marked with [DataContract(IsReference = true)]. // 2) ObjectToIdCache.RemoveAt() is called. The method is called only when one uses // DataContract Surrogate. // 3) There's hash-key collision in the cache and elements are deletes at certain position. // // The reason that I used multi-iterations here is because the issue does not always repro. // It was a matter of odds. The more iterations we run here the more likely we repro the issue. for (int iterations = 0; iterations < 5; iterations++) { int length = 2000; DataContractSerializer dcs = new DataContractSerializer(typeof(FamilyForStress)); dcs.SetSerializationSurrogateProvider(new MyPersonSurrogateProvider()); var members = new NonSerializablePersonForStress[2 * length]; for (int i = 0; i < length; i++) { var m = new NonSerializablePersonForStress("name", 30); // We put the same object at two different slots. Because the DataContract type // is marked with [DataContract(IsReference = true)], after serialization-deserialization, // the objects at this two places should be the same object to each other. members[i] = m; members[i + length] = m; } MemoryStream ms = new MemoryStream(); FamilyForStress myFamily = new FamilyForStress { Members = members }; dcs.WriteObject(ms, myFamily); ms.Position = 0; var newFamily = (FamilyForStress)dcs.ReadObject(ms); Assert.StrictEqual(myFamily.Members.Length, newFamily.Members.Length); for (int i = 0; i < myFamily.Members.Length; ++i) { Assert.StrictEqual(myFamily.Members[i].Name, newFamily.Members[i].Name); } var resultMembers = newFamily.Members; for (int i = 0; i < length; i++) { Assert.Equal(resultMembers[i], resultMembers[i + length]); } } }
public virtual XmlObjectSerializer CreateSerializer(Type type, XmlDictionaryString name, XmlDictionaryString ns, IList <Type> knownTypes) { DataContractSerializerSettings settings = new DataContractSerializerSettings(); settings.RootName = name; settings.RootNamespace = ns; settings.KnownTypes = knownTypes; settings.MaxItemsInObjectGraph = MaxItemsInObjectGraph; settings.DataContractResolver = DataContractResolver; DataContractSerializer dcs = new DataContractSerializer(type, settings); dcs.SetSerializationSurrogateProvider(SerializationSurrogateProvider); return(dcs); }
/// <summary> /// Gets the settings used to create DataContractSerializer for serializing and de-serializing request message body. /// </summary> /// <param name="remotingRequestType">Remoting RequestMessageBody Type.</param> /// <param name="knownTypes">The return types of all of the methods of the specified interface.</param> /// <returns><see cref="DataContractSerializerSettings" /> for serializing and de-serializing request message body.</returns> internal DataContractSerializer CreateMessageBodyDataContractSerializer( Type remotingRequestType, IEnumerable <Type> knownTypes) { var serializer = new DataContractSerializer( remotingRequestType, new DataContractSerializerSettings() { MaxItemsInObjectGraph = int.MaxValue, KnownTypes = knownTypes, }); serializer.SetSerializationSurrogateProvider(new ActorDataContractSurrogate()); return(serializer); }
internal static DataContractSerializer CreateDataContractSerializer(Type actorStateType) { var dataContractSerializer = new DataContractSerializer( actorStateType, new DataContractSerializerSettings { MaxItemsInObjectGraph = Int32.MaxValue, #if !DotNetCoreClr DataContractSurrogate = ActorDataContractSurrogate.Singleton, #endif KnownTypes = new[] { typeof(ActorReference), } }); #if DotNetCoreClr dataContractSerializer.SetSerializationSurrogateProvider(ActorDataContractSurrogate.Singleton); #endif return(dataContractSerializer); }