Fairly new to WCF, DataContracts and such here's is my first ever question on SO.
The problem: I want to send an xml request to my WCF service. The request has the following layout;
<DataContractName>
<Other Stuff>
<Blah></Blah>
</Other Stuff>
<IdList>
<Id>1</Id>
<Id>2</Id>
<IdList>
</DataContractName>
[DataContract(Name="DataContractName")]
public sealed class Request
{
[DataMember]
public List<string> IdList :get;set;}
}
Running this request ends up ending with an empty list and thus not yielding the result I expected. Browsing the net and ending up mostly on Stackoverflow I found below links. However none of them seemed to have the right response for me.
How to send list of objects to WCF service?
how to pass List<Object> to WCF
Passing List<T> as parameter to WCF service operation
When I use the following lay-out, the request does work as expected.
does work
<DataContractName>
<Other Stuff>
<Blah></Blah>
</Other Stuff>
<IdList xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<a:string>1</a:string>
<a:string>2</a:string>
<IdList>
</DataContractName>
Obviously I want to be able to get rid of the namespace and string annotation for my list elements. Based on the information found in below links, it must be possible without the namespace annotation.
REST-ful WCF service: Deserializing C# List of Strings not working properly --> https://msdn.microsoft.com/en-us/library/aa347850.aspx
However I am not able to make it work.
What I have done: Created a new CollectionDataContract;
[CollectionDataContract]
public sealed class IdsList : Collection<string>
{
}
and use it within my existing contract
[DataContract(Name="DataContractName")]
public sealed class Request
{
[DataMember]
public IdsList IdList {get;set;}
}
However when I run this, the result is not what I expect (empty list again).
So what can I do to create a valid request that my service will understand?
OT: Why not just a simple <code> tag?
I've tested your code here, and I just add some more data to the attributes and worked fine.
First, in the interface I declare a namespace:
[DataContract(Namespace = "http://customnamespace")]
public class Result
{
[DataMember]
public string Bla { get; set; }
[DataMember]
public IdsList List { get; set; }
}
Secound, in the collection class, I also declared namespace (i've used the same of interface) and itemname:
[CollectionDataContract(ItemName = "List", Namespace = "http://customnamespace")]
public class IdsList : List<string>
{
}
The returned xml does not have the namespace declaration in the collection member. Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With