【问题标题】:ASP.NET5 500 Error when using the repository pattern使用存储库模式时出现 ASP.NET5 500 错误
【发布时间】:2016-12-30 01:25:57
【问题描述】:

编辑 3:我不知道我做了什么来解决这个问题,但我的问题消失了。我认为我发现的最重要的事情,如果你有这个问题,要带走的是,在 ASP.NET 5 MVC 6 中,我们必须为几乎所有东西添加中间件,包括使用 app.UseDeveloperExceptionPage();... 的开发人员错误页面之后我意识到堆栈跟踪可能是指从控制器中引用的其他类,例如存储库和上下文对象,在我的例子中。我把他们弄乱了一点,但我不记得有什么过激的改变。我想我的存储库类的构造函数中可能有错字。

控制器出现 500 内部服务器错误,但它在没有构造函数的情况下显示静态 json 数据就好了。我怀疑内置依赖注入有问题,但我无法弄清楚,因为在构建过程中我没有收到任何错误或警告。

编辑:当导航到http://localhost:5000/api/blogpost/ 时,我在浏览器中绝对看不到任何东西。只是一个空白页。没有错误。没什么。使用 Postman 发送 HTTP 请求,我得到错误代码 500。同样,通过注释掉构造函数,我看到 {"name":"Cameron"},并在浏览器和 Postman 中都得到代码 200 OK。 VS内部没有异常,Output控制台也没有错误。

编辑 2:我找到了中间件 app.UseDeveloperExceptionPage();,它产生了 MissingMethodException: No parameterless constructor defined for this object. - 如果我创建一个无参数的构造函数,它会产生:InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'MyApp.Controllers.BlogPostController'. There should only be one applicable constructor. - 似乎 ASP.NET5 的 dep 注入发生了一些奇怪的事情?

这是我的控制器:

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

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

【问题讨论】:

  • 异常的类型是什么?什么信息?
  • @JoelRamosMichaliszen,也不例外。没有任何错误。即使我在浏览器中加载它,我也什么也看不到。尝试从 Postman 加载时,我看到 500 内部服务器错误。
  • @CodeCaster:感谢您提供启用详细错误的参考,但没有任何变化。除了 Postman 的代码 500 之外,我没有发现任何异常,也没有任何错误。
  • 你看到了什么?您是否正在运行 IE,是否启用了“显示友好的 HTTP 消息”选项?

标签: c# asp.net asp.net-core asp.net-core-mvc


【解决方案1】:

我遇到了同样的问题。我的构造函数没有范围。这个类是公开的,所以我在构造函数之前添加了“public”并且它起作用了!

所以,不工作:

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

和工作:

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

【讨论】:

    【解决方案2】:

    我遇到了一种情况,我正在添加另一个控制器并忘记更新 Startup.cs 页面。

    在 Startup.cs 的 ConfigureServices 下,尝试添加:

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

    假设您在新的控制器构造函数中传入 ISomeOtherRepository

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 2013-05-21
      • 1970-01-01
      相关资源
      最近更新 更多