C# 无效操作异常错误

C# 无效操作异常错误,c#,asp.net,asp.net-mvc,asp.net-identity,C#,Asp.net,Asp.net Mvc,Asp.net Identity,你好,我收到了这个错误: InvalidOperationException:无法解析类型的服务 “RegistrationMVC.Model.OurDbContext”正在尝试激活 '注册MVC.Controllers.HomeController' 我不知道是什么原因造成的,可能是有依赖关系的吗 startup.cs using System; using System.Collections.Generic; using System.Linq; using System.Threadin

你好,我收到了这个错误:

InvalidOperationException:无法解析类型的服务 “RegistrationMVC.Model.OurDbContext”正在尝试激活 '注册MVC.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?}");
        });
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Logging;
使用WebApplicationCore.NetCore.DataAccess;
使用WebApplicationCore.NetCore.BusinessLogic;
命名空间WebApplicationCore.NetCore
公营创业
{
公共启动(IHostingEnvironment环境)
{
var builder=new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(“appsettings.json”,可选:true,重载更改:true)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,可选:true)
.AddenEnvironmentVariables();
Configuration=builder.Build();
}
公共IConfigurationRoot配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddMvc();
services.AddSingleton(sp=>{返回this.Configuration;});
services.addScope();
services.addScope();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
loggerFactory.AddDebug();
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseStaticFiles();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
});
}
}


一些容器会自动实例化像OurDbContext这样的类(只要它可以成功构造),因为它是一种可以构造的类型,而其他容器则会在依赖项容器中未定义OurDbContext时抛出异常。因此,这可能很简单,因为没有注册OurDbContext,而容器的行为是这样的…

您不需要在任何地方注册
OurDbContext
,这是HomeController的构造函数所需要的。您还应该阅读并确保问题中的代码是正确的。