我尝试过以下代码:
PolicyProcessRequest.BranchCode = "HeadOff"
PolicyProcessRequest.Policy.BranchCode = "HeadOff"
PolicyProcessRequest.Policy.Risks.Item(0).BranchCode = "HeadOff"
Dim dcs As DataContractSerializer = New DataContractSerializer(GetType(PureMessagingService.PolicyProcessRequestType))
Dim ms As New MemoryStream()
dcs.WriteObject(ms, PolicyProcessRequest)
在调用WriteObject
时遇到以下异常发现了System.Runtime.Serialization.SerializationException
HResult = -2146233076 Message =类型为Sirius.SBO.Import.PureMessagingService.BaseRequestType的成员BranchCode无法序列化。 此异常通常是由于尝试使用不允许空值的空值引起的。
'BranchCode'成员设置为其默认值(通常为null或零)。该成员的EmitDefault设置为“false”,表示该成员不应序列化。
但是,成员的IsRequired设置为“true”,表示必须将其序列化。这种冲突无法解决。考虑将“BranchCode”设置为非默认值。或者,您可以将DataMemberAttribute属性上的EmitDefaultValue属性更改为true,或将IsRequired属性更改为false。
然而,我已将'BranchCode'属性设置为请求中的任何位置的非默认值。
Public Class BaseNBQuoteRequestType
Private agentCodeField As String
Private branchCodeField As String
Private currencyCodeField As CurrencyType
Private currencyCodeFieldSpecified As Boolean
Private itemField As BasePartyType
Private policyField As BaseQuoteRiskMsgType
Private updatePartyField As Boolean
Public Property AgentCode() As String
Get
Return Me.agentCodeField
End Get
Set(ByVal value As String)
Me.agentCodeField = value
End Set
End Property
Public Property BranchCode() As String
Get
Return Me.branchCodeField
End Get
Set(ByVal value As String)
Me.branchCodeField = value
End Set
End Property
Public Property CurrencyCode() As CurrencyType
Get
Return Me.currencyCodeField
End Get
Set(ByVal value As CurrencyType)
Me.currencyCodeField = value
End Set
End Property
Public Property CurrencyCodeSpecified() As Boolean
Get
Return Me.currencyCodeFieldSpecified
End Get
Set(ByVal value As Boolean)
Me.currencyCodeFieldSpecified = value
End Set
End Property
Public Property Party() As BasePartyType
Get
Return Me.itemField
End Get
Set(ByVal value As BasePartyType)
Me.itemField = value
End Set
End Property
Public Property Policy() As BaseQuoteRiskMsgType
Get
Return Me.policyField
End Get
Set(ByVal value As BaseQuoteRiskMsgType)
Me.policyField = value
End Set
End Property
Public Property UpdateParty() As Boolean
Get
Return Me.updatePartyField
End Get
Set(ByVal value As Boolean)
Me.updatePartyField = value
End Set
End Property
End Class
答案 0 :(得分:0)
我遇到了同样的问题,它来自用于生成代码的DataContractSerializer。我有其他服务使用XmlSerializer没有问题。
不幸的是,在Visual Studio中使用Svcutil.exe或“添加服务引用”功能生成客户端代码时,会自动为您选择合适的序列化程序。如果架构与DataContractSerializer不兼容,则选择XmlSerializer(source)。
因此,每次生成时都必须手动修复references.cs中的IsRequired设置。
替换
IsRequired=true
通过
IsRequired=false
在reference.cs文件中。