0
votes

I'm building an N-tier application which has to send JSON data, which is read from SQL Server 2012 through Enity Framework.

When I try to request a collection of users I get an "An error has occurred" page. It works with hardcoded data.

This is my code:

public IEnumerable<User> Get()
{
    IUserManager userManager = new UserManager();
    return userManager.GetUsers();
}


public IEnumerable<User> GetUsers()
{
    return repo.ReadUsers();
}

public IEnumerable<User> ReadUsers()
{
    IEnumerable<User> users = ctx.Users.ToList();
    return users;
}

"ctx" is a reference to a DbContext-object.

EDIT: This works:

public IEnumerable<User> Get()
{
    IList<User> users = new List<User>();
    users.Add(new User() { FirstName = "TestPerson1" });
    users.Add(new User() { FirstName = "TestPerson2" });

    return users;
}

Browser screenshot: http://i.imgur.com/zqG0qe0.png

EDIT: Full error (screenshot): http://i.imgur.com/dt48tRG.png

Thanks in advance.

3
Can you post more information on the error received? Screenshot in browser? Inner exception? Stack Trace? - Chris Bohatka
Give us exception which occurs in ctx.Users.ToList();. You snapshot doesn't help at all. - Artiom
It doesn't give an exception, just shows this page - Sn0wBlind
@Sn0wBlind set breakpoint at the point where users are returned. And see if data is present - Artiom
@Sn0wBlind try to enable exceptions msdn.microsoft.com/en-us/library/038tzxdw.aspx - Artiom

3 Answers

2
votes

If your website returns internal error and no call stack you are not seeing the full exception(which makes it kind of hard to exactly point out your problem).
So first of all to get to the actual exception with call stack you have 2 methods.

  • Debug the website : start the website locally or attach your debugger to a locally running website. While stepping through the code the debugger will stop when it hits an exception and you'll see the exception details then.
  • Disable custom errors : IIS wants to protect your internal workings so standard behavior is not to show full exceptions. To disable this behavior edit your web.config and add the xml node under

After you get the actual exception please update your question with call stack & the real internal server error. My guess is that you have a serialization issue, maybe a circular reference of some sort. You're fix would be to either make a simpel viewModel(and not return the entity directly) or add serialization settings(json.net support circular references for example).

Edit
As suspected the serialization is giving you a hard time. The cause is the proxy creation used by the lazy loading. You can disable the proxy creation with the following code(make note that this also disables lazy loading).

public IEnumerable<User> ReadUsers()
{
    ctx.Configuration.ProxyCreationEnabled = false;
    IEnumerable<User> users = ctx.Users.ToList();
    return users;
}

if this works you might consider disabling proxy creation during the context initialization.

1
votes

Try to return plain model, smth like

var logins = ctx.Users.Select(user => new FlatUserModel
                                    {
                                        Id = user.Id,
                                        Name = user.UserName,
                                        Email = user.Email
                                    })
                                    .ToArray();
return logins;

Also look in browser what do you get in response.

0
votes

As it works with hard coded data there is possibility of lazyloading enabled on context.

If so disable lazy loading in the context or like this so that serialization to JSON does not load the entities from database.

public IEnumerable<User> ReadUsers()
{
    ctx.Configuration.LazyLoadingEnabled = false;
    IEnumerable<User> users = ctx.Users.ToList();
    return users;
}