Elena
Elena

Reputation: 839

WCF possibly serialization related issue

On server I have the following class

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

I have defined a service with the following operation contract

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

in the reference there is defined a shallow copy of A in the following way

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

On the client I call the service.GetAll() and I use the shallow copy of A, defined in the proxy which defines my model for a view. the call is something similar to

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

However the list on the client is always emtpy. What is the problem?

Upvotes: 1

Views: 1510

Answers (2)

Amar Palsapure
Amar Palsapure

Reputation: 9680

On the class you will need DataContract attribute from System.Runtime.Serialization.DataContractAttribute namespace.

Some thing like this

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

Read more on MSDN

Upvotes: 2

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

You should define your data class, A as a datacontract:

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

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

Upvotes: 2

Related Questions