Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF doesn't serialize all properties

I'm consuming a SOAP service that has been imported as a Service Reference in VS2010. I call one of the services with a request object that is provided by the service. The problem I'm having is that not all properties of the object are serialized, or rather not sent over the wire. The request object looks like this:

var serviceRequest = new UpdateRequest{
    StoreId = request.StoreId,
    Id = request.Id,
    Status = (Status)Enum.Parse(typeof(Status), request.myStatus.ToString()),
    parameters = request.Parameters,
    validFrom = request.ValidFrom.Value,
    validFromSpecified = request.ValidFromSpecified
};

And here is what is sent over the wire. I've captured it with wireshark

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<updateStore xmlns="http://localhost.service.com/">
    <StoreRequest>
    <StoreId>1234</StoreId>
    <validFrom>2011-11-29T00:00:00</validFrom>
    <parameters>
        <param1>true</param1>
    </parameters>
    </StoreRequest>
</updateStore>
</s:Body>
</s:Envelope>

Two of the parameters, Id and Status have not been sent to the service, and I just can't figure out why. The values are being set and the WSDL generated properties are public and have the same serialization attributes as the properties that are being serialized.

Any help would be appreciated.

Edit---- Updated with service reference generated code

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://myservice.com/")]
public partial class StoreUpdateRequest : object,        
System.ComponentModel.INotifyPropertyChanged {
    private long StoreIdField;        
    private long IdField;        
    private bool IdFieldSpecified;        
    private Status StatusField;        
    private bool StatusFieldSpecified;        
    private long storeIdField;        
    private bool storeIdFieldSpecified;        
    private System.DateTime validFromField;        
    private bool validFromFieldSpecified;        
    private System.DateTime validToField;        
    private bool validToFieldSpecified;        
    private technicalParameters parametersField;        
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public long StoreId {
        get {
            return this.StoreIdField;
        }
        set {
            this.StoreIdField = value;
            this.RaisePropertyChanged("StoreId");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=1)]
    public long Id {
        get {
            return this.IdField;
        }
        set {
            this.IdField = value;
            this.RaisePropertyChanged("Id");
        }
    }
like image 621
Gargamel Avatar asked Nov 29 '11 13:11

Gargamel


1 Answers

The problem is resolved. The error was that the SOAP service had been updated and some properties now where optional. When a field/property say XX is marked as optional in a SOAP message WCF creates an corresponding extra XXIsSpecified property that must be set to true when the XX value is set. Otherwise WCF will not serialize nor send that property.

There are different ways to see if a property is set as optional.

  • In the generated Reference.cs file each optional will have a corresponding IsSpecified property, like so: private System.DateTime validFromField; private bool validFromFieldSpecified;

  • You can use soapUI to see and test the wsdl

  • Browse to the wsdl with chrome or another browser and see if the element has the minoccurs attribute. If it's there and it has the value of 0 then it's optional. like this <xs:element minOccurs="0" name="validFrom" type="xs:dateTime"/>
like image 170
Gargamel Avatar answered Sep 29 '22 18:09

Gargamel