WCF 错误“尝试序列化参数时出错...”
Posted
技术标签:
【中文标题】WCF 错误“尝试序列化参数时出错...”【英文标题】:WCF error 'There was an error while trying to serialize parameter...' 【发布时间】:2013-01-26 16:29:01 【问题描述】:我的服务适用于其他方法,但是当我尝试调用具有更复杂集合的方法时,我收到错误(来自服务跟踪查看器)
'尝试序列化参数http://tempuri.org/:GetDataEventSetResult时出错。 InnerException 消息是 'Type 'MimosaServerLib.DAInt' with data contract name 'DAInt:http://schemas.datacontract.org/2004/07/MimosaServerLib' is not expected...'
对我看到的具有相同错误的问题的答案涉及更改类定义以关闭“ProxyCreationEnabled”,但我正在使用的类(DataEventSet
、DAInt
)来自一个自动的文件使用我提供的 xsd 工具生成。即我不应该改变它。
我创建了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;
【问题讨论】:
【参考方案1】:我认为您需要将KnownType(typeof(DAInt))
属性添加到DataEventSet
类,因为您以多态方式使用它。我通常将我对生成代码的更改添加到一个名为 DataEventSet.xsd.nongenerated.cs 的新文件中。这就是生成的代码将类创建为部分类的原因。
在 DataEventSet.xsd.nongenerated.cs 中,您将拥有如下内容:
[KnownType(typeof(DAInt))]
public partial class DataEventSet
如果这不起作用,那么您可以随时尝试更改合同以使用XmlSerializer
而不是DataContractSerializer
。这应该很好用,因为它使用 xsd.exe 创建的 Xml 属性。
您可以通过将XmlSerializerFormatter 属性添加到服务协定来指定运行时使用XmlSerializer。
【讨论】:
谢谢!它现在在 WCF 测试客户端中工作。但是当我尝试使用外部客户端访问服务时,我得到一个“NetDispatcherFaultException”:“tempuri.org/:GetDataEventSetResult。InnerException 消息是“第 1 行位置 898 中的错误。元素“schemas.datacontract.org/2004/07/MimosaServerLib:DataEvent”包含来自映射到的类型的数据名字。” 那是使用 KnownType() 属性吗? 是的,我还必须添加 [KnownType(typeof(DADataSeq))]以上是关于WCF 错误“尝试序列化参数时出错...”的主要内容,如果未能解决你的问题,请参考以下文章
WCF 错误“对象图中可以序列化或反序列化的最大项目数为 '65536'”
WCF 错误“对象图中可以序列化或反序列化的最大项目数为 '65536'”