0

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: https://i.stack.imgur.com/1SDEF.png

EDIT: Full error (screenshot): https://i.stack.imgur.com/4mFpz.png

Thanks in advance.

Sn0wBlind
  • 35
  • 4
  • 2
    Can you post more information on the error received? Screenshot in browser? Inner exception? Stack Trace? – Chris Bohatka Apr 03 '15 at 12:08
  • Give us exception which occurs in `ctx.Users.ToList();`. You snapshot doesn't help at all. – Artiom Apr 03 '15 at 12:11
  • It doesn't give an exception, just shows this page – Sn0wBlind Apr 03 '15 at 12:12
  • @Sn0wBlind set breakpoint at the point where users are returned. And see if data is present – Artiom Apr 03 '15 at 12:14
  • @Sn0wBlind try to enable exceptions https://msdn.microsoft.com/en-us/library/038tzxdw.aspx – Artiom Apr 03 '15 at 12:19
  • @Artiom Here is a screenshot of the returned data: http://i.imgur.com/OA8MM0l.png – Sn0wBlind Apr 03 '15 at 12:22
  • @Artiom all exceptions are enabled – Sn0wBlind Apr 03 '15 at 12:26
  • Please see ["Should questions include “tags” in their titles?"](http://meta.stackexchange.com/questions/19190/should-questions-include-tags-in-their-titles), where the consensus is "no, they should not"! –  Apr 03 '15 at 12:26
  • possible duplicate of [Unloaded "eager-loaded" properties causing issues when returning json'd data](http://stackoverflow.com/questions/23095393/unloaded-eager-loaded-properties-causing-issues-when-returning-jsond-data) – Artiom Apr 03 '15 at 14:16

3 Answers3

2

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.

Kristof
  • 3,267
  • 1
  • 20
  • 30
  • @Return only plain model to client – Artiom Apr 03 '15 at 14:17
  • 1
    i agree with Artiom that mapping your entities would be a good idea. But since i don't wanna force my coding style and it seems like you don't want to map(since you didn't accept his answer) i provided another work arround in my answer. – Kristof Apr 03 '15 at 14:21
  • @Kristof The ProxyCreationEnabled = false indeed fixed the problem I was facing – Sn0wBlind Apr 03 '15 at 15:23
  • @Sn0wBlind take into account that you send to client all personal data of users. – Artiom Apr 04 '15 at 12:35
  • @Artiom Yes I know, it was more of a test to see what was going wrong. Thanks for the heads-up though ;) – Sn0wBlind Apr 04 '15 at 12:38
1

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.

Artiom
  • 7,694
  • 3
  • 38
  • 45
0

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;
}
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62