繁体   English   中英

内部服务器错误500 Asp.net WebApi尝试与实体进行DI

[英]Internal Server Error 500 Asp.net WebApi trying to DI with Entity

我正在构建一个ASP.net WebApi,并尝试与Entity一起使用。 我正在遵循本指南。

ASP.NET Web API 2(C#)入门

我正在使用Fiddler收到500个内部服务器错误。 JSON异常消息指出ExceptionMessage=An error occurred when trying to create a controller of type 'LocationsController'. Make sure that the controller has a parameterless public constructor. ExceptionMessage=An error occurred when trying to create a controller of type 'LocationsController'. Make sure that the controller has a parameterless public constructor.

这是Controller.cs

[RoutePrefix("api/Locations")]
public class LocationsController : ApiController
{
    // GET api/<controller>
    private IlocationsRepository LocationsRepo;

    public LocationsController(IlocationsRepository _repo)
    {
        if (_repo == null) { throw new ArgumentNullException("_repo"); }
        LocationsRepo = _repo;
    }

    [HttpGet]
    [Route("")]
    public IEnumerable<Location> GetAll()
    {
       return LocationsRepo.GetAll();
    }
}

我不能使用无参数的公共构造函数,因为我需要使用为Locations创建的数据库存储库。 我通过执行以下操作验证了问题在于IlocationsRepository

当我不使用任何参数替换LocationsController构造函数,并在控制器内声明List<Location> ,请使用伪数据。 我收到200所有正确的json数据。

这是Global.asax.cs文件的开始

 public class Global : System.Web.HttpApplication
 {
    protected void Application_Start(object sender, EventArgs e)
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }

    protected void Session_Start(object sender, EventArgs e)
    {

    }
 }

似乎我需要在全局中进行依赖项注入,但是这些指南都没有关于这部分的任何信息。

为了后代,这里是ContextDB CS

public class WebServerContext : DbContext
{
    public WebServerContext() : base("WebServerContext") {}
    public DbSet<Order> dOrders { get; set; }
    public DbSet<Location> dLocations { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}

有关其他后代,请参阅位置信息库。

public class LocationsRepository : IlocationsRepository
{
    private z_Data.WebServerContext db = new z_Data.WebServerContext();

    public void Add(Location item)
    {
        db.dLocations.Add(item);
    }
    public IEnumerable<Location> GetAll()
    {
        return db.dLocations;
    }
}

根据MSDN上的Dependency Injection for Web Api教程 ,您缺少向Web Api注册依赖项解析器(实现System.Web.Http.IDependencyResolver的具体类)的那一行。 它就像您的DI容器和Web Api之间的桥梁,因此可以解决您的构造函数依赖关系。

public static void Register(HttpConfiguration config)
{
    var container = new UnityContainer();
    container.RegisterType<IProductRepository, ProductRepository>(new HierarchicalLifetimeManager());
    config.DependencyResolver = new UnityResolver(container); // <- You need this

    // Other Web API configuration not shown.
}

当然,这假设您正在使用Unity。 如果不是,则应该使用DI容器随附的DependencyResolver或实现自己的容器。

注意:对于某些DI容器,您还需要显式注册所有控制器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM