Question

I have a WCF (ajax enabled) web service, and have the following method, that accepts a object called TypeRequest.

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/XML/GetTypes", 
BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]

XElement XMLGetTypes(TypeRequest obj) 
{  
  //do something 
}

The TypeRequest object is defined as;

[DataContract(Namespace = "")]
public class TypeRequest 
{
    [Required]
    [DataMember]
    public long Id{ get; set; }

    [Required]
    [DataMember]
    public Account Account { get; set; }
}

Then my child Account class is as follows;

[DataContract(Namespace = "")]
public class Account
{
    /// <summary>
    /// Email address
    /// </summary>
    [DataMember]
    [Required]
    [StringLength(175)]        
    public string Email { get; set; }

    /// <summary>
    /// Plain text password
    /// </summary>
    [DataMember]
    [Required]
    [StringLength(16, MinimumLength = 8)]
    public string Password { get; set; }
}

The problem I have is that if I make the call passing the following sample Xml

<TypeRequest>
<Id>1</Id>
<Account>
<Email>myemail@domain.com</Email>
<Password>12345</Password>
</Account>
</TypeRequest>

to the method XMLGetTypes(TypeRequest obj), I can view the obj and Id has the correct value, but the Account object is null

Why is this, what am I doing wrong as I can't seem to figure it out. Do I need to provide anymore information to get some help ?

Further to my previous information, I have added a method for accepting JSON requests like

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/JSON/GetTypes", 
BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]

string JSONGetTypes(TypeRequest obj) 
{  
  //do something 
}

Calling this with the JSON as

{
  "TypeRequest": {
    "Id": "1",
    "Account": {
      "Email": "myemail@domain.com",
      "Password": "12345"
    }
  }
}

I can view the TypeRequest obj, and can view all the correct data that was posted. Therefore, why isn't the Xml post working ?!?

Was it helpful?

Solution 2

Adding

[XmlSerializerFormat] to my method solved the problem. For example

[OperationContract]
[XmlSerializerFormat]
[WebInvoke(Method = "POST", UriTemplate = "/XML/GetTypes", 
BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]

XElement XMLGetTypes(TypeRequest obj) 
{  
  //do something 
}

OTHER TIPS

Try changing your xml to this?

<TypeRequest>
  <Id>1</Id>
  <Account>
    <Email>myemail@domain.com</Email>
    <Password>12345</Password>
</TypeRequest>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top