Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

c# - WCF returns ArrayOfKeyValueOfintstringKeyValueOfintstring[] instead of Dictionary<int, string> and Array instread of List

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:

  1. WCF service returning array instead of List
  2. Why does WCF return myObject[] instead of List like I was expecting?
  3. WCF service returning an array of dictionary
  4. WCF Proxy Returning Array instead of List EVEN THOUGH Collection Type == Generic.List
  5. WCF Returning Array instead of List EVEN THOUGH Collection Type == Generic.List
  6. Why does my WCF service return and ARRAY instead of a List ?
  7. Array instead of List in WCF Service Proxy Generated using svcutil.exe

What I have set up: Service Reference Configuration

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well I found out what was causing the serialization to be so crude.

In my ServiceContract I had the following:

[OperationContract]
List<DataTable> ShowTables();
[OperationContract]
DataTable FetchContacts(string filter = "All");
[OperationContract]
DataTable FetchUsers();
[OperationContract]
DataTable FetchDrops();

After commenting this out and recompiling my WCF Service Library, I found that everything serialized/ deserialized appropriately when generating the proxy.

It seems that when svcutil.exe encounters something that it does not know how to serialize then all bets are off. Quite literally it will ignore your commands/settings and serialize everything as gibberish like ArrayOfKeyValueOfinttringKeyValueOfintstring. So if you receive this error, you should ask yourself if svcutil.exe is able to properly serialize all of what you are returning.

I hope the identification of the source of my issue will help others in the future.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...