Can't serialize using DataContractSerializer - wcf

I have tried the following code:
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)
Am getting the following exception on the call to WriteObject
System.Runtime.Serialization.SerializationException was caught
HResult=-2146233076 Message=Member BranchCode in type Sirius.SBO.Import.PureMessagingService.BaseRequestType cannot be serialized.
This exception is usually caused by trying to use a null value where a null value is not allowed.
The 'BranchCode' member is set to its default value (usually null or zero). The member's EmitDefault setting is 'false', indicating that the member should not be serialized.
However, the member's IsRequired setting is 'true', indicating that it must be serialized. This conflict cannot be resolved. Consider setting 'BranchCode' to a non-default value. Alternatively, you can change the EmitDefaultValue property on the DataMemberAttribute attribute to true, or changing the IsRequired property to false.
Yet I've set the 'BranchCode' property to the non default value everywhere in the request.
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

I have the same problem, and it comes from the DataContractSerializer which is used to generate the code. I have other services which use the XmlSerializer with no problem.
Unfortunatly, when using Svcutil.exe or the 'Add Service Reference' feature in Visual Studio to generate client code, an appropriate serializer is automatically selected for you. If the schema is not compatible with the DataContractSerializer, the XmlSerializer is selected (source).
So, you have to manualy fix the IsRequired setting in references.cs each time you generate it.
Replace
IsRequired=true
by
IsRequired=false
in the references.cs file.

Related

In VB.NET, is it possible to break on an AutoProperty's Set?

In VB.NET, if a property is defined thusly:
Private _status As String
Public Property Status() As String
Get
Return _status
End Get
Set(value As String)
_status = value
End Set
End Property
... then it is trivial to add a break to the Set part of the property in Visual Studio.
However, if it is defined as an AutoProperty:
Public Property Status() As String
, can a break still be added to the Set somehow, without converting it temporarily to a property with a backing variable as in the first example?

how to append one class to another of same in vb.net

PropertyPolicy is a class that defines a collection of several fields/entities. Sometimes two separate functions are needed to build out the collection. (LoadEstateAIN and LoadAIN). I need to combine the results of both classes but have tried concat but get a cast exception. What would work here?
Public Class PropertyPolicy
Private agentfield As Entity
Private aininsuredfield() As Entity
Private billinginfofield As BillingInfo
Private cancellationdatefield As Date
Private claimsfield() As Claims
Public Property Agent As Entity
Get
Return Me.agentfield
End Get
Set(ByVal value As Entity)
Me.agentfield = value
End Set
End Property
Public Property AINInsured() As Entity()
Get
Return Me.aininsuredfield
End Get
Set(ByVal value As Entity())
Me.aininsuredfield = value
End Set
End Property
Public Property BillingInfo As BillingInfo
Get
Return Me.billinginfofield
End Get
Set(ByVal value As BillingInfo)
Me.billinginfofield = value
End Set
End Property
Public Property CancellationDate As Date
Get
Return Me.cancellationdatefield
End Get
Set(ByVal value As Date)
Me.cancellationdatefield = value
End Set
End Property
Public Property Claims() As Claims()
Get
Return Me.claimsfield
End Get
Set(ByVal value As Claims())
Me.claimsfield = value
End Set
End Property
End Class
Dim propTemp1 As New PropertyPolicy
Dim propTemp2 As New PropertyPolicy
Dim propTempComb As New PropertyPolicy
propTemp1.AINInsured = LoadEstateAIN(policyid, asofDate, lob, NINclientid, estatecompany)
propTemp2.AINInsured = LoadAIN(policyid, asofDate, lob, NINclientid, estatecompany)
propTempComb.AINInsured = propTemp1.AINInsured.Concat(propTemp2.AINInsured)
The result of Concat is not an array; it is an IEnumerable(Of T). In your case it is an IEnumerable(Of Entity). You just need to add ToArray() to the end of the Concat if you want to assign it back to an array.
propTempComb.AINInsured = propTemp1.AINInsured.Concat(propTemp2.AINInsured).ToArray()
Breaking down this line of code:
[instance3].[property] = [instance1].[property].Concat([instance2].[property])
Assigns the result of the Concat to the property, but the property is an array so you need to change the result of Concat which is an IEnumerable(Of Entity) into an array which is trivial with ToArray.
I could go further and recommend that you don't use arrays as public members, rather IEnumerable. Also, auto-properties would be a better choice for some of these Public/Public properties.
Public Class PropertyPolicy
Private aininsuredfield As Entity()
Private claimsfield As Claims()
Public Property Agent As Entity
Public Property BillingInfo As BillingInfo
Public Property CancellationDate As Date
Public Property AINInsured() As IEnumerable(Of Entity)
Get
Return aininsuredfield
End Get
Set(value As IEnumerable(Of Entity))
aininsuredfield = value.ToArray()
End Set
End Property
Public Property Claims() As IEnumerable(Of Claims)
Get
Return claimsfield
End Get
Set(value As IEnumerable(Of Claims))
claimsfield = value.ToArray()
End Set
End Property
End Class
And btw, that would cause your original code to work without ToArray()
propTempComb.AINInsured = propTemp1.AINInsured.Concat(propTemp2.AINInsured)

