WCF返回ArrayOfKeyValueOfintstringKeyValueOfintstring []而不是Dictionary< int,string>和List的Array instread [英] WCF returns ArrayOfKeyValueOfintstringKeyValueOfintstring[] instead of Dictionary<int, string> and Array instread of List

查看:237
本文介绍了WCF返回ArrayOfKeyValueOfintstringKeyValueOfintstring []而不是Dictionary< int,string>和List的Array instread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我花了最后几个小时调查这个问题,很明显,我不是唯一的一个。为什么我的字典和列表返回为数组?



我理解为了兼容性,我们使用Arrays作为默认值。 WCF做一个有意识的努力远离.Net依赖。但是我的服务器和客户端都是用C#.Net开发的,所以我没关系。



这里是一个只针对StackOverflow的类似问题的例子:


  1. WCF服务返回数组而不是List

  2. 期望?

  3. WCF服务返回字典数组

  4. 相反, WCF代理返回数组of List EVEN THOUGH Collection
    Type == Generic.List

  5. WCF返回数组而不是列表EVEN THOUGH集合类型==
    Generic.List

  6. 为什么我的WCF服务返回和ARRAY而不是列表?

  7. 数组而不是WCF服务代理中的列表使用
    生成的svcutil.exe

我设置的内容:



我通过此命令生成代理:

  C:\Program Files(x86)\Microsoft SDKs\Windows\v7.0A\Bin> svcutil.exe / language:cs 
/out:generatedProxy.cs /config:app.config /ct:System.Collections.Generic.List`1
http://192.168.0.99:9000/ProjectDatabase/ ??

我的服务合约如下所示:

  [ServiceContract] 
public interface IMyContract
{
[OperationContract]
[ServiceKnownType(typeof(Dictionary< int,string>))
字典< int,string> getClassDictionary();
}



我的实现:

  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;
}

即使在我的 Reference.svcmap 我有:

 < CollectionMappings> 
< CollectionMapping TypeName =System.Collections.Generic.List`1Category =List/>
< / CollectionMappings>然而,尽管我尽了最大努力和研究,我仍然得到:









b $ b


字典<'int,string'> returns as ArrayOfKeyValueOfintstringKeyValueOfintstring []



列表<'T'>以T []返回


我觉得我已经尝试了一切,做了一切正确,但我必须缺少的东西。那么是什么呢?感谢您的时间,帮助和考虑。



更新:



我甚至尝试了绕过 Array 拼版,通过编写 serializable struct 到数组。

  [Serializable] 
public struct KeyValuePair< K,V&
{
public K Key {get;组; }
public V Value {get;组; }
}

但是,当我返回 KeyValuePair< int ,string> [] 。我的代理生成 KeyValuePairOfintstring [] 的返回。



解决方案如下。 >

解决方案

我发现导致序列化的原因是什么。



在我的 ServiceContract 我有以下:

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

注释掉这个并重新编译我的WCF服务库后,我发现一切序列化/反序列化代理。



看来当 svcutil.exe 遇到一些不知如何序列化的东西, 。毫无疑问,它会忽略你的命令/设置,并序列化一切像乱七八糟的像 ArrayOfKeyValueOfinttringKeyValueOfintstring 。所以如果你收到这个错误,你应该问自己,如果 svcutil.exe 能够正确地序列化所有你返回的。



我希望我的问题的来源的识别将在未来帮助别人。


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:

I am generating the Proxy via this command:

 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin>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.

解决方案

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.

这篇关于WCF返回ArrayOfKeyValueOfintstringKeyValueOfintstring []而不是Dictionary&lt; int,string&gt;和List的Array instread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