scrapcode
scrapcode

Reputation: 45

ASP.NET5 500 Error when using the repository pattern

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>();
}

Upvotes: 1

Views: 1496

Answers (2)

Par6
Par6

Reputation: 389

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

Upvotes: 1

Craig Wilson
Craig Wilson

Reputation: 1031

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() {...}
}

Upvotes: 3

Related Questions