WCF converts List<string> to string[]

J

John

I'm having a problem passing List<T> from a service. I don't know if the
problem is at the service or the client. For some reason a List is getting
converted to an array. Here are the details:

I have a simple type called SimpleType that contains a List:

[DataContract]
public class SimpleType
{
[DataMember]
public List<string> Strings
{
get { return strings; }
set { strings = value; }
}
private List<string> strings;
}

The service code is equally simple:

public SimpleType GetSimpleType()
{
return new SimpleType();
}

The client creates the following code for SimpleType's List:

[System.Runtime.Serialization.DataMemberAttribute()]
public string[] Strings
{
get
{
return this.StringsField;
}
set
{
this.StringsField = value;
}
}

Is there something I need to do to have a List created at the client rather
than an array? Is there a property I forgot to set somewhere?

Thanks,
John
 

My Computer

C

Chris Mullins

"John" <[email protected]> wrote
> I'm having a problem passing List<T> from a service.


> [DataMember]
> public List<string> Strings
> {
> get { return strings; }
> set { strings = value; }
> }


If nothing else, this code won't pass any of the Static Code Analysis tools.
You should be exposing the IList<> interface, or one of the
System.Collections.ObjectModel collections. Requiring things via your
interface (especially one exposed on the web) to be an actual List<> class
is bad form.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise
http://www.coversant.net/blogs/cmullins
 

My Computer

J

John

Thank you for the critique, Chris. This was only a simple example to
demonstrate the problem I'm having. But I welcome any constructive advice
you can provide. If you can recommend some good material on the subject I
would appreciate it. But now back to my problem...

I've tried List<>, IList, ArrayList and even
System.Collections.ObjectModel.Collection. In all cases the client builds a
proxy containing a simple array. I would like it to create something more
useful than an array. Is that possible?

John


"Chris Mullins" <[email protected]> wrote in message
news:[email protected]
> "John" <[email protected]> wrote
>> I'm having a problem passing List<T> from a service.

>
>> [DataMember]
>> public List<string> Strings
>> {
>> get { return strings; }
>> set { strings = value; }
>> }

>
> If nothing else, this code won't pass any of the Static Code Analysis
> tools. You should be exposing the IList<> interface, or one of the
> System.Collections.ObjectModel collections. Requiring things via your
> interface (especially one exposed on the web) to be an actual List<> class
> is bad form.
>
> --
> Chris Mullins, MCSD.NET, MCPD:Enterprise
> http://www.coversant.net/blogs/cmullins
>
>
 

My Computer

Top