views:

21

answers:

2

I'm using Visual Studio 2010, and I've got a service reference to a web service we created. Our methods return objects that contain generic List properties:

public class ExampleResponse
{
  private System.Collections.Generic.List<int> intValues;

  [WCF::MessageBodyMember(Name = "IntValues")]
  public System.Collections.Generic.List<int> IntValues    
  {
    get { return intValues; }
    set { intValues= value; }
  }
}

On the client-side, it creates a References.cs file with int[] instead of List:

[System.ServiceModel.MessageBodyMemberAttribute(Namespace="SomeNamespace", Order=0)]
[System.Xml.Serialization.XmlArrayAttribute(IsNullable=true)]
[System.Xml.Serialization.XmlArrayItemAttribute(Namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays", IsNullable=false)]
public int[] IntValues;

On the service reference settings the Collection Type is set to use List, not Arrays. Yet, it's still doing so.

Any info on how to solve this would be extremely helpful, it seems to make no sense.

A: 

In the Add Service Reference, you can choose which type to use for Collections. For some reason Array is default. After changing it, I've had to delete the entire reference and re-add it, choosing List from the beginning. I've had weird issues changing it after the fact.

Nate Bross
I've done exactly this, by setting the Collection value before it even can create the reference itself. But I'm still getting Arrays.
Grandpappy
+1  A: 

Did you add a "Service Reference" or a "Web Reference"? It appears that the proxy was generated with the XmlSerializer instead of the DataContractSerializer. If the DataContractSerializer was used, you would have System.Runtime.Serialization... Attributes instead of the Xml.Serialization... attributes. How exactly did you generate this web reference? The updated XmlSerializer will convert all collections to Arrays, where as, the Datacontract serializer knows how to generate .Net DataTypes. Add Web Reference uses the XmlSerializer BTW.

Also, I'm curious about your use of MessageBodyMember. Why are you trying to generate your own MessageContracts. Messing with MessageContracts can be very dangerous, especially if you don't know exactly what you are doing.

Instead, try the following:

[DataContract]
public class ExampleResponse
{
    private System.Collections.Generic.List<int> intValues;

    [DataMember]
    public System.Collections.Generic.List<int> IntValues
    {
        get { return intValues; }
        set { intValues = value; }
    }
}

See how that works for you and let us know.

CkH
The reference was created by right clicking on the project and selecting "Add Service Reference...". When the dialog came up to find the web service, I don't choose the Web Reference option at the bottom. I'll check on the other suggestion you had, but this had been working until recently - it's from a web service that we "inherited".
Grandpappy
Okay, got it. Your comment sent me down a path I had not thought of before. We had an enum that didn't have a DataContract attribute on it, so VS2010 was creating a WebReference without telling me.I also understand what you say about handling our own message contracts, it's ugly.
Grandpappy
Glad I could help. Happy Coding
CkH