首页 文章

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

提问于
浏览
4

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

尝试反序列化参数http://tempuri.org/:GetPatientInsuranceInformationResult时出错 . InnerException消息是'第1行位置错误1604.元素'http://schemas.datacontract.org/2004/07/SubSonic:_currentValue'包含'http://schemas.datacontract.org/2004/07的数据/ System:DBNull'数据合约 . 反序列化器不知道映射到此 Contract 的任何类型 . 将与“DBNull”对应的类型添加到已知类型列表中 - 例如,通过使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型列表中 . 有关更多详细信息,请参阅InnerException

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

class总是由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;
            }
        }
    }
}

.

2 回答

  • 4

    我的WCF服务是使用框架4.5构建的,我的使用客户端是使用框架3.5构建的 . 由于此Add Service Reference向导未生成使用声明的KnownTypes的Class属性

    [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>
    
  • 0

    错误消息基本上意味着您的集合包含DBNull类型的对象 . WCF反序列化器不知道该类型,因此抛出异常 . 如果将KnownTypeAttribute添加到数据协定中并在其参数中包含DBNull,则可以 .

    有点像:

    [CollectionDataContract]
    [KnownType(typeof(DBNull))]
    public class YourList : ArrayList {
    }
    

相关问题