locked
Is List<T> is serializable by default when T is serializable? RRS feed

  • Question

  • User-758219241 posted

    there is a data contract (say, `EmployeeView`) in my WCF service. I have decorated it with `Serializable` attribute, and all members are marked as `DataMember`
    A method in the WCF is returning `List<EmployeeView>`.
    When I execute this method through WCF Test client or MVC app, it gets executed successfully, but while transferring result it is giving an error of `The underlying connection was closed: The connection was closed unexpectedly`. Is `List<EmployeeView>` not serialized though `EmployeeView` is marked as serialized?

    Tuesday, January 22, 2013 1:56 AM

Answers

  • User-837620913 posted

    WCF serializes Lists<T> as arrays, so that should work. Make sure your DataContracts are setup correctly, like this:

    [DataContract]
    public class EmployeeList
    {
        [DataMember]
        public int Count { get; set; }
    
        [DataMember]
        public List<EmployeeView> ListOfEmployees
        {
            { get; set; }
        }
    }
    
    [DataContract]
    public class EmployeeView
    {
        [DataMember]
        public string EmployeeName { get; set; }
    
        // more properties
    }
    

    You can run into problems if you are using inheritance. If that is an issue, use the KnownTypes attribute. See this article for details: http://msdn.microsoft.com/en-us/magazine/gg598929.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 22, 2013 5:36 AM

All replies

  • User-837620913 posted

    WCF serializes Lists<T> as arrays, so that should work. Make sure your DataContracts are setup correctly, like this:

    [DataContract]
    public class EmployeeList
    {
        [DataMember]
        public int Count { get; set; }
    
        [DataMember]
        public List<EmployeeView> ListOfEmployees
        {
            { get; set; }
        }
    }
    
    [DataContract]
    public class EmployeeView
    {
        [DataMember]
        public string EmployeeName { get; set; }
    
        // more properties
    }
    

    You can run into problems if you are using inheritance. If that is an issue, use the KnownTypes attribute. See this article for details: http://msdn.microsoft.com/en-us/magazine/gg598929.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 22, 2013 5:36 AM
  • User-758219241 posted

    Hi Darrel,

    Thanks for the link you provided, and I came across something which will be very much helpful for me.

    however, I have tried adding "knowntype", but it didn't work.

    Also, i tried removing inheritance in data contract and that too didn't work.

    whatever attempt I do I get same error message saying, "The underlying connection was closed: The connection was closed unexpectedly."

    I am clueless on this to be honest 

    Tuesday, January 22, 2013 1:27 PM
  • User-1000095884 posted

    whatever attempt I do I get same error message saying, "The underlying connection was closed: The connection was closed unexpectedly."

    To troubleshoot the issue, please enable tracing for your service and inspect the logs to help to dignose the problem.

    http://msdn.microsoft.com/en-us/library/ms733025.aspx

    Best Regards.

    Wednesday, January 23, 2013 4:53 AM