2017-05-26 7 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をどこにも登録しません。また、[ask]を読んで、あなたの質問のコード_が[mcve]であることを確認する必要があります。 – CodeCaster

答えて

0

一部のコンテナは自動的に、自動的に(限り、それがうまく構築することができるよう)OurDbContextのようなクラスをインスタンス化しますOurDbContextが依存関係コンテナで定義されていない場合、例外をスローします。だから、OurDbContextが登録されておらず、コンテナがこのように動作するのと同じくらいシンプルになる可能性があります。

関連する問題