0

On my local server it returns json but when I publish it to remote server. I get 500 internal server error.

I can't run the hosted site in debug mode, on local host it seems fine. The remote server is IIS8.5 X-AspNet-Version: 4.0.30319 and my local IIS is iis 8

The html response is 500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed.

Api Controller

public class loginapiController : ApiController
{
    [System.Web.Http.HttpGet]
    public UserProjects UserProjects(int id)
    {
        return new UserProjects(id);
    }
}

public class UserProjects
{
    public IEnumerable<Project> Projects = new List<Project>();
    public List<Project> ExistingUserProjects = new List<Project>();
    public int UserId { get; set; }

    public UserProjects(int userId)
    {
        UserId = userId;
        var context = new AuditConnection();
        var existingProjectIds = context.UserProjects.Where(up => up.UserId == userId).Select(p => p.ProjectId);
        foreach (var id in existingProjectIds)
        {
            var project = context.Projects.Where(p => p.ProjectId == id).First();
            ExistingUserProjects.Add(project);
        }

        var proj = context.Projects.ToList();
        Projects = proj.Except(ExistingUserProjects);
    }
}

You can look at the exception using this link http://sentry.landsea.com.au/api/loginapi/UserProjects/4

Hello
  • 17
  • 1
  • 4
  • You can use logging to locate the problem, But my guess is a connection string issue. – VahidNaderi May 13 '14 at 17:11
  • There are other actions which are running fine. Other actions query the same database. So i guess the connection string is fine. I will be looking at the log. Thank you for the suggestion. – Hello May 13 '14 at 17:27
  • Take a look at windows event log to get more detailed information about the error on the hosted server. There 1 million reason for a 500 error. It could be databse, it could be something else – qamar May 13 '14 at 17:27
  • you can see the exception at this following link sentry.landsea.com.au/api/loginapi/UserProjects/4 – Hello May 13 '14 at 17:49

2 Answers2

0

EDIT: given the error

"There is already an open DataReader associated with this Command which must be closed first."

I believe this answer will explain your issue. Your best bet is to let your Reader finish the first command before starting another (you could do this with ToList()), or collapse the query down to a single call (I believe that you could do this by refactoring your queries into one which uses SelectMany()).

Keep in mind that turning on MARS may be covering up a bigger problem, and you should understand it fully before you go that route, especially for a web application with the lifetime scope of your DbContext should be setup as the life of a single http request. Doing that is trivial if you're properly manually disposing of your context or if you're using a dependency injection framework.

Community
  • 1
  • 1
joelmdev
  • 11,083
  • 10
  • 65
  • 89
  • I am using web deploy. I have other actions querying the same database using the same connection string with out any problem. you can see the exception at this following link http://sentry.landsea.com.au/api/loginapi/UserProjects/4 – Hello May 13 '14 at 17:47
0

For me the URL shows a clear error message: "Message":"There is already an open DataReader associated with this Command which must be closed first." You can only have one DataReader open at once, so the code has to be fixed.

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74