2017-05-26 83 views
-2

你好,我得到这个错误:出现InvalidOperationException错误

InvalidOperationException: Unable to resolve service for type 'RegistrationMVC.Model.OurDbContext' while attempting to activate 'RegistrationMVC.Controllers.HomeController'.

我不知道什么可能导致它,可能是一些与依赖关系?

startup.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using Microsoft.AspNetCore.Builder; 
using Microsoft.AspNetCore.Hosting; 
using Microsoft.Extensions.Configuration; 
using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Logging; 
using WebApplicationCore.NetCore.DataAccess; 
using WebApplicationCore.NetCore.BusinessLogic; 
namespace WebApplicationCore.NetCore 
    public class Startup 
    { 
     public Startup(IHostingEnvironment env) 
     { 
      var builder = new ConfigurationBuilder() 
       .SetBasePath(env.ContentRootPath) 
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
       .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) 
       .AddEnvironmentVariables(); 
      Configuration = builder.Build(); 
     } 

    public IConfigurationRoot Configuration { get; } 

    // This method gets called by the runtime. Use this method to add services to the container. 
    public void ConfigureServices(IServiceCollection services) 
    { 
     // Add framework services. 
     services.AddMvc(); 

     services.AddSingleton<IConfigurationRoot>(sp => { return this.Configuration; }); 
     services.AddScoped<IContactDataAccess, ContactDataAccess>(); 
     services.AddScoped<IContactBusinessLogic, ContactBusinessLogic>(); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
     loggerFactory.AddDebug(); 

     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
      app.UseBrowserLink(); 
     } 
     else 
     { 
      app.UseExceptionHandler("/Home/Error"); 
     } 

     app.UseStaticFiles(); 

     app.UseMvc(routes => 
     { 
      routes.MapRoute(
       name: "default", 
       template: "{controller=Home}/{action=Index}/{id?}"); 
     }); 
    } 
} 

HomeController

OurDbContext

+1

您不会在HomeController的构造函数要求的任何位置注册“OurDbContext”。你还应该阅读[问]并确保你的问题中的代码是[mcve]。 – CodeCaster

回答

0

一些容器将自动实例化等OurDbContext类(只要它可以被成功构建)自动因为它是一个类型,可以如果OurDbContext未在依赖容器中定义,则其他人会抛出异常。所以它可以像OurDbContext没有注册一样简单,并且容器按照这种方式工作...

相关问题