none
[WCF] Errore serializzazione xml passato come parametro

    Domanda

  • Ciao a tutti

    Ho il seguente file xml che passo a un servizio wcf:

    <AvailabilityUpdateRequest>
      <Username>test</Username>
      <Password>123456</Password>
      <PropertyCode>VPSc9e6835c654943</PropertyCode>
      <RoomTypes>
        <RoomType id="VPCae487c103b2347">
          <AvailabilityPeriod>
            <Period start="2017-07-01" end="2017-07-31" >
              <Availability>5</Availability>
            </Period>
          </AvailabilityPeriod>
          <Rates>
            <Rate id="1">
              <Period start="2017-07-01" end="2017-07-31" >
                <Closed>false</Closed>
                <Price>50.00</Price>
                <MinStay>1</MinStay>
                <MaxStay>99</MaxStay>
                <ClosedArrival>false</ClosedArrival>
                <ClosedDeparture>false</ClosedDeparture>
              </Period>
    		  <Period start="2017-08-01" end="2017-08-31" >
                <Closed>false</Closed>
                <Price>80.00</Price>
                <MinStay>7</MinStay>
                <MaxStay>21</MaxStay>
                <ClosedArrival>false</ClosedArrival>
                <ClosedDeparture>false</ClosedDeparture>
              </Period>
            </Rate>
    		<Rate id="2">
              <Period start="2017-07-01" end="2017-07-31" >
                <Closed>false</Closed>
                <Price>45.00</Price>
                <MinStay>1</MinStay>
                <MaxStay>99</MaxStay>
                <ClosedArrival>false</ClosedArrival>
                <ClosedDeparture>false</ClosedDeparture>
              </Period>
    		  <Period start="2017-08-01" end="2017-08-31" >
                <Closed>false</Closed>
                <Price>75.50</Price>
                <MinStay>7</MinStay>
                <MaxStay>21</MaxStay>
                <ClosedArrival>false</ClosedArrival>
                <ClosedDeparture>false</ClosedDeparture>
              </Period>
            </Rate>
          </Rates>
        </RoomType>
    	<RoomType id="VPCe3d87ddfb9c64c">
          <AvailabilityPeriod>
            <Period start="2017-08-01" end="2017-08-31" >
              <Availability>5</Availability>
            </Period>
          </AvailabilityPeriod>
          <Rates>
            <Rate id="1">
              <Period start="2017-08-01" end="2017-08-31" >
                <Closed>false</Closed>
                <Price>65.00</Price>
                <MinStay>7</MinStay>
                <MaxStay>99</MaxStay>
                <ClosedArrival>false</ClosedArrival>
                <ClosedDeparture>false</ClosedDeparture>
              </Period>		  
            </Rate>		
          </Rates>
        </RoomType>
    	<RoomType id="VPC10631a5313f44a">
          <AvailabilityPeriod>
            <Period start="2017-07-01" end="2017-07-31" >
              <Availability>5</Availability>
            </Period>
          </AvailabilityPeriod>
          <Rates>
            <Rate id="2">
              <Period start="2017-07-01" end="2017-07-31" >
                <Closed>false</Closed>
                <Price>75.00</Price>
                <MinStay>1</MinStay>
                <MaxStay>99</MaxStay>
                <ClosedArrival>false</ClosedArrival>
                <ClosedDeparture>false</ClosedDeparture>
              </Period>		  
            </Rate>		
          </Rates>
        </RoomType>
    	<RoomType id="VPC6de7bcb4d23046">
          <AvailabilityPeriod>
            <Period start="2017-08-01" end="2017-08-31" >
              <Availability>5</Availability>
            </Period>
          </AvailabilityPeriod>
          <Rates>
            <Rate id="1">
              <Period start="2017-08-01" end="2017-08-31" >
                <Closed>false</Closed>
                <Price>85.00</Price>
                <MinStay>7</MinStay>
                <MaxStay>21</MaxStay>
                <ClosedArrival>false</ClosedArrival>
                <ClosedDeparture>false</ClosedDeparture>
              </Period>		  
            </Rate>		
          </Rates>
        </RoomType>
    	<RoomType id="VPCd33d5e3993424c">
          <AvailabilityPeriod>
            <Period start="2017-07-01" end="2017-07-31" >
              <Availability>5</Availability>
            </Period>
          </AvailabilityPeriod>
          <Rates>
            <Rate id="2">
              <Period start="2017-07-01" end="2017-07-31" >
                <Closed>false</Closed>
                <Price>95.00</Price>
                <MinStay>1</MinStay>
                <MaxStay>1</MaxStay>
                <ClosedArrival>false</ClosedArrival>
                <ClosedDeparture>false</ClosedDeparture>
              </Period>		  
            </Rate>		
          </Rates>
        </RoomType>
      </RoomTypes>
    </AvailabilityUpdateRequest>

    Nel WebClient ho questo codice:

    HttpWebRequest req = null;
            HttpWebResponse res = null;
            try
            {
                const string url = "http://localhost:52300/Test.svc/AvailabilityUpdate";
                req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                req.ContentType = "application/xml; charset=utf-8";
                req.Timeout = 30000;
                req.Headers.Add("SOAPAction", url);
    
                var xmlDoc = new XmlDocument { XmlResolver = null };
                xmlDoc.Load(Server.MapPath("PostData.xml"));
                string sXml = xmlDoc.InnerXml;
                req.ContentLength = sXml.Length;
                var sw = new StreamWriter(req.GetRequestStream());
                sw.Write(sXml);
                sw.Close();
    
                res = (HttpWebResponse)req.GetResponse();
                Stream responseStream = res.GetResponseStream();
                var streamReader = new StreamReader(responseStream);
    
                //Read the response into an xml document
                var soapResonseXmlDocument = new XmlDocument();
                soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());
    
                //return only the xml representing the response details (inner request)
                string response = soapResonseXmlDocument.InnerXml;
                TextBox1.Text = response;
                //Response.Write(soapResonseXMLDocument.InnerXml);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }


    Nel servizio ho definito:

    [OperationContract]
            [ServiceKnownType(typeof(AvailabilityUpdateResponse))]
            [ServiceKnownType(typeof(Errors))]
            [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "AvailabilityUpdate")]
            object AvailabilityUpdate(AvailabilityUpdateRequest xml);

    Dove 'AvailabilityUpdateRequest' è definito così:

    namespace Test
    {
        [Serializable]
        [DataContract(Namespace = "Test")]
        public class AvailabilityUpdateRequest
        {
            public AvailabilityUpdateRequest()
            {
                RoomTypes = new List<tipoAvailabilityRoomType>();
            }
    
            [DataMember]
            public string Username
            {
                get;
    
                set;
            }
    
            [DataMember]
            public string Password
            {
                get;
    
                set;
            }
    
            [DataMember]
            public string PropertyCode
            {
                get;
    
                set;
            }
    
            [DataMember]
            [XmlArrayItemAttribute("RoomType", IsNullable = false)]
            public List<tipoAvailabilityRoomType> RoomTypes
            {
                get;
    
                set;
            }
        }
    
        public class tipoAvailabilityRoomType
        {
            public tipoAvailabilityRoomType()
            {
                AvailabilityPeriod = new List<tipoAvailabilityAvailabilityPeriod>();
                Rates = new List<tipoAvailabilityRoomTypeRate>();
            }
    
            [DataMember]
            [XmlAttributeAttribute()]
            public string id
            {
                get;
    
                set;
            }
    
            [DataMember]
            [XmlIgnore]
            [XmlArrayItemAttribute("Period", IsNullable = false)]
            public List<tipoAvailabilityAvailabilityPeriod> AvailabilityPeriod
            {
                get;
    
                set;
            }
    
            [DataMember]
            [XmlIgnore]
            [XmlArrayItemAttribute("Rate", IsNullable = false)]
            public List<tipoAvailabilityRoomTypeRate> Rates
            {
                get;
    
                set;
            }
        }
    
        #region tipoAvailabilityAvailabilityPeriod
        public class tipoAvailabilityAvailabilityPeriod
        {
            public tipoAvailabilityAvailabilityPeriod()
            {
                Availability = string.Empty;
            }
    
            [DataMember]
            [XmlAttributeAttribute()]
            public string start
            {
                get;
    
                set;
            }
    
            [DataMember]
            [XmlAttributeAttribute()]
            public string end
            {
                get;
    
                set;
            }
    
            [DataMember]
            public string Availability
            {
                get;
    
                set;
            }
        }
        #endregion
    
        #region tipoAvailabilityRoomTypeRate
        public class tipoAvailabilityRoomTypeRate
        {
            public tipoAvailabilityRoomTypeRate()
            {
                Period = new List<tipoAvailabilityRoomTypeRatePeriod>();
            }
    
            [DataMember]
            [XmlAttributeAttribute()]
            public string id
            {
                get;
    
                set;
            }
    
            [DataMember]
            [XmlElementAttribute("Period", IsNullable = false)]
            public List<tipoAvailabilityRoomTypeRatePeriod> Period
            {
                get;
    
                set;
            }
        }
    
        public class tipoAvailabilityRoomTypeRatePeriod
        {
            public tipoAvailabilityRoomTypeRatePeriod()
            {
                start = string.Empty;
                end = string.Empty;
                Closed = false;
                Price = 0;
                MinStay = 1;
                MaxStay = 1;
                ClosedArrival = false;
                ClosedDeparture = false;
            }
    
            [DataMember]
            [XmlAttributeAttribute()]
            public string start
            {
                get;
    
                set;
            }
    
            [DataMember]
            [XmlAttributeAttribute()]
            public string end
            {
                get;
    
                set;
            }
    
            [DataMember]
            public bool Closed
            {
                get;
    
                set;
            }
    
            [DataMember]
            [XmlIgnore]
            public decimal Price
            {
                get;
    
                set;
            }
    
            [DataMember]
            public int MinStay
            {
                get;
    
                set;
            }
    
            [DataMember]
            public int MaxStay
            {
                get;
    
                set;
            }
    
            [DataMember]
            public bool ClosedArrival
            {
                get;
    
                set;
            }
    
            [DataMember]
            public bool ClosedDeparture
            {
                get;
    
                set;
            }
        }
        #endregion
    }

    Quando richiamo il servizio, la variabile 'xml' che passo ha corretta 5 nodi 'RoomType con il valore id corretto, ma 'AvailabilityPeriod' e 'Rates' sono vuoti.

    Dove sbaglio?

    Grazie mille

    martedì 27 giugno 2017 16:10