WCF可能与序列化相关的问题

时间:2012-02-07 12:28:25

标签: wcf c#-4.0

在服务器上我有以下类

public class A 
{ 
   string a1 {get; set ;}
   string a2 {get; set;}
}

我已使用以下操作合同

定义了服务
[OperationContract]
public list<A> GetAll()
{
    return new List<A> {new A {a1="1", a2="2"}, new A{a1="3", a2="4"}};
}

在参考文献中,以下列方式定义了A的浅表副本

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="GetAll", Namespace="http://schemas.datacontract.org/2004/07/SomeModel")]
[System.SerializableAttribute()]
public partial class A: object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

        [System.NonSerializedAttribute()]
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

        [System.Runtime.Serialization.OptionalFieldAttribute()]
        private string A1Field;

        [System.Runtime.Serialization.OptionalFieldAttribute()]
        private string A2Field;


        [global::System.ComponentModel.BrowsableAttribute(false)]
        public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
            get {
                return this.extensionDataField;
            }
            set {
                this.extensionDataField = value;
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public string A1{
            get {
                return this.A1Field;
            }
            set {
                if ((object.ReferenceEquals(this.A1Field, value) != true)) {
                    this.A1Field= value;
                    this.RaisePropertyChanged("A1");
                }
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public string A2{
            get {
                return this.A2Field;
            }
            set {
                if ((object.ReferenceEquals(this.A2Field, value) != true)) {
                    this.A2Field= value;
                    this.RaisePropertyChanged("A2");
                }
            }
        }


        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

在客户端上,我调用service.GetAll(),并使用代理中定义的A的浅表副本,该副本定义了视图的模型。这个电话类似于

ActionResult GetAll()
{
   List<A> allAs = service.GetAll();
   return new View (allAs);
}

然而,客户端上的列表始终是emtpy。有什么问题?

2 个答案:

答案 0 :(得分:2)

您应该将数据类A定义为datacontract:

[DataContract]
public class A
{
    [DataMember]
    public string a1 {get; set ;}

    [DataMember]
    public string a2 {get; set ;}
}

答案 1 :(得分:2)

在课程中,您需要DataContract命名空间中的System.Runtime.Serialization.DataContractAttribute属性。

有点像这样

[DataContract]
public class A 
{ 
   [DataMember]
   public string a1 {get; set ;} //This should be public
   [DataMember]
   public string a2 {get; set;}//This should be public
}

详细了解MSDN