Predefined class properties if need expand or shrink quantity of them on each app start

Public Class Products
Private zCriteria As String
Public Property Criteria As String
Get
Return zCriteria
End Get
Set(value As String)
zCriteria = value
End Set
End Property
Private zProductList As New List(Of Product)
Public Property ProductList As List(Of Product)
Get
Return zProductList
End Get
Set(value As List(Of Product))
zProductList = value
End Set
End Property
End Class
Public Class Product
Private zCriteriaList As List(Of Criterias)
Public Property CriteriaList As List(Of Criterias)
Get
Return zCriteriaList
End Get
Set(value As List(Of Criterias))
zCriteriaList = value
End Set
End Property
End Class
Public Class Criterias
Private zCrPropName As String
Public Property CrPropName As String
Get
Return zCrPropName
End Get
Set(value As String)
zCrPropName = value
End Set
End Property
Private zCritCode As String
Public Property CritCode As String
Get
Return zCritCode
End Get
Set(value As String)
zCritCode = value
End Set
End Property
Private zcrPropValue As String
Public Property crPropValue As String
Get
Return zcrPropValue
End Get
Set(value As String)
zcrPropValue = value
End Set
End Property
End Class
For Each oProducts As Products In oAssigment.ProductsList
For Each oproduct As Product In oProducts.ProductList
For Each cr As Criterias In oproduct.CriteriaList
cr.CrPropName = "Product Name" 'some object property name
cr.CritCode = "PN"
cr.crPropValue = "" ' Value of property "Product Name"
Next
Next
Next
It is all made to have different properties of some object depending on options set in text file. It is only a sample o usage.
conditions:
Criterias on applications start is same for all objects (=Product) i want to read.
Every time on start, application read options file where is defined few property names and codes (values i want get from objects). So every run can be initiated different quantity of properties to read. It means i can not have hard-coded names of properties.
Someone can advice me how to predefine "CrPropName" and "CritCode" on app start so many times how much property names defined in options and after that populate it so many times as how many objects exist from which i read these property values.
p.s. I am not very professional at coding and sorry for my language

How to make several similar properties call one generic one

I'm wondering if it's possible in VB.NET to make similar properties call one generic one?
A sentence doesn't explain it well so here's a code example.
I have a bit field defined like this:
<Flags()> _
Enum E_Operation As Integer
Upload = 1
Download = 2
Overwrite = 4
etc...
End Enum
Now my class has one property per bit in the bit field. Each property just returns the value or sets the corresponding flag. e.g.
Public Property IsUpload() As Boolean
Get
Return ((Operation And E_Operation.Upload) = E_Operation.Upload)
End Get
Set(ByVal value As Boolean)
SetBit(E_Operation.Upload, value)
End Set
End Property
Now I have quite a lot of properties and I would like to simplify them (ideally just one line) by calling a generic property with the bit number to Set or Get.
Public Property IsUpload() As Boolean
GenericProperty(E_Operation.Upload)
End Property
Is there any way to achieve this in VB.NET?
You can make the enumeration a parameter in a private property:
Private Property OperationFlag(Flag As E_Operation) As Boolean
Get
Return ((Operation And Flag) = Flag)
End Get
Set(ByVal value As Boolean)
Operation = (Operation And Not Flag) Or (value And Flag)
End Set
End Property
And make a public property wrapper:
Public Property IsUpload As Boolean
Get
Return OperationFlag(E_Operation.Upload)
End Get
Set(value As Boolean)
OperationFlag(E_Operation.Upload) = value
End Set
End Property

how to create this class

I have a class created from an XSD file in vb.net2010
Partial Public Class responseOperation
Private attributeField() As attribute
Public Property attribute() As attribute()
Get
Return Me.attributeField
End Get
Set(ByVal value As attribute())
Me.attributeField = value
End Set
End Property
how do i instantiate or fill up attribute property where attribute class is given as
Partial Public Class attribute
Private nameField As String
Private valueField As String
Public Property name() As String
Get
Return Me.nameField
End Get
Set(ByVal value As String)
Me.nameField = value
End Set
End Property
Public Property value() As String
Get
Return Me.valueField
End Get
Set(ByVal value As String)
Me.valueField = value
End Set
End Property
End Class
when i run
xml_req_obj.attribute(0).value = "abcd"
xml_req_obj.attribute(0).name = "efg"
I get runtime error as im assigning something to a class that is not created
This seems like a trivial task
If some can tell me how can I create this object and pass data to attribute property that would be wonderful
Thankyou
Hansen
You are not instantiating the array or array items.
Try
Private attributeField() As attribute = New attribute(20)
and
xml_req_obj.attribute(0) = New Attribute()
xml_req_obj.attribute(0).value = "abcd"
xml_req_obj.attribute(0).name = "efg"