2

I'm getting SerializationException when trying to return a list via WebApi get.

Full error message on pastebin

Here's my code:

HomeController.cs:

public IEnumerable<Article> Get() {
    return repo.GetArticles();
}

Repository.cs:

public List<Article> GetArticles() {
    using (var db = new ArticlesContext()) {
        return db.Articles.ToList();
    }
}

ArticlesContext.cs:

public class Article {
    [Key]
    public int ArticleID { get; set; }
    public string Title { get; set; }
    public int UserID { get; set; }

    [ForeignKey("UserID")]
    public virtual User Author { get; set; }
}

public class User {
    [Key]
    public int UserID { get; set; }

    [ForeignKey("ArticleID")]
    public virtual List<Article> Articles { get; set; }
}

public class ArticlesContext : DbContext {
    public DbSet<Article> Articles { get; set; }
    public DbSet<User> Users { get; set; }

    public ArticlesContext() : base("name=conn") {
        //etc
    }
}
1
  • Do you have any additional details regarding your SerializationException? Commented Apr 10, 2016 at 1:26

4 Answers 4

1

Most serialization issues generally stem from either existing circular references that are being pulled or issues related to formatting (i.e. JSON, XML, etc.).

You can check if properties like Proxy Generation and Lazy Loading are enabled and disable them to try to resolve the issue within the constructor for your DbContext:

 this.Configuration.LazyLoadingEnabled = false;
 this.Configuration.ProxyCreationEnabled = false;

You can try adding the following section within your Global.asax file to handle circular references :

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

and the other to explicitly ignore XML serialization :

GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);          
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like you beat me to it with a better solution.
0

What about returning a List instead of IEnumerable

public List<Article> Get() {
    return repo.GetArticles();
}

Comments

0

Based on the error message it appears that there is a problem serializing the proxy class. You could try turning off proxy generation.

Sometimes it is useful to prevent the Entity Framework from creating proxy instances. For example, serializing non-proxy instances is considerably easier than serializing proxy instances. Proxy creation can be turned off by clearing the ProxyCreationEnabled flag. One place you could do this is in the constructor of your context. For example:

public class BloggingContext : DbContext 
{ 
    public BloggingContext() 
    { 
        this.Configuration.ProxyCreationEnabled = false; 
    }  

    public DbSet<Blog> Blogs { get; set; } 
    public DbSet<Post> Posts { get; set; } 
}

https://msdn.microsoft.com/en-us/data/jj592886.aspx

Comments

0

use AsEnumerable() instead ToList()

public IEnumerable<Article> GetArticles() {
    using (var db = new ArticlesContext()) {
        return db.Articles.AsEnumerable();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.