So I've spent the last several hours investigating this issue and it is clear that I am not the only one. Why are my Dictionaries and Lists being returned as Arrays?
I understand that Arrays are used as default for the sake of compatibility. WCF makes a conscious effort to distance itself from being .Net dependent. But both my Server and Client are developed in C# .Net so I'm okay.
Here is a sampling of similar questions on just StackOverflow alone:
- WCF service returning array instead of List
- Why does WCF return myObject[] instead of List like I was
expecting?
- WCF service returning an array of dictionary
- WCF Proxy Returning Array instead of List EVEN THOUGH Collection
Type == Generic.List
- WCF Returning Array instead of List EVEN THOUGH Collection Type ==
Generic.List
- Why does my WCF service return and ARRAY instead of a List ?
- Array instead of List in WCF Service Proxy Generated using
svcutil.exe
What I have set up:
I am generating the Proxy via this command:
C:Program Files (x86)Microsoft SDKsWindowsv7.0ABin>svcutil.exe /language:cs
/out:generatedProxy.cs /config:app.config /ct:System.Collections.Generic.List`1
http://192.168.0.99:9000/ProjectDatabase/??
My service contract looks like this:
[ServiceContract]
public interface IMyContract
{
[OperationContract]
[ServiceKnownType(typeof(Dictionary<int, string>))]
Dictionary<int, string> getClassDictionary();
}
My implementation:
public Dictionary <int, string> getClassDictionary()
{
Dictionary<int, string> myDict = new Dictionary<int, string>();
myDict.Add(1, "Geometry");
myDict.Add(2, "Algebra");
myDict.Add(3, "Graph Theory");
return myDict;
}
Even in my Reference.svcmap
I have:
<CollectionMappings>
<CollectionMapping TypeName="System.Collections.Generic.List`1" Category="List" />
</CollectionMappings>
However, despite my best efforts and research I still get:
Dictionary<'int, string'> returning as ArrayOfKeyValueOfintstringKeyValueOfintstring[]
And:
List<'T'> returning as T[]
I feel like I've tried everything and done everything right, but I have to be missing something. So what is it? Thank you for your time, help, and consideration.
Update:
I've even attempted a route of working around the Array
imposition, by writing a serializable
struct
and adding them to an array.
[Serializable]
public struct KeyValuePair<K, V>
{
public K Key { get; set; }
public V Value { get; set; }
}
However, when I return the KeyValuePair<int, string>[]
. My proxy is generating a return of KeyValuePairOfintstring[]
.
Solution is posted below.
See Question&Answers more detail:
os