您好我收到此错误:
InvalidOperationException:无法解析类型的服务 尝试激活时“RegistrationMVC.Model.OurDbContext” '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?}");
});
}
}
答案 0 :(得分:0)
某些容器会自动实例化类似OurDbContext的类(只要它可以成功构建),因为它是一个可以构造的类型,而其他容器会在依赖项容器中未定义OurDbContext时抛出异常。所以它可能就像没有注册OurDbContext那样简单,容器就是这样......