none
Why does this data contract not want to serialize? RRS feed

  • Question

  • Hi all, I keep getting this "

    cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute."

    on this class, I'm probably too close to the issue, can you guys/gals help me out...?

        [DataContract]
        public class PagedDataRequest
        {
            [DataMember]
            public string[] Filters { get; set; }
            [DataMember]
            public string Sort { get; set; } 
            [DataMember]
            public int PageSize { get; set; }
            [DataMember]
            public int PageIndex { get; set; }
            [DataMember]
            public int RowLimit { get; set; }
        }
    
     [DataContract]
    public class ListOrdersRequest : PagedDataRequest
        {
            [DataMember]
            public string code{ get; set; }
    
            [DataMember]
            public string status {get;set;}
    }

    I had heard that inheritance in WCF is "not cool" and that I should be using datacontractresolver(s) of some kind... but I'm not...

    I had tried ditching the base class but still gets the same issue... I also tried ditching the "string[]"... same deal...

    Any thoughts?

    UPDATE: Hmmmm... okay this was the WRONG question to ask, because there is nothing (technically) wrong with the code I posted...

    I put this into another project and it works as desired...

    So now the question becomes more complex... thoughts and suggestions about how to figure out what in my WCF app is causing greif, and delivering at runtime this misleading message...!?




    • Edited by noJedi Friday, August 29, 2014 7:22 AM
    Friday, August 29, 2014 1:04 AM

Answers

  • Hi,

    the "base" class is used in variuos other contracts for requesting (and another paged Response for returning data from one of these requests), I have just not shown this in this post.

    I think you, Amy, have some good thoughts...

    Currently I'm thinking that perhaps I have a clash happening somewhere that is trying to do something automatically that is overriding my explicit Attributes... being set, but I do have a bug in (an unrelated module) that isn't compiling at the moment... so perhaps its a compilation/caching issues... I'll look into that pathway now...

    --UPDATE/SOLUTION (for ONE situation in my instance - I still have another class that is doing this, that has no complex types... wondering if something in the message header is causing this...):

    Turns out that in the erroneous instances there was a "deeper" contract (basically a complex type as a property of the message)... Haven't validated this but here is what I think was happening;

    ProjectBroken:

    ClassA has a ClassB
    ClassA is annotated with "DataContract/DataMember", ClassB is not...

    ProjectWorking

    ClassA has a ClassB
    Neither class is annotated with "DataContract/DataMember"

    because this is not compulsory in WCF3.5+... (I think) then it works

    Project Broken complains (my theory) because once you start using annotations then you MUST continue to do so...

    My confusion was caused by much stuffing around with many contracts (data and message) flooding my brain and therefore not keeping clear view on what did/didn't have the appropriate annotations, but also the "misleading" error telling me that MESSAGE CONTRACT (ClassA) was missing annotations (which it wasn't) when in actual fact it was the property (effectively ClassB) that was the problem...

    Red herring anyone? Sigh...


    • Marked as answer by noJedi Monday, September 22, 2014 8:18 AM
    • Unmarked as answer by noJedi Monday, September 22, 2014 8:29 AM
    • Edited by noJedi Monday, September 22, 2014 8:30 AM
    • Marked as answer by noJedi Wednesday, September 24, 2014 3:28 AM
    Tuesday, September 2, 2014 3:59 AM
  • While the above seemed to solve my issue mostly I still had a problem... after much head scratching and gnashing of teeth, I realised that there was another class (created accidentally by refactoring tools apparently... grr...) called DataContract that inherited from Attribute... this is what was causing the second part of my issue...

    Sigh... thanks for the assistance/patience!

    :)

    • Marked as answer by noJedi Wednesday, September 24, 2014 3:28 AM
    Wednesday, September 24, 2014 3:27 AM

All replies

  • Hi,

    You use this:
    ListOrdersRequest : PagedDataRequest
    But you do not use it. So why use this?

    Friday, August 29, 2014 12:05 PM
  • Hi nojedi,

    Based on your description, I know that when you put this into another project, it works.
    Then it seems that you want to to know why this error information appears in your original project, if so, then it may be caused by that you have forgotten to re-build your project after doing some update. One of the solution is to handle this kind of the exception in the WCF Service to help find the root cause.
    For more information, please try to refer to the following articles:
    #Best Ways For WCF Exception Handling:
    http://www.c-sharpcorner.com/UploadFile/aravindbenator/wcf-exception-handling-best-ways/ .

    #Fault Handling in WCF Services:
    http://msdn.microsoft.com/en-us/library/ee942778.aspx .

    #WCF Exception Handling:
    http://www.codeproject.com/Articles/636843/WCF-Exception-Handling .


    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, September 1, 2014 3:04 AM
    Moderator
  • Hi,

    the "base" class is used in variuos other contracts for requesting (and another paged Response for returning data from one of these requests), I have just not shown this in this post.

    I think you, Amy, have some good thoughts...

    Currently I'm thinking that perhaps I have a clash happening somewhere that is trying to do something automatically that is overriding my explicit Attributes... being set, but I do have a bug in (an unrelated module) that isn't compiling at the moment... so perhaps its a compilation/caching issues... I'll look into that pathway now...

    --UPDATE/SOLUTION (for ONE situation in my instance - I still have another class that is doing this, that has no complex types... wondering if something in the message header is causing this...):

    Turns out that in the erroneous instances there was a "deeper" contract (basically a complex type as a property of the message)... Haven't validated this but here is what I think was happening;

    ProjectBroken:

    ClassA has a ClassB
    ClassA is annotated with "DataContract/DataMember", ClassB is not...

    ProjectWorking

    ClassA has a ClassB
    Neither class is annotated with "DataContract/DataMember"

    because this is not compulsory in WCF3.5+... (I think) then it works

    Project Broken complains (my theory) because once you start using annotations then you MUST continue to do so...

    My confusion was caused by much stuffing around with many contracts (data and message) flooding my brain and therefore not keeping clear view on what did/didn't have the appropriate annotations, but also the "misleading" error telling me that MESSAGE CONTRACT (ClassA) was missing annotations (which it wasn't) when in actual fact it was the property (effectively ClassB) that was the problem...

    Red herring anyone? Sigh...


    • Marked as answer by noJedi Monday, September 22, 2014 8:18 AM
    • Unmarked as answer by noJedi Monday, September 22, 2014 8:29 AM
    • Edited by noJedi Monday, September 22, 2014 8:30 AM
    • Marked as answer by noJedi Wednesday, September 24, 2014 3:28 AM
    Tuesday, September 2, 2014 3:59 AM
  • While the above seemed to solve my issue mostly I still had a problem... after much head scratching and gnashing of teeth, I realised that there was another class (created accidentally by refactoring tools apparently... grr...) called DataContract that inherited from Attribute... this is what was causing the second part of my issue...

    Sigh... thanks for the assistance/patience!

    :)

    • Marked as answer by noJedi Wednesday, September 24, 2014 3:28 AM
    Wednesday, September 24, 2014 3:27 AM