【问题标题】:having trouble returning this type in A Asynchronous WCF Service Call From Silverlight在 Silverlight 的异步 WCF 服务调用中返回此类型时遇到问题
【发布时间】:2010-08-09 21:12:15
【问题描述】:

我有一个 WCF 服务。
它返回以下类型。我得到第一级的数据,但没有嵌套列表中的任何数据......我的问题可能是什么?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace slCF2.Web
{
    public class Customer
    {
        string _firstname;
        string _lastname;              
        List<BO> _bos;
        List<AO> _aos;



        public string FirstName
        {
            get { return _firstname; }
            set { _firstname = value; }
        }

        public string LastName
        {
            get { return _lastname; }
            set { _lastname = value; }
        }

        public System.Collections.Generic.List<AvailableOption> AvailableOptions
        {
            get { return _availableoptions; }
            set { _availableoptions = value; }
        }

        public System.Collections.Generic.List<BuiltOption> BuiltOptions
        {
            get { return _builtoptions; }
            set { _builtoptions = value; }
        }

    }
    [Serializable]
    public class AO
    {
        string _code;

        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }

    }
    [Serializable]
    public class BO
    {
        string _code;

        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }

    }
}

【问题讨论】:

    标签: .net silverlight wcf asynchronous nested


    【解决方案1】:

    我会将[DataContract] 属性放在类上,将[DataMember] 放在要包含在 WCF 消息中的所有属性上。

    [DataContract]
    public class Customer
    {
        string _firstname;
        string _lastname;              
        List<BO> _bos;
        List<AO> _aos;
    
        [DataMember]
        public string FirstName
        {
            get { return _firstname; }
            set { _firstname = value; }
        }
    
        [DataMember]
        public string LastName
        {
            get { return _lastname; }
            set { _lastname = value; }
        }
    
        [DataMember]
        public System.Collections.Generic.List<AvailableOption> AvailableOptions
        {
            get { return _availableoptions; }
            set { _availableoptions = value; }
        }
    
        [DataMember]
        public System.Collections.Generic.List<BuiltOption> BuiltOptions
        {
            get { return _builtoptions; }
            set { _builtoptions = value; }
        }
    }
    
    [DataContract]
    public class AO
    {
        string _code;
    
        [DataMember]
        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }
    }
    
    [DataContract]
    public class BO
    {
        string _code;
    
        [DataMember]
        public string Code
        {
            get { return _code; }
            set { _code = value; }
        }
    }
    

    使用 .NET 3.5 SP1 中的 WCF,这不再是必须具备的标准,但为了清楚明确地表达我的意图,我仍然会使用这些标准。您需要用那些装饰 所有 类及其属性 - 甚至是嵌套类和后代类等。

    此外,您使用的[Serializable] 属性实际上与WCF 消息序列化没有任何关系。 WCF 使用使用[DataContract] / [DataMember] 属性的数据协定序列化程序(默认情况下)或XmlSerializer(可选;也可以不使用[Serializable] 属性)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多