WCF – Generic List is converted to Array

As I discussed in my previous blogs I am developing a small tool for transferring files between computers in LAN. I decided to use WCF as my main technology in this application. During my developing I discovered that a data member whose type is a generic list, was changed when WCF service is exposed to client. The type was changed from a list to an array.

We pay attention to the data member “Files” whose type is a List of FileObject

    [DataContract]
    public class TransferObject
    {
        [DataMember]
        public QueryTypes QueryType { get; set; } // The action of the packet
        [DataMember]
        public string Name { get; set; } //Client name
        [DataMember]
        public string Id { get; set; } //Identification
        [DataMember]
        public List<FileObject> Files { get; set; } //the list of the file

        public TransferObject()
        {
            Files = new List<FileObject>();
        }

        public TransferObject(QueryTypes qtType) : this()
        {
            QueryType = qtType;
        }
    }

And this is the generated client code. We can recognize immediately that the type was changed to an array of FileObject.

 [System.Runtime.Serialization.DataMemberAttribute()]
        public Files_Over_LAN.FileServiceReference.FileObject[] Files {
            get {
                return this.FilesField;
            }
            set {
                if ((object.ReferenceEquals(this.FilesField, value) != true)) {
                    this.FilesField = value;
                    this.RaisePropertyChanged("Files");
                }
            }
        }

The reason for this converting is that “WCF Service are platform-agnostic, they need to use the most primitive data types“. Please consider two scenario below:
Scenario 1 :
What would happen if your web service would be consumed by a .NET 1.x application, where no generics are available?
Scenario 2 :
How would non .Net applications (J2ee or PHP) will consume the service, which doenst have the knowledge of .Net Generics?
The convertion happens because array is the lowest common denominator.

And what we should do if we still want to use generic list. It’s pretty simple. Right click on your service reference, choose “Configure Service Reference”.

At the combo box of collection type, we choose Generic List instead of Array

Now the client code will be exposed correctly as we expect.