MrKanin
MrKanin

Reputation: 29

WCF client side List<>

I got a WCF service with a method (GetUserSoftware)to send a List to a client.

the software I have defined like this:

[DataContract]
public class Software
{
    public string SoftwareID { get; set; }
    public string SoftwareName { get; set; }
    public string DownloadPath { get; set; }
    public int PackageID { get; set; }

}

the method is going through my db to get all software availeble to the clien, and generates a list of that to send back to the client.

problem is i on the client side the list is turned into an array. and every item in that array dont contain any of my software attributs.

i have debugged my way through the server side. and seen that the list its about to send is correct. with the expected software and attributs in it.

any one know how to work around this or know what i can do ?

Upvotes: 3

Views: 672

Answers (5)

jakaria manik
jakaria manik

Reputation: 1

I was suffering with same problem and now I solved it! It was a ServiceKnownType problem. If you have a in known type loader we have to add runtime Type like;

Type aaa =  Type.GetType("System.Collections.Generic.List`1[[ProjectName.BusinessObjects.Bank, ProjectName.BusinessObjects, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null]]");

knownTypes.Add(aaa);

Anyone having same problem can try this. It's working in my environment!

Upvotes: 0

Felice Pollano
Felice Pollano

Reputation: 33272

You can mantain List instead of array on the clien when you add the Service Reference: click the "advanced" button and change the collection type to the one you want.

Upvotes: 0

CodingWithSpike
CodingWithSpike

Reputation: 43738

First off, each of the properties that you want to serialize should have the [DataMember] attribute:

[DataContract]
public class Software
{
    [DataMember]
    public string SoftwareID { get; set; }
    [DataMember]
    public string SoftwareName { get; set; }
    [DataMember]
    public string DownloadPath { get; set; }
    [DataMember]
    public int PackageID { get; set; }    
}

Second, the translation to an Array would be handled by the client, not the server.

Upvotes: 1

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364369

When you use DataContract attribute for a type you have to use DataMember attribute for each property or field you want to serialize and transfer between service and client. Collections are by default created as arrays. If you don't like it you can change this behavior in Add Service Reference window -> Advanced settings where you can select which collection type should be used.

Upvotes: 4

Aliostad
Aliostad

Reputation: 81690

Did you forget [DataMemeber] attribute on your properties?

Upvotes: 4

Related Questions