Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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).

share|improve this question
 
Why don't you try serializing one of the types and then deserializing it as the other and see what error you get? This problem is fairly independent of web api. –  Darrel Miller Apr 12 at 12:29
 
Hi @Darrel, I think serialization and deserialization happens automatically using DataContract, DataMember. Am I right?.. I have added more details –  Blazi Apr 12 at 13:19
 
if you are not running the application locally, can you try enabling detailed error logging by doing "config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always" –  Kiran Challa Apr 12 at 15:26
 
Hi @KiranChalla , sorrily I run it locally, but if some information is needed I can get it –  Blazi Apr 12 at 20:11
 
Is this a SelfHost application or a webhost one? –  Kiran Challa Apr 12 at 20:12
show 1 more comment

1 Answer

up vote 0 down vote accepted

I suspect that you have circular references in your object graph which obviously cannot be JSON serialized. So your HTTP request is never sent to the server because the client doesn't know how to JSON serialize your model.

This could happen for example if your UserProfile model has reference to the Discussion model. And since the Discussion model has reference to the UserProfile model you've got a problem.

To solve this issue you should use view models and not expose your domain models in your Web API.

share|improve this answer
 
Thank you @Darin very much. I looked and there is no circular references in the client and UserProfile has just a Username property. but at the other hand, in server side, there maybe occur but I avoided it by not decorating it with [DataMember]. –  Blazi Apr 12 at 14:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.