【问题标题】:WCF: generic list serialized to arrayWCF:序列化为数组的通用列表
【发布时间】:2011-09-26 01:51:24
【问题描述】:

所以我正在使用 WCF,我的服务返回包含通用列表的类型。 WCF 目前正在通过网络将这些转换为数组。有没有办法配置 WCF 之后将它们转换回列表?我知道在添加服务引用时单击高级有一种方法,但我正在寻找配置文件或类似文件中的解决方案。

[DataContract(IsReference = true)]
public class SampleObject
{
  [DataMember]
  public long ID { get; private set; }

  [DataMember]
  public ICollection<AnotherObject> Objects { get; set; }
}

这也很奇怪,因为一个服务将它作为列表返回,另一个作为数组返回,我很确定它们的配置相同。

【问题讨论】:

    标签: c# arrays wcf list serialization


    【解决方案1】:

    在添加服务参考时的高级选项卡中,您也可以设置此选项。设置了标准数组。

    【讨论】:

    • “我知道添加服务引用时点击高级有一种方法,但我正在寻找配置文件或类似文件中的解决方案。”
    【解决方案2】:

    我认为这与客户端工具从 WSDL 生成合同的方式完全不同。就我而言,我制作了一个可重用的 .dll,其中包含我的 [OperationContract] 和 [DataContract] 类,并在客户端和服务器上都使用它,而不是使用 SvcUtil 生成一个。这样我就保留了我的泛型列表。

    【讨论】:

      【解决方案3】:

      此外,注意不要在使用 WCF 序列化实例的类中同时包含数组和泛型,因为在反序列化过程中会遇到问题:所有内容都将转换为 ArrayOf(如果你不更改配置)或集合类型。

      因此,您将在 WCF 代码的反序列化过程中遇到错误,该代码试图分配一个等待集合的数组,反之亦然。

      这只是我在 WCF 的一个小项目中学到的 2cent 建议。 :)

      【讨论】:

        【解决方案4】:

        我找到了一个更简单且对我来说效果很好的解决方案,尽管它可能不适用于其他人。我只是从使用 ICollection(IList 也产生此结果)切换到 List。之后效果很好。

        来自Here的解决方案。

        我还从底部附近的Here 找到了一个可能的配置解决方案。

        <CollectionMappings>
            <CollectionMapping TypeName="ChangeTracker.ChangeTrackingCollection'1" Category="List" />
        </CollectionMappings>
        

        【讨论】:

          【解决方案5】:

          而不是在您的数据合同中使用ICollection&lt;AnotherObject&gt;,它将在客户端应用程序中生成为AnotherObject[]

          试试这个:

          定义一个新的数据契约

          [CollectionDataContract]
          public class AnotherObjectCollection : List<AnotherObject> {}
          

          在您的代码中:

          DataContract(IsReference = true)]
          public class SampleObject
          {
            [DataMember]
            public long ID { get; private set; }
          
            [DataMember]
            public AnotherObjectCollection Objects { get; set; }
          }
          

          在 Visual Studio(与 svcUtil 相同)中,客户端代理代码将如下所示:

          [System.Diagnostics.DebuggerStepThroughAttribute()]
          [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
          [System.Runtime.Serialization.CollectionDataContractAttribute(Name="AnotherObjectCollection", Namespace="http://schemas.datacontract.org/2004/07/SampleObject", ItemName="AnotherObject")]
          [System.SerializableAttribute()]
          public class AnotherObjectCollection : System.Collections.Generic.List<AnotherObject> {}
          
           DataContract(IsReference = true)]
              public class SampleObject
              {
                [DataMember]
                public long ID { get; private set; }
          
                [DataMember]
                public AnotherObjectCollection Objects { get; set; }
              }
          

          这也适用于内置的 .NET 类型。

          安东尼奥

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-01-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-08-20
            • 2016-12-29
            • 1970-01-01
            相关资源
            最近更新 更多