1

Edit 3: I don't know what I did to fix this, but my problem went away. I think the most important things I found, and to take away if you have this issue is that in ASP.NET 5 MVC 6 we have to add middleware for almost everything, including the developer error pages using app.UseDeveloperExceptionPage();... after that I realized that the stack trace might be referring to other classes referenced from within the controller such as the repository and the context object, in my case. I messed with them a bit but I don't remember changing anything drastic. I think I may have had a typo in the constructor of my repository class.

I'm getting a 500 Internal Server Error with the controller, but it displays the static json data without the constructor just fine. I suspect there is something going wrong with the built-in dependency injection but I can't figure it out as I'm not getting any errors or warnings during build.

Edit: I see absolutely nothing in browser when navigationg to http://localhost:5000/api/blogpost/. Just a blank page. No Error. No nothing. Using Postman to send the HTTP request, I get error code 500. Again, by commenting out the constructor I see {"name":"Cameron"}, and get code 200 OK in both browser and Postman. There are no exceptions within VS, and there are no errors in the Output console either.

Edit 2: I found the middleware app.UseDeveloperExceptionPage(); and it produces MissingMethodException: No parameterless constructor defined for this object. - If I make a parameterless constructor, it produces: InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'MyApp.Controllers.BlogPostController'. There should only be one applicable constructor. - It seems like something whacky is going on with ASP.NET5's dep injection?

Here's my controller:

using MyApp.Data.Repository;
using MyApp.Models;
using Microsoft.AspNet.Mvc;
using System;

    namespace MyApp.Controllers
    {
        [Route("api/[controller]")]
        public class BlogPostController : Controller
        {
            // If I comment out this var and the constructor this works.
            private IBlogPostRepository _repository;

            // If I leave them here, I get a 500 Internal Server Error.
            // If I set a breakpoint here, it never fires.
            // If I create a constructor that takes zero args, it never fires.
            // I do not get any errors.
            public BlogPostController(IBlogPostRepository repository)
            {
                _repository = repository;
            }

            [HttpGet]
            public JsonResult Get()
            {
                return Json(new { name = "Cameron" });
            }
        }
    }

Here is Startup.cs ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
    // MVC 6
    services.AddMvc();

    // EntityFramework 7
    services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<AppDbContext>(options =>
        {
            // Will use the last Data:DefaultConnection:ConnectionString
            // that was loaded from the config files in the constructor.
            options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]);
        });

    // Injections
    services.AddTransient<AppDbContextSeedData>();
    services.AddScoped<IBlogPostRepository, BlogPostRepository>();
}
scrapcode
  • 45
  • 8
  • What is the type of the exception? and what message? – Joel R Michaliszen Jan 02 '16 at 01:02
  • @JoelRamosMichaliszen, there is no exception. No error of any kind. Even when I load it in my browser, I see nothing. I see the 500 internal server error when trying to load from Postman. – scrapcode Jan 02 '16 at 01:13
  • 1
    http://stackoverflow.com/questions/5385714/deploying-website-500-internal-server-error – CodeCaster Jan 02 '16 at 01:16
  • @CodeCaster: thanks for the reference to enable detailed errors, but there was no change. I get no exceptions, no errors of any kind, except for the code 500 from Postman. – scrapcode Jan 02 '16 at 01:28
  • What _do_ you see? Are you running IE and is the option "Show friendly HTTP messages" enabled by any chance? – CodeCaster Jan 02 '16 at 01:29
  • @CodeCaster I edited my original response as that might clarify to everyone that I really do see absolutely nothing. Also, I'm running Chrome, Firefox and Postman HTTP request sender. – scrapcode Jan 02 '16 at 01:53
  • some additional info about the error message would be useful, either via the inspector - see the HTTP response part from the browsers inspector, https://developers.google.com/web/tools/chrome-devtools/profile/network-performance/resource-loading , or maybe there's an error message while debugging? – monkeyhouse Jan 02 '16 at 02:00
  • we know the http500 response has nothing visible on screen, but you can usually dig in a bit using browser tools or the debugger to see the full response returned – monkeyhouse Jan 02 '16 at 02:02
  • @CodeCaster @user1778606 -- Found `app.UserDeveloperExceptionPage() and posted the results in my original. – scrapcode Jan 02 '16 at 02:14

2 Answers2

3

I had this same problem. My constructor was did not have a scope. The class was public, so I added "public" before the constructor and it worked!

So, NOT WORKING:

public class VisualizationsController : Controller
{
VisualizationsController() {...}
}

And WORKING:

public class VisualizationsController : Controller
{
public VisualizationsController() {...}
}
Craig Wilson
  • 921
  • 6
  • 6
1

I came across a situation where I was adding another controller and forgot to update the Startup.cs page.

In Startup.cs, under the ConfigureServices, try adding:

services.AddSingleton<ISomeRepository, SomeRepository>();
services.AddSingleton<ISomeOtherRepository, SomeOtherRepository>();

Assuming you're passing in ISomeOtherRepository in your new controller constructor

Par6
  • 389
  • 1
  • 10
  • 20