WCF代理返回数组而不是列表甚至收集类型== Generic.List

时间:2012-11-22 12:44:05

标签: wcf tfs visual-studio-2012 tfs2010 wcf-client

我有一个包含WCF库项目的VS 2010解决方案和另一个使用该Web服务的项目。在VS 2012开放后,它已升级。

代理现在返回List< T>类型为数组,即使CollectionMappings明确设置为Generic.List。

可能会发生什么?

其他人有类似的问题here,但他正在从VS 2012降级到VS 2010。

编辑:我仔细检查过,Reference.svcmap包含:

<CollectionMappings>
  <CollectionMapping TypeName="System.Collections.Generic.List`1" Category="List" />
</CollectionMappings>

但Reference.cs包含诸如以下内容:

 public xxx.ServiceReference1.ADUser[] get_ADUsers;

在Web服务中它是:

 public List<ADUser> get_ADUsers(string wildcard, string sortexp, string sortorder)

更多信息(2012年12月12日添加):

在VS2010中创建的解决方案在另一台PC上运行良好。它从该PC检入TFS。在这个有问题的PC上,我们进行了映射和GET。当我们尝试构建时,我们得到了所有List&lt; T&gt;的错误。服务引用中使用的类型都以某种方式被视为数组。我们在有问题的PC上安装了VS 2010并获得了该解决方案。同样的错误也存在。因此,它似乎与VS 2012无关。

所有PC均为Windows 7 Professional。

更多信息(2012年12月19日添加):

项目打开后,本地PC上的ServiceReferences / ServiceReference1 / Reference.cs会自动修改。变化是巨大的。以下是其中的一小部分:

enter image description here

显示了两种方法。列表与LT;串GT; get_Hotlines()成为string [] get_Hotlines()和List&lt; string&gt; get_HotlinesBySite()变为string [] get_HotlinesBySite()。

为什么即使没有我的询问,文件也会改变? VS 2012升级日志表示两个文件已更改,但Reference.cs不是其中之一。

3 个答案:

答案 0 :(得分:5)

当您添加对wcf服务的引用时,您需要将Collection Type更改为Generic List:

enter image description here

您也可以更新此设置,只需在解决方案中选择服务参考,右键单击并选择“配置服务参考...”

答案 1 :(得分:0)

对我来说是什么:

  1. 安装Visual Studio 2013的所有更新
  2. 取消选中“在引用的程序集中重用类型”

答案 2 :(得分:0)

我和VS2010有同样的问题。经过几个小时的尝试和测试,我认为这是一个代码生成的错误。我能够重现它。这是服务器端的代码,它会影响Visual Studio代码生成的行为。 注释行在我的情况下产生了这个问题。当此成员不可为空时,Visual Studio将生成数组集合而不是List&lt;&gt;。

[DataContract]
public enum DocumentAttachmentSourceType
{
    [EnumMember]
    ServiceMission                              
}

[MessageContract]
public class DocumentUploadRequest : IDisposable
{
    [MessageHeader]
    public long NodeId { get; set; }

    [MessageHeader]
    public DocumentAttachmentSourceType? AttachmentSource { get; set; } //This works
    //public DocumentAttachmentSourceType  AttachmentSource { get; set; }   //This not works !!!!!!!

    [MessageBodyMember]
    public System.IO.Stream Stream { get; set; }


    public void Dispose()
    {
        if (Stream != null)
        {
            Stream.Close();
            Stream = null;
        }
    } 

}