开发者

HttpWebREquest XMLSerialzation POST request sending partially null properties to REST service

开发者 https://www.devze.com 2023-04-10 11:07 出处:网络
I implemented a method which makes POST request to my REST services. It serializes the passed object using XML serialization, but at server side half of the properties become null, and first 3 propert

I implemented a method which makes POST request to my REST services. It serializes the passed object using XML serialization, but at server side half of the properties become null, and first 3 properties retain values.

I checked all class property well decorated with XMLElement attribute. Calling this method with smaller size (three property ) works fine, but when I pass class-object with 7/8 property it causes above mentioned problem. Here is my method: (This code is being written for .NET Compact framework 3.5, though I copy paste the same code to .NET 4.0 class library project, and it is giving me same error. So Compact Framework doesn't seem to be an issue) .

  public static  object Post<T>(string uri, T paramObj)
    { 
        string baseURI = InfraHelper.BaseURI;

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseURI + uri);

        req.Method = "POST";
        req.ContentType开发者_Python百科 = "application/xml; charset=utf-8";
        req.Timeout = 30000;
        req.AllowWriteStreamBuffering = true;

        Encoding enc = new UTF8Encoding(false);

        using (System.IO.StreamWriter sw = new System.IO.StreamWriter(req.GetRequestStream(), enc))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T), "http://www.123insight.com/InsightHandHeldDeviceSvc");                

            serializer.Serialize(sw, paramObj);
            sw.Flush();                
        }

        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

        // Retrieve response stream and wrap in StreamReader
        string strResp = string.Empty;
        using (Stream respStream = resp.GetResponseStream())
        {
            StreamReader rdr = new StreamReader(respStream);
            strResp = rdr.ReadToEnd();
            rdr.Close();
        }

        XElement xDoc = XElement.Parse(strResp);
        XNamespace ns = xDoc.Name.Namespace;

        var node = from compileItem in xDoc.Elements(ns + "Value")
                   select compileItem.Value;


        return node.First().ToString();

    }

My Service method is well-decorated

 [OperationContract]
        [WebInvoke(Method = "POST",
            UriTemplate = "AdjustStock",
            RequestFormat = WebMessageFormat.Xml,
           ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Bare)]
        public PostResponse AdjustStock(AdjustStockRequestDTO adjStockDTO)
        {

Many Thanks for any help friends, my whole day wasted here :( I also tried with specifying req.ContentLength = length; to figure out the length I used following method:-

Encoding enc = new UTF8Encoding(false);
            XmlSerializer serializer1 = new XmlSerializer(typeof(T), "http://www.xxx.com/xxx");    
            StringWriter sw1 = new StringWriter();
            serializer1.Serialize(sw1, paramObj);
            string data = sw1.ToString();
            sw1.Close();

            byte[] byteArray = enc.GetBytes(data);
            int length = byteArray.Length;

rest of the code is same as above. Now it is failing with timeout exception, and call is not reaching to server..


WCF REST usage of XML has some De-serialization issues. i switched to JSON. for .NET CF 3.5 I am using solution from http://json.codeplex.com/ (3.5 release for CF) and it is working fine for me.

Following Thread help me to go Json way:- Null values for object properties de-serialized by WCF

Not sure why such obvious XML- Desalinization should have such issue on WCF-REST service side. Also this issue was not CF specific, as REST svc was hosted at server, and I tried calling using same method with normal WinForm client. If anybody has better answer...meanwhile JSON rocks


You need to declare Order for DataMember attributes in DataContract. This is necessary if your client is sending xml request. for example

  [Serializable]
  [DataContract(Namespace = Utils.DataNamespace)]
  public class Detail 
  {
    [DataMember(Order = 1)] 
    public string LineNbr { get; set; }

    [DataMember(Order = 2)]
    public string UPC { get; set; }

    [DataMember(Order = 3)] 
    public string SkuNbr { get; set; }
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消