2012-02-07 2 views
1

가 나는 다음과 같은 작업을 계약 기준에WCF 가능성이 직렬화 관련 문제

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

와 서비스를 정의

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

다음 클래스를 가지고 거기의 단순 복사본을 정의 다음과 같은 방법 나는 service.GetAll을 (전화 클라이언트에

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

)과에 나는 t를 사용 그는보기에 대한 내 모델을 정의하는 프록시에 정의 된 A의 얕은 사본입니다. 전화는

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

과 비슷하지만 클라이언트의 목록은 항상 emtpy입니다. 문제가 무엇입니까?

답변

2

당신은 datacontract로 데이터 클래스,를 정의한다 : 클래스에

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

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

에 더 읽기이

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

같은 일부 것은 getter 및 setter가 필요합니다. –

2

당신이 System.Runtime.Serialization.DataContractAttribute 네임 스페이스에서 DataContract 속성이 필요합니다. 공용 변수를 사용할 때 일이 잘 작동 -

내가 당신이 실제로하지 않는 것을 추가하고 싶습니다 MSDN

+1

나는 getters와 setters가 실제로 필요 없다고 덧붙이고 싶다. public 변수를 사용할 때 잘 작동한다. –

+0

+1 맞습니다. 그러나 OOPs를 따르기를 원한다면 public member 대신에 getter와 setter property를 사용할 것입니다. 개발자에 따라 다릅니다. 둘 다 작동합니다. –

+0

@ThorstenDittmar 그냥이 게시물을 읽고 http://stackoverflow.com/questions/4213982/wcf-classes-public-property-vs-property-with-getset 이것에 대해 확실하지, 그것을 테스트해야합니다. –