none
DataContractSerializer stops working RRS feed

  • Question

  • Hi all!

    I'm working on a big software project in which we are encountering a very strange problem. We have been looking into it for several weeks now, thats why we try to get some help here now:

    We try to send an object of the class Project over WCF with a net.tcp binding. The object which is sent has several lists of other classes also containing lists of other classes and so on. The references in there are cyclic.

    Actually everything works as expected. But at some point, after the user made some normal changes, the project can't be loaded from the server anymore. After the timeout the client throws an exception telling that there has been some problem.

    We could debug it untill the point, where the datacontract serializer just stops doing his work - after enabling more exceptions we could catch an index out of bounce exception in the function EnsureSetAsIsReference[1]. In there we have an isReferenceArray of size 8, but the index (count-1 in code) is 8 (the value of count is 9 at this point).

    Can someone probably tell us, why this error is occuring?

    It seems that this problem occurs totally random. Project has the same amount of objects, only some properties of some classes have been modified (like a description, or changing a boolean value), and then suddenly this bug occurs.

    I really appreciate any help!

    Greets,

    Sam

    (sorry, it seems that my account is not verified, so I can't post a proper link)

    [1] http://referencesource.microsoft.com/#System.Runtime.Serialization/System/Runtime/Serialization/ObjectReferenceStack.cs,00526dec84163041

    Wednesday, September 13, 2017 10:04 AM

Answers

  • Hi Sam,

    >> but as it seems the project is "fixed" now because the owner of the project continued working, and got it into a working state it seems.

    I am glad your code works now, and I would suggest you mark your reply as answer to close this thread.

    >> why could he continue working

    If you are interested on the root cause of this issue, I would suggest you post a new thread, and share us a simple project to reproduce your issue.

    >> Is it possible, that whenever the serialization works, it is just some kind of luck, that the isReferenceArray is just big enough and that we are missing something essential?

    Based on your description, it seems to be related with n-to-n mapping table, but I would suggest you post a new thread and share us a simple demo, and then we could work closely with you.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Qerildan Wednesday, September 20, 2017 5:35 AM
    Wednesday, September 20, 2017 2:19 AM

All replies

  • Hi Sam,

    Based on your description, it seems to be related with your specific class. Did you add DataContract and DataMember for the class? I suggest you check below link whether you used unsupported types.

    #Types Supported by the Data Contract Serializer

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/types-supported-by-the-data-contract-serializer

    I would suggest you create a new WCF Service with this class, will your issue be reproduceable? It would be helpful if you could share us the class and detail steps to reproduce your issue.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 14, 2017 1:39 AM
  • Hello Edward!

    Thank you for your answer! I will carefully work through the provided information, and will report my findings here.

    But this might take some days, since I'm very busy atm.

    Thank you for your afford!

    Greets,

    Sam


    • Edited by Qerildan Thursday, September 14, 2017 7:21 AM
    Thursday, September 14, 2017 7:20 AM
  • Hi Sam,

    If you have any update about your issue, please feel free to let us know.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 15, 2017 1:29 AM
  • Hello Edward!

    We have been looking through the provided information, but till now we could not find any problem.

    At the bottom you can find our defenition of the class project. As I already mentioned, project is a root class and the object graph contains about 50 different classes - if fully loaded.

    We can reproduce the explained bug in the EnsureSetAsIsReference function in the  following way:

    Project->Projectmilestonemappings->Projectmilestone->Deliverableprojecttaskmilestonemapping

    Deliverableprojecttaskmilestonemapping also has a list of Informationmappings. If we do not add this mappings, the error occures. We are aware that this example might not help you in any way. But we still want to give an example, since it is very hard to move the project in some smaller scale project which could give you the chance to debug it.

    Is there any change to get an unoptimized version of the System.Runtime.Serialization.dll, because we could try to debug the code and possibly find the error.

    Thank you in advance!

    Greets, Sam

    namespace NHibernateHelper
    {
        [Serializable]
        [DataContract(IsReference = true)]
        public partial class Project : NHibernateHelper.Mappings.Contract.Item
        {
            public Project()
            {
                Projectmilestonemappings = new List<Projectmilestonemapping>();
                Projecttasks = new List<Projecttask>();
                Rolepersonificationmappings = new List<Rolepersonificationmapping>();
                Userrightmappings = new List<Userrightmapping>();
                SubProjects = new List<Project>();
                Clusterrationalemappings = new List<Clusterrationalemapping>();
                Deliverableprojectmappings = new List<Deliverableprojectmapping>();
                Reviewmappings = new List<Reviewmapping>();
                OpenIssues = new List<OpenIssues>();
                Activityrationalemappings = new List<Activityrationalemapping>();
            }
    
            [DataMember]
            public virtual string Rating { get; set; }
    
            [DataMember]
            public virtual IList<Project> SubProjects { get; set; }
    
            [DataMember]
            public virtual Project ParentProject { get; set; }
    
            [DataMember]
            public virtual string ProjectIntegrityPath { get; set; }
    
            [DataMember]
            public virtual string Title { get; set; }
    
            [DataMember]
            public virtual string Type { get; set; }
    
            [DataMember]
            public virtual string Customer { get; set; }
    
            [DataMember]
            public virtual string CustomerShort { get; set; }
    
            [DataMember]
            public virtual short IsActive { get; set; }
    
            [DataMember]
            public virtual string Description { get; set; }
    
            [DataMember]
            public virtual short? Status { get; set; }
    
            [DataMember]
            public virtual string StatusDescription { get; set; }
    
            [DataMember]
            public virtual IList<Projectmilestonemapping> Projectmilestonemappings { get; set; }
    
            [DataMember]
            public virtual IList<Projecttask> Projecttasks { get; set; }
            
            [DataMember]
            public virtual IList<OpenIssues> OpenIssues { get; set; }
    
            [DataMember]
            public virtual IList<Rolepersonificationmapping> Rolepersonificationmappings { get; set; }
    
            [DataMember]
            public virtual Standard Standard { get; set; }
    
            [DataMember]
            public virtual IList<Userrightmapping> Userrightmappings { get; set; }
    
            [DataMember]
            public virtual IList<Clusterrationalemapping> Clusterrationalemappings { get; set; }
    
            [DataMember]
            public virtual IList<Activityrationalemapping> Activityrationalemappings { get; set; }
    
            [DataMember]
            public virtual IList<Deliverableprojectmapping> Deliverableprojectmappings { get; set; }
            
            [DataMember]
            public virtual IList<Reviewmapping> Reviewmappings { get; set; }
    
            [OnDeserializing]
            private void DeserializationInitializer(StreamingContext ctx)
            {
                Projectmilestonemappings = new List<Projectmilestonemapping>();
                Projecttasks = new List<Projecttask>();
                Reviewmappings = new List<Reviewmapping>();
                Rolepersonificationmappings = new List<Rolepersonificationmapping>();
                Userrightmappings = new List<Userrightmapping>();
                SubProjects = new List<Project>();
                Clusterrationalemappings = new List<Clusterrationalemapping>();
                Deliverableprojectmappings = new List<Deliverableprojectmapping>();
                OpenIssues = new List<OpenIssues>();
                Activityrationalemappings = new List<Activityrationalemapping>();
            }
    
            public virtual bool AddProjectmilestonemapping(Projectmilestonemapping projectmilestonemapping)
            {
                
            }
    
    
            public virtual bool AddRolepersonificationmapping(Rolepersonificationmapping rolepersonificationmapping)
            {
                
            }
    
            public virtual bool RemoveRolepersonificationmapping(Rolepersonificationmapping rolepersonificationmapping)
            {
               
            }
    
            private int? _hashCode;
    
            public override int GetHashCode()
            {            
            }
    
            public override bool Equals(object obj)
            {           
    		}
    
            public static bool operator ==(Project lhs, Project rhs)
            {
                return Equals(lhs, rhs);
            }
    
            public static bool operator !=(Project lhs, Project rhs)
            {
                return !Equals(lhs, rhs);
            }
        }
    }


    • Edited by Qerildan Monday, September 18, 2017 7:44 AM
    Monday, September 18, 2017 7:08 AM
  • Hi again,

    we just have been able to debug the object array.

    objectArray has a size of 16, with 9 elements in it.

    isReferenceArray has a size of 8, and we try to write into the element with index 8, the 9th  element.

    isReferenceArray[0-6] = true, while isReferenceArray[7] = false.

    So it looks like we need to know why it did not increase the size and why isReferenceArray[7] is false.

    Thank you for your effort!

    Greets Sam


    • Edited by Qerildan Monday, September 18, 2017 9:18 AM
    Monday, September 18, 2017 7:44 AM
  • Hi Sam,

    Based on your description, you assume your issue is related with DataContractSerializer.

    How did you use DataContractSerializer?

    I suggest you make a test with below code to serialize Project class from Using DataContractSerializer to serialize, but can't deserialize back to check whether you will get the same issue.

        public static string Serialize(object obj) {
            using(MemoryStream memoryStream = new MemoryStream())
            using(StreamReader reader = new StreamReader(memoryStream)) {
                DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
                serializer.WriteObject(memoryStream, obj);
                memoryStream.Position = 0;
                return reader.ReadToEnd();
            }
        }
    
        public static object Deserialize(string xml, Type toType) {
            using(Stream stream = new MemoryStream()) {
                byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
                stream.Write(data, 0, data.Length);
                stream.Position = 0;
                DataContractSerializer deserializer = new DataContractSerializer(toType);
                return deserializer.ReadObject(stream);
            }
        }

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, September 19, 2017 2:57 AM
  • Hi Edward!

    Thank you for your help. We wanted to try to test your code, but as it seems the project is "fixed" now because the owner of the project continued working, and got it into a working state it seems.

    (why could he continue working: we removed some connections within the classes on the server, and just mapped them on the client, which enabled serialization again)

    So the question is: what did the user do? He basically did 2 things: There is a n-to-n mapping table between 2 classes (deliverable and workproduct), he deleted some mappings and create(/modified) some.

    The second thing is, that some link has been copy pasted into the deliverable (it is saved as an rtf string there).

    Do you see any possible problem there? Any mistake we could have overlooked?

    And the second question: Is it possible, that whenever the serialization works, it is just some kind of luck, that the isReferenceArray is just big enough and that we are missing something essential?

    Greets,

    Sam

    • Edited by Qerildan Tuesday, September 19, 2017 10:08 AM
    Tuesday, September 19, 2017 8:49 AM
  • Hi Sam,

    >> but as it seems the project is "fixed" now because the owner of the project continued working, and got it into a working state it seems.

    I am glad your code works now, and I would suggest you mark your reply as answer to close this thread.

    >> why could he continue working

    If you are interested on the root cause of this issue, I would suggest you post a new thread, and share us a simple project to reproduce your issue.

    >> Is it possible, that whenever the serialization works, it is just some kind of luck, that the isReferenceArray is just big enough and that we are missing something essential?

    Based on your description, it seems to be related with n-to-n mapping table, but I would suggest you post a new thread and share us a simple demo, and then we could work closely with you.

    Best Regards,

    Edward


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Qerildan Wednesday, September 20, 2017 5:35 AM
    Wednesday, September 20, 2017 2:19 AM
  • Hello Edward,

    thank you for your support and suggestions! We will try to get a small project out of this, and we will try to reproduce it, but that seems to be hard, since it only seems to occure on some data-dependent states.We will create a new post, if we are able to reproduce it, so that we can  closely exame the issue together.

    Thanks again!

    Greets Sam

    Wednesday, September 20, 2017 5:35 AM
  • Hello Edward!

    As the issue occures again, I have created a new topic[1]. Maybe you can have a look at it.

    Greets,

    Sam

    [1] https: //social.msdn.microsoft.com/Forums/vstudio/en-US/2409dd4d-d339-4e39-9694-d37ad3e7eb00/datacontractserializer-ensuresetasisreference?forum=wcf

    Monday, October 23, 2017 6:59 AM