gpt4 book ai didi

c# - WCF 服务异常 :The formatter threw an exception while trying to deserialize the message

转载 作者:太空狗 更新时间:2023-10-30 01:03:57 24 4
gpt4 key购买 nike

格式化程序在尝试反序列化消息时抛出异常:

There was an error while trying to deserialize parameter http://tempuri.org/:GetPatientInsuranceInformationResult. The InnerException message was 'Error in line 1 position 1604. Element 'http://schemas.datacontract.org/2004/07/SubSonic:_currentValue' contains data of the 'http://schemas.datacontract.org/2004/07/System:DBNull' data contract. The deserializer has no knowledge of any type that maps to this contract. Add the type corresponding to 'DBNull' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details

我的wcf服务函数

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;
}

类总是由 subsonic 生成。我在业务逻辑中编写了部分类,如下所示

 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;
}
}
}
}

.

最佳答案

我的 WCF 服务是使用框架 4.5 构建的,我的消费客户端是使用框架 3.5 构建的。由于这个添加服务引用向导没有为使用声明的 KnownTypes 生成类属性

 [ServiceKnownType(typeof(System.DBNull))] 

因此,当反序列化客户端时,没有获取 System.DBNull 类型。我们必须在客户端配置文件中添加已知类型。

客户端需要的这个配置解决了我的问题:

<system.runtime.serialization>
<dataContractSerializer>
<declaredTypes>
<add type="NameSpace.ServiceClientName.ClassNameForWhichKnownTypeIsToBeGiven, AssemblyName">
<knownType type="System.DBNull"></knownType>
</add>
</declaredTypes>
</dataContractSerializer>
</system.runtime.serialization>

关于c# - WCF 服务异常 :The formatter threw an exception while trying to deserialize the message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25548365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com