C# WCF服务异常:格式化程序在尝试反序列化消息时引发异常
格式化程序在尝试反序列化消息时引发异常: 尝试反序列化参数时出错 . 这个 InnerException消息在第1行位置1604处为“错误”。元素 '' 包含 “”数据协定。 反序列化程序不知道映射到此 合同。将与“DBNull”对应的类型添加到已知 类型-例如,通过使用KnownTypeAttribute属性或 将其添加到传递给的已知类型列表中 DataContractSerializer.'。有关详细信息,请参阅InnerException 我的wcf服务功能C# WCF服务异常:格式化程序在尝试反序列化消息时引发异常,c#,wcf,subsonic,C#,Wcf,Subsonic,格式化程序在尝试反序列化消息时引发异常: 尝试反序列化参数时出错 . 这个 InnerException消息在第1行位置1604处为“错误”。元素 '' 包含 “”数据协定。 反序列化程序不知道映射到此 合同。将与“DBNull”对应的类型添加到已知 类型-例如,通过使用KnownTypeAttribute属性或 将其添加到传递给的已知类型列表中 DataContractSerializer.'。有关详细信息,请参阅InnerException 我的wcf服务功能 public PatientI
public PatientInsurance GetPatientInsuranceInformation(int PatientKey)
{
PatientInsurance col = new PatientInsurance();
if (PatientKey > 0)
{
Query qry = new Query(PatientInsurance.Schema.TableName).WHERE(PatientInsurance.Columns.Deleted, false).AND(PatientInsurance.Columns.PatientKey, PatientKey);
col.LoadAndCloseReader(qry.ExecuteReader());
}
return col;
}
类始终由亚音速生成,我在业务逻辑中编写了部分类,如下所示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace PatientPortal.Model.Data
{
[KnownType(typeof(System.DBNull))]
[XmlInclude(typeof(DBNull))]
[KnownType(typeof(PatientInsurance))]
public partial class PatientInsurance
{
public string InsuranceTypeText
{
get
{
string insuranceTypeText = "";
//if (!string.IsNullOrEmpty(Convert.ToString(this.InsuranceType)))
//{
// int InsuranceType = Convert.ToInt32(this.InsuranceType);
// switch (InsuranceType)
// {
// case 1:
// insuranceTypeText = "Primary Insurance";
// break;
// case 2:
// insuranceTypeText = "Secondary Insurance";
// break;
// case 3:
// insuranceTypeText = "Tertiary Insurance";
// break;
// }
//}
return insuranceTypeText;
}
}
public string PrimPolicyHolderNameDisplay
{
get
{
string primPolicyHolderNameDisplay = "display:none;";
if (!string.IsNullOrEmpty(Convert.ToString(this.PrimRelationship)))
{
primPolicyHolderNameDisplay = (this.PrimRelationship == "Self") ? "display:none;" : "";
}
return primPolicyHolderNameDisplay;
}
}
public string SecPolicyHolderNameDisplay
{
get
{
string secPolicyHolderNameDisplay = "display:none;";
if (!string.IsNullOrEmpty(Convert.ToString(this.SecRelationship)))
{
secPolicyHolderNameDisplay = (this.SecRelationship == "Self") ? "display:none;" : "";
}
return secPolicyHolderNameDisplay;
}
}
public string TerPolicyHolderNameDisplay
{
get
{
string terPolicyHolderNameDisplay = "display:none;";
if (!string.IsNullOrEmpty(Convert.ToString(this.TerRelationship)))
{
terPolicyHolderNameDisplay = (this.TerRelationship == "Self") ? "display:none;" : "";
}
return terPolicyHolderNameDisplay;
}
}
}
}
.错误消息基本上意味着您的集合包含DBNull类型的对象。WCF反序列化程序不知道该类型,因此引发异常。如果将KnownTypeAttribute添加到数据契约中,并在其参数中包含DBNull,那么就可以了 有点像这样:
[CollectionDataContract]
[KnownType(typeof(DBNull))]
public class YourList : ArrayList {
}
我的WCF服务是使用Framework4.5构建的,我的消费客户端是使用Framework3.5构建的。由于此原因,添加服务引用向导未为使用声明的KnownTypes生成类属性
[ServiceKnownType(typeof(System.DBNull))]
所以,当反序列化客户端未获得System.DBNull类型时。我们必须在客户端配置文件中添加knowntypes
客户端需要此配置,解决了我的问题:
<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="NameSpace.ServiceClientName.ClassNameForWhichKnownTypeIsToBeGiven, AssemblyName">
<knownType type="System.DBNull"></knownType>
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>
解决此问题所需的所有信息都在异常消息中。。。你到底需要我们做什么?你能发布你的Web服务接收到的类以及Web服务本身吗?你好,Nate Kerkhofs!谢谢你的回复:)我已经更新了我的问题谢谢你法拉文的回复!我是wcf服务的新手。根据您的建议,我添加了[CollectionDataContract][KnownType(typeof(PatientInsurance))]这些属性在我的类之上。但给我的错误是带有CollectionDataContractAttribute属性的“Type”PatientPortal.Model.Data.PatientInsurance“是无效的集合类型,因为它具有DataContractAttribute属性。”还有一个问题,[KnownType(typeof(Player))]属性中的“Player”是什么。它是属性吗?如果是,我是否需要为所有其他列多次添加相同的属性?Player类只是一个示例,为了清晰起见,我对答案进行了编辑。我相信已知的类型只需要是DataContracts。如果您发布更多的代码,这将有所帮助。