WCF error 'There was an error while trying to serialize parameter...'

12,834

I think that you will need to add the KnownType(typeof(DAInt)) attribute to the DataEventSet class because you are using it in a polymorphic manner. I usually add my changes to the generated code in a new file called something like DataEventSet.xsd.nongenerated.cs. This is why the generated code creates the classes as partial classes.

Inside DataEventSet.xsd.nongenerated.cs, you'll have something like this:

[KnownType(typeof(DAInt))]
public partial class DataEventSet {
}

If that doesn't work, then you can always try changing the contract to use the XmlSerializer instead of the DataContractSerializer. That should work well because it uses the Xml attributes created by xsd.exe.

You can specify that the runtime uses the XmlSerializer by adding the XmlSerializerFormatter attribute to the service contract.

Share:
12,834
Jimmy
Author by

Jimmy

A few of my creations: https://golfforecast.co.uk https://golftipsters.com https://whatsyum.app https://smoothopener.com https://deathcalculator.com

Updated on July 16, 2022

Comments

  • Jimmy
    Jimmy almost 2 years

    My service works for other methods but when I try to invoke a method with a more complicated collection I get the error (from Service Trace Viewer)

    'There was an error while trying to serialize parameter http://tempuri.org/:GetDataEventSetResult. The InnerException message was 'Type 'MimosaServerLib.DAInt' with data contract name 'DAInt:http://schemas.datacontract.org/2004/07/MimosaServerLib' is not expected...'

    Answers to questions with the same error that I've seen involve changing the class definition to turn off 'ProxyCreationEnabled' but the classes I'm working with (DataEventSet, DAInt) come from a file that's been auto generated using xsd tool that I've been given. i.e. I ought not to be changing it.

    I create the DataEventSet object, which is exposed, like so:

        private DataEventSet CreateDataEventSet()
        {
            DataEventSet aDataEventSet = new DataEventSet();
            DataEvent[] dataEvents = new DataEvent[2];
            DAInt aDAInt = new DAInt();
                aDAInt.id = 100100100;
                aDAInt.value = 1;
                dataEvents[0] = aDAInt;
            DADataSeq aDADataSeq = new DADataSeq();
                aDADataSeq.id = 200100100;
                double[] vals = new double[2];
                    vals[0] = 5;
                    vals[1] = 44;
                aDADataSeq.values = vals;
                double[] vals2 = new double[2];
                    vals2[0] = 1;
                    vals2[1] = 1;
                aDADataSeq.xAxisDeltas = vals2;
                aDADataSeq.xAxisStart = 0;
                dataEvents[1] = aDADataSeq;
            aDataEventSet.id = 0;
            Site aSite = new Site();
                aSite.category = SITE_CATEGORY.SITE_SPECIFIC;
            aDataEventSet.site = aSite;
            OsacbmTime aTime = new OsacbmTime();
                aTime.tick_time = 12313246;
                aTime.time = "2007-09-20T14:46:04.123";
                aTime.time_type = OsacbmTimeType.OSACBM_TIME_MIMOSA;
            aDataEventSet.time = aTime;
            aDataEventSet.dataEvents = dataEvents;
    
            return aDataEventSet;
        }
    

    Edit: The class definition of the DataEventSet

    //This source code was auto-generated by xsd
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.mimosa.org/OSACBMV3-1l")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.mimosa.org/OSACBMV3-1l", IsNullable=false)]
    public partial class DataEventSet {
    
    private bool alertStatusField;
    
    private bool alertStatusFieldSpecified;
    
    private DataEvent[] dataEventsField;
    
    private ulong idField;
    
    private Site siteField;
    
    private OsacbmTime timeField;
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public bool alertStatus {
        get {
            return this.alertStatusField;
        }
        set {
            this.alertStatusField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool alertStatusSpecified {
        get {
            return this.alertStatusFieldSpecified;
        }
        set {
            this.alertStatusFieldSpecified = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("dataEvents", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public DataEvent[] dataEvents {
        get {
            return this.dataEventsField;
        }
        set {
            this.dataEventsField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public ulong id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public Site site {
        get {
            return this.siteField;
        }
        set {
            this.siteField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public OsacbmTime time {
        get {
            return this.timeField;
        }
        set {
            this.timeField = value;
        }
    }
    

    }