none
There was an error while trying to serialize parameter http://xxx/Service:MethodResult. Consider using a DataContractResolver or ...

    السؤال

  • Please help me!!!

    I have c# class generated using xsd.exe form XSD file:

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://xxx/PorukaRazmeneV1.xsd")]
    [System.Xml.Serialization.XmlRootAttribute("PorukaRazmeneV1", Namespace="http://xxx/PorukaRazmeneV1.xsd", IsNullable=false)]
    public partial class PorukaRazmeneV1Type {
       
        private string message_idField;
       
        private PorukaRazmeneV1TypeVrsta_poruke vrsta_porukeField;
       
        private PorukaRazmeneV1TypePosiljalac posiljalacField;
       
        private UcesnikRazmeneTip[] primaociField;
       
        private string zavodni_brojField;
       
        private string strani_brojField;
       
        private string nazivField;
       
        private PorukaRazmeneV1TypeStepen_hitnosti stepen_hitnostiField;
       
        private int broj_prilogaField;
       
        private System.Nullable<System.DateTime> time_stampField;
       
        private bool time_stampFieldSpecified;
       
        public PorukaRazmeneV1Type() {
            this.stepen_hitnostiField = PorukaRazmeneV1TypeStepen_hitnosti.nije_hitno;
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public string message_id {
            get {
                return this.message_idField;
            }
            set {
                this.message_idField = value;
            }
        }
       
        /// <remarks/>
        public PorukaRazmeneV1TypeVrsta_poruke vrsta_poruke {
            get {
                return this.vrsta_porukeField;
            }
            set {
                this.vrsta_porukeField = value;
            }
        }
       
        /// <remarks/>
        public PorukaRazmeneV1TypePosiljalac posiljalac {
            get {
                return this.posiljalacField;
            }
            set {
                this.posiljalacField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("UcesnikApstraktno", IsNullable=false)]
        [System.Xml.Serialization.XmlArrayItemAttribute("fizicko_lice_eposta", typeof(FizickoEPostaLiceTip), IsNullable=false)]
        [System.Xml.Serialization.XmlArrayItemAttribute("organ", typeof(OrganTip), IsNullable=false)]
        public UcesnikRazmeneTip[] primaoci {
            get {
                return this.primaociField;
            }
            set {
                this.primaociField = value;
            }
        }
       
        /// <remarks/>
        public string zavodni_broj {
            get {
                return this.zavodni_brojField;
            }
            set {
                this.zavodni_brojField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public string strani_broj {
            get {
                return this.strani_brojField;
            }
            set {
                this.strani_brojField = value;
            }
        }
       
        /// <remarks/>
        public string naziv {
            get {
                return this.nazivField;
            }
            set {
                this.nazivField = value;
            }
        }
       
        /// <remarks/>
        public PorukaRazmeneV1TypeStepen_hitnosti stepen_hitnosti {
            get {
                return this.stepen_hitnostiField;
            }
            set {
                this.stepen_hitnostiField = value;
            }
        }
       
        /// <remarks/>
        public int broj_priloga {
            get {
                return this.broj_prilogaField;
            }
            set {
                this.broj_prilogaField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
        public System.Nullable<System.DateTime> time_stamp {
            get {
                return this.time_stampField;
            }
            set {
                this.time_stampField = value;
            }
        }
       
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool time_stampSpecified {
            get {
                return this.time_stampFieldSpecified;
            }
            set {
                this.time_stampFieldSpecified = value;
            }
        }

        public bool ShouldSerializestrani_broj()
        {
            return strani_broj != null;
        }

        public bool ShouldSerializemessage_id()
        {
            return message_id != null;
        }

        public bool ShouldSerializetime_stamp()
        {
            return time_stamp != null;
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://xxx/PorukaRazmeneV1.xsd")]
    public enum PorukaRazmeneV1TypeVrsta_poruke {
       
        /// <remarks/>
        req,
       
        /// <remarks/>
        ack,
       
        /// <remarks/>
        res,
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://xxx/PorukaRazmeneV1.xsd")]
    public partial class PorukaRazmeneV1TypePosiljalac {
       
        private UcesnikRazmeneTip itemField;
       
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("fizicko_lice_eposta", typeof(FizickoEPostaLiceTip))]
        [System.Xml.Serialization.XmlElementAttribute("organ", typeof(OrganTip))]
        public UcesnikRazmeneTip Item {
            get {
                return this.itemField;
            }
            set {
                this.itemField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://xxx/PorukaRazmeneV1.xsd")]
    [System.Xml.Serialization.XmlRootAttribute("fizicko_lice_eposta", Namespace="http://xxx/PorukaRazmeneV1.xsd", IsNullable=false)]
    public partial class FizickoEPostaLiceTip : FizickoLiceTip {
       
        private string epostaField;
       
        /// <remarks/>
        public string eposta {
            get {
                return this.epostaField;
            }
            set {
                this.epostaField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(FizickoEPostaLiceTip))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://xxx/PorukaRazmeneV1.xsd")]
    public partial class FizickoLiceTip : UcesnikRazmeneTip {
       
        private string jmbgField;
       
        private string punoimeField;
       
        /// <remarks/>
        public string jmbg {
            get {
                return this.jmbgField;
            }
            set {
                this.jmbgField = value;
            }
        }
       
        /// <remarks/>
        public string punoime {
            get {
                return this.punoimeField;
            }
            set {
                this.punoimeField = value;
            }
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(OrganTip))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(FizickoLiceTip))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(FizickoEPostaLiceTip))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://xxx/PorukaRazmeneV1.xsd")]
    public partial class UcesnikRazmeneTip {
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://xxx/PorukaRazmeneV1.xsd")]
    [System.Xml.Serialization.XmlRootAttribute("organ", Namespace="http://xxx/PorukaRazmeneV1.xsd", IsNullable=false)]
    public partial class OrganTip : UcesnikRazmeneTip {
       
        private string sifraField;
       
        /// <remarks/>
        public string sifra {
            get {
                return this.sifraField;
            }
            set {
                this.sifraField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://xxx/PorukaRazmeneV1.xsd")]
    public enum PorukaRazmeneV1TypeStepen_hitnosti {
       
        /// <remarks/>
        nije_hitno,
       
        /// <remarks/>
        hitno,
       
        /// <remarks/>
        veoma_hitno,
    }

    When add service reference to  WCF client  in Reference.cs file I dont have generated derived classes (OrganTip, FizickoLIceTip, FizickoEPostaLiceTip) only UcesnikRazmeneTip, i 'l try to add it manualy but it doesn't work

    And when call service Method i got this error:

    There was an error while trying to serialize parameter http://xxx/Service:MethodResult. The InnerException message was 'Type 'OrganTip' with data contract name 'OrganTip:http://schemas.datacontract.org/2004/07/' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

    • تم التحرير بواسطة MarkoOkram 25/صفر/1432 11:35 م
    25/صفر/1432 11:29 م

الإجابات

جميع الردود

  • Since you generated your data classes using xsd, they should be serializable using the XmlSerializer, not the (default) DataContractSerializer. On your [ServiceContract] interface (or on each [OperationContract] which uses those types), add the [XmlSerializerFormat] attribute, to tell WCF to use the XmlSerializer instead of the DCS.
    25/صفر/1432 11:34 م
  • in fact

    [KnownType(typeof("Derived type")] attribute should be add to root type of generated class( by xsd.exe tool).

    And add that attribute for all derived types

    Thanx

    • تم وضع علامة كإجابة بواسطة MarkoOkram 27/صفر/1432 09:40 ص
    27/صفر/1432 09:38 ص