C# WCF错误';尝试序列化参数时出错…';

C# WCF错误';尝试序列化参数时出错…';,c#,wcf,web-services,serialization,C#,Wcf,Web Services,Serialization,我的服务适用于其他方法,但当我尝试使用更复杂的集合调用方法时,会出现错误(来自服务跟踪查看器) '尝试序列化参数时出错。InnerException消息为“Type”MimosaServerLib.DAInt”,数据协定名称为“DAInt:”不应为…' 对于出现与我所看到的相同错误的问题的回答涉及更改类定义以关闭“ProxyCreationEnabled”,但我正在使用的类(DataEventSet,DAInt)来自使用给定的xsd工具自动生成的文件。i、 我不应该改变它 我创建了DataEve

我的服务适用于其他方法,但当我尝试使用更复杂的集合调用方法时,会出现错误(来自服务跟踪查看器)

'尝试序列化参数时出错。InnerException消息为“Type”MimosaServerLib.DAInt”,数据协定名称为“DAInt:”不应为…'

对于出现与我所看到的相同错误的问题的回答涉及更改类定义以关闭“ProxyCreationEnabled”,但我正在使用的类(
DataEventSet
DAInt
)来自使用给定的xsd工具自动生成的文件。i、 我不应该改变它

我创建了
DataEventSet
对象,它是公开的,如下所示:

    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;
    }
编辑:
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;
    }
}
//此源代码是由xsd自动生成的
[System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”、“2.0.50727.42”)]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute(“代码”)]
[System.Xml.Serialization.XmlTypeAttribute(命名空间=”http://www.mimosa.org/OSACBMV3-1l")]
[System.Xml.Serialization.XmlRootAttribute(命名空间=”http://www.mimosa.org/OSACBMV3-1l“,IsNullable=false)]
公共部分类DataEventSet{
私有布尔报警状态字段;
指定专用布尔报警状态字段;
私有DataEvent[]DataEvents字段;
乌隆-伊菲尔德私人酒店;
私人场地;
私有时间域;
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共布尔警报状态{
得到{
返回此.alertStatus字段;
}
设置{
this.alertStatusField=值;
}
}
/// 
[System.Xml.Serialization.XmlIgnoreAttribute()]
指定了公共布尔报警状态{
得到{
返回指定的此.alertStatusField;
}
设置{
this.alertStatusFieldSpecified=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(“dataEvents”,Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共DataEvent[]数据事件{
得到{
返回this.dataevents字段;
}
设置{
this.dataevents字段=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共乌龙id{
得到{
返回此.idField;
}
设置{
this.idField=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共站点{
得到{
返回此.siteField;
}
设置{
this.siteField=值;
}
}
/// 
[System.Xml.Serialization.xmlementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
公共时间{
得到{
返回此.timeField;
}
设置{
this.timeField=值;
}
}

}

我认为您需要将
KnownType(typeof(DAInt))
属性添加到
DataEventSet
类中,因为您正在以多态方式使用它。我通常将更改添加到一个名为DataEventSet.xsd.nongenerated.cs的新文件中生成的代码中。这就是生成的代码将类创建为分部类的原因

在DataEventSet.xsd.nongenerated.cs中,您将看到如下内容:

[KnownType(typeof(DAInt))]
public partial class DataEventSet {
}
如果这不起作用,那么您可以尝试更改契约以使用
XmlSerializer
而不是
DataContractSerializer
。这应该可以很好地工作,因为它使用由xsd.exe创建的Xml属性


您可以通过向服务契约添加属性来指定运行时使用XmlSerializer。

谢谢!它现在在WCF测试客户端中工作。但是,当我尝试使用外部客户端访问服务时,我得到一个“NetDispatcherFaultException”:“。InnerException消息是“第1行位置898处出错。元素“”包含映射到名称的类型的数据。”这是使用KnownType()属性的吗?是的,我还必须添加[KnownType(typeof(DADataSeq))]