Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top