簡體   English   中英

WCF服務異常:格式化程序在嘗試反序列化消息時拋出異常

[英]WCF service Exception:The formatter threw an exception while trying to deserialize the message

格式化程序在嘗試反序列化消息時拋出異常:

嘗試反序列化參數http://tempuri.org/:GetPatientInsuranceInformationResult時出錯。 設置InnerException信息是“錯誤在第1個位置1604元素” http://schemas.datacontract.org/2004/07/SubSonic:_currentValue “包含的數據的” http://schemas.datacontract.org/2004/07 / System:DBNull '數據合約。 反序列化器不知道映射到此合同的任何類型。 將與“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;
            }
        }
    }
}

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

錯誤消息基本上意味着您的集合包含DBNull類型的對象。 WCF反序列化器不知道該類型,因此拋出異常。 如果將KnownTypeAttribute添加到數據協定中並在其參數中包含DBNull,則可以。

有點像:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM