I have a server which communicates with client using ASP.NET Web API.
My model in server side is more detailed and client has less data so I use [DataContract]/[DataMember] to match the both.
Here is the model on the server side:
[DataContract]
public class Discussion
{
public Discussion()
{
this.Posts = new HashSet<Post>();
this.Members = new HashSet<UserProfile>();
}
[DataMember]
public int DiscussionId { get; set; }
[DataMember]
public int AuthorId { get; set; }
public int CategoryId { get; set; }
[Required]
[MinLength(4)]
[MaxLength(140)]
[DataMember]
public string DiscussionSubject { get; set; }
[Required]
[MinLength(10)]
[MaxLength(10000)]
[DataMember]
public string DiscussionDescription { get; set; }
[Required]
[DataMember]
public DateTime CreateDate { get; set; }
[Required]
[DataMember]
public DateTime LastChangeDate { get; set; }
[NotMapped]
[DataMember]
public int NumberOfPosts { get { return Posts.Count; } }
[DataMember]
public virtual UserProfile Author { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<UserProfile> Members { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
And this is the client side model:
[DataContract]
public class Discussion
{
[DataMember]
public int DiscussionId { get; set; }
[DataMember]
public int AuthorId { get; set; }
[DataMember]
public string DiscussionSubject { get; set; }
[DataMember]
public string DiscussionDescription { get; set; }
[DataMember]
public DateTime CreateDate { get; set; }
[DataMember]
public DateTime LastChangeDate { get; set; }
[DataMember]
public int NumberOfPosts { get; set; }
[DataMember]
public virtual UserProfile Author { get; set; }
}
Now when I send a post request, I get an Internal Server Error, and that even doesn't reach to the Post method point while I debug
Discussion discussion = new Discussion
{
DiscussionSubject = subject,
DiscussionDescription = description,
};
response = await client.PostAsJsonAsync(requestUri, discussion);
response.EnsureSuccessStatusCode();
I guess the problem is in deserialization because when I change the model to another, it is fine. This will be good to know that when I send a Get
request instead, every thing is OK.
How can I solve this problem, and where can I see what is the internal server error exactly?
EDIT: this is the Post method in the server-side:
public HttpResponseMessage Post(Discussion discussion)
I Expect Deserialization happen automatically here (from received client Discussion
to server Discussion
).