Returning data error and reflection exception

Topics: Web Api
Dec 22, 2011 at 4:54 PM

hi everybody,

I am new to Web Api, so sorry if the questions I am going to do are silly.

First question.

I have the following class:

 

public class UserDTO
    {
        public int Id { get; set; }
        public string Email { get; set; }
        public int EmailVerified { get; set; }
        public string EncryptedPassword { get; set; }
        public string PasswordSalt { get; set; }
        public string ResetPasswordToken { get; set; }
        public DateTime LastSignInAt { get; set; }
        public string LastSignInIp { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
    }

 

Now, I have:

 

[WebGet(UriTemplate = "")]
        public List<UserDTO> Get()
        {
            List<UserDTO> list = new List<UserDTO>();
            list.Add(new UserDTO { Id = 1, Email = "test@hi.com" });
            return new List<UserDTO>(list);
        }

 

Using the Web Api test client, I get:

 

Response 200/OK
Headers
Date: Thu, 22 Dec 2011 16:40:29 GMT
Content-Length: 347
Server: Microsoft-HTTPAPI/2.0
Content-Type: application/xml; charset=utf-8
Body
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfUserDTO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UserDTO>
    <Id>1</Id>
    <Email>test@hi.com</Email>
    <EmailVerified>0</EmailVerified>
    <LastSignInAt>0001-01-01T00:00:00</LastSignInAt>
    <CreatedAt>0001-01-01T00:00:00</CreatedAt>
  </UserDTO>
</ArrayOfUserDTO>

 

If I change to Accept:text/json I get:

Response 0/
 

What is the reason?

Second question

I'm using NHibernate to abstract data access. So I have the following class:

public partial class User {
        public User() {
            Profiles = new List<Profile>();
        }
        public virtual int Id { get; set; }
        public virtual IEnumerable<Profile> Profiles { get; set; }
        public virtual string Email { get; set; }
        public virtual int EmailVerified { get; set; }
        public virtual string EncryptedPassword { get; set; }
        public virtual string PasswordSalt { get; set; }
        public virtual string ResetPasswordToken { get; set; }
        public virtual DateTime LastSignInAt { get; set; }
        public virtual string LastSignInIp { get; set; }
        public virtual DateTime CreatedAt { get; set; }
        public virtual byte[] UpdatedAt { get; set; }
    }
But I get an exception on httpservicehost.Open() telling me there was an error reflecting User object. That is the reason I am going to map User to UserDTO. Do you know a better solution?