WCF Deserializing Problem
My "Location" object isnt getting serialized in my WCF datacontract, however, all other variables are being set properly. When I try to output a variable in the location object I get the "Object reference not set to an instance of an object" error
My DataContract:
[DataContract(Namespace = "")]
public class CalcRequest : BaseRequest
{
[DataMember(Name = "Products")]
public List<Product> products;
[DataMember(Name = "Location")]
public Location location;
[DataMember(Name = "ShippingMethod")]
public string shippingMethod;
[DataMember(Name = "SystemPromotionCode")]
public string systemPromotionCode;
[DataMember(Name = "UserPromotionCode")]
public string userPromotionCode;
}
The "Location" object:
[DataContract(Name = "Location", Namespace = "")]
public class Location
{
public Location()
{
// do nothing
}
[DataMember(Name = "Country")]
public string country;
[DataMember(Name = "StateProvince")]
public string stateProvince;
[DataMember(Name = "PostalCode")]
public string postalCode;
}
my XML request (version, msgtype, processorID, and customerid are in my "BaseRequest"):
<root>
<Version>1.0</Version>
<MsgType>type</MsgType>
<ProcessorId>28000</ProcessorId>
<CustomerId>28000</CustomerId>
<Products>
<Product>
<SKU>1</SKU>
<Price>2999</Price>
<ProductName>name1</ProductName>
<Quantity>1</Quantity>
</Product>
<Product>
<SKU>2</SKU>
<Price>1999</Price>
<ProductName>name2</ProductName>
<Quantity>1</Quantity>
</Product>
</Products>
<Location>
<Country>US</Country>
<StatePr开发者_运维问答ovince>OH</StateProvince>
<PostalCode>44060</PostalCode>
</Location>
<ShippingMethod>USPS-NextDay</ShippingMethod>
<SystemPromotionCode>CD1244578</SystemPromotionCode>
<UserPromotionCode>2FDGRR</UserPromotionCode>
</root>
... Not sure why this isn't working... any help would be appreciated.
I don't understand what you think is missing, really....
(stuff deleted - not relevant)
UPDATE: to make sure the order of the elements in the XML is correct and interpreted in the right order, you might want to add Order=xxx
statement to the data member attributes-
Otherwise, the data contract serializer will serialize (and deserialize) in alphabetical order (other than the XmlSerializer which serializes in the order the fields appear).
Alphabetical order is case-sensitive,i.e. any upper case characters are considered before any low-case characters.
If you have multiple elements of the same order (that's not a problem), then they'll be serialized alphabetically within their order (e.g. all elements of Order=1 will be serialized in an alphabetical fashion - then all elements with Order=2 and so on).
For derived classes, properties of base class will be serialized first(in alphabetical order) and properties of derived class later(also in alphabetical order).
精彩评论