Pakauji Pakau
Pakauji Pakau

Reputation: 49

OBJECT Serialized using XML Serializer, when passed to the service using HTTPPOST, The values of the object are null or empty

This is my WCF REST OperationContract

 [OperationContract(Name = "SaveBasicInfoObjectById")]
     [WebInvoke(Method = "POST",
          ResponseFormat = WebMessageFormat.Xml,
          RequestFormat = WebMessageFormat.Xml,
         BodyStyle = WebMessageBodyStyle.Bare,
         UriTemplate = "Core/SaveBasicInfoObjectById?ID={ID}")]
     List<Response> SaveBasicInfoObjectById(string ID, BasicClass basicClass);

This is my client code

  string ID= "123";

  MyService.BasicClass basicClass = new MyService.BasicClass();


  string sErrorMsg = "";
  string sResponseStatus = "";
string URLString = "http://localhost:59133/MyService.svc/Core/SaveBasicInfoObjectById?ID=" + ID;


basicClass.FirstName = txt_Fname.Text.Trim();
basicClass.Lastname = txt_Lname.Text.Trim();

var paramContent = Serialize(basicClass);

CreateWebRequest(URLString, paramContent.ToString());

This is the Serialize Method

    public static string Serialize<T>(T obj)
    {

        var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
        var ms = new MemoryStream();
        serializer.Serialize(ms, obj);
        string retVal = Encoding.Default.GetString(ms.ToArray());
        ms.Dispose();
        return retVal;

    }

This Is The CreateWebRequest method

    private string CreateWebRequest(string endPoint, string paramContent)
    {
        string result = string.Empty;

        byte[] buffer = Encoding.UTF8.GetBytes(paramContent);
        var myHttpWebRequest = (HttpWebRequest)WebRequest.Create(endPoint);

        myHttpWebRequest.Method = "POST";
        myHttpWebRequest.ContentLength = buffer.Length;
        myHttpWebRequest.ContentType = "text/xml";

        using (var request = myHttpWebRequest.GetRequestStream())
        {
            request.Write(buffer, 0, buffer.Length);
            request.Close();
        }

        var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
        using (var reader = new StreamReader(myHttpWebResponse.GetResponseStream(), Encoding.UTF8))
        {
            result = reader.ReadToEnd();
            myHttpWebResponse.Close();
        }


        return result;
    }

*The problem here that i am facing is the Object is serialized in the following format, and when the breakpoint is hit at the service the object values are empty i.e firstname = "" and lastname = "" *

<?xml version="1.0"?>
 <BasicClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="0">
<FirstName xmlns="http://tempuri.org/">James</FirstName>
<Lastname xmlns="http://tempuri.org/">Kravis</Lastname>
</BasicClass>

Can any one advice why this is happening?

I have now replaced the tempuri namespace against the firstname and lastname element as "" or empty, Now this working and the values from the client are now recieved at the service

Following is the updated code for serialize method

public  string Serialize<T>(T obj)
    {
        //System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
        var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
        var ms = new MemoryStream();
        serializer.Serialize(ms, obj);
        string retVal = Encoding.Default.GetString(ms.ToArray());
        ms.Dispose();
        return retVal.Replace("xmlns=\"http://tempuri.org/\"", "");
    }

** Update to above code **

public  string Serialize<T>(T obj)
    {
        var serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
        var ms = new MemoryStream();
        serializer.Serialize(ms, obj);
        string retVal = Encoding.Default.GetString(ms.ToArray());
        ms.Dispose();
        return retVal;
    }

** Got Rid of the replace and found the way to stop the default namespace being added to the elements **

public class BasicClass
{
    [XmlElement(Namespace="")]//add namespace as ""
    public string Firstname{get;set;}

    [XmlElement(Namespace="")]//add namespace as ""
    public string Lastname{get;set;}
}

By Doing as above i am able to remove the default namespace and no need of replace

<?xml version="1.0"?>
 <BasicClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="0">
<FirstName>James</FirstName>
<Lastname>Kravis</Lastname>
</BasicClass>

Upvotes: 1

Views: 1379

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87228

BY default WCF uses the DataContractSerializer as its serializer, not the XmlSerializer. They produce (and expect) different formats, so you should change either the client (serialize your object using the DataContractSerializer instead of the XmlSerializer), or the service (apply the [XmlSerializerFormat] attribute to either the operation or the service contract to have WCF use that serializer).

You also shouldn't replace the "xmlns" declaration (last line in your Serialize<T> method), since by doing that you're changing the semantics of the XML produced.

Upvotes: 1

Related Questions