C# InvalidOperationException:无法解析类型为';IDocumentService';试图激活';文档控制器';
现在我正在开发Web api Asp.net核心,并且我已经用DI实现了truble。 当我尝试向我的api发送请求时,出现了以下错误 我用了Ninject,我想我在这方面有问题 GitLab与我的项目 启动文件可能有问题C# InvalidOperationException:无法解析类型为';IDocumentService';试图激活';文档控制器';,c#,api,asp.net-web-api,C#,Api,Asp.net Web Api,现在我正在开发Web api Asp.net核心,并且我已经用DI实现了truble。 当我尝试向我的api发送请求时,出现了以下错误 我用了Ninject,我想我在这方面有问题 GitLab与我的项目 启动文件可能有问题 public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IC
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DocumentContext>
(options => options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));
services.AddControllers();
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new AutomapperProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
services.AddMvc();
NinjectModule ninjectModule = new NinjModule();
NinjectModule serviceModule = new ServiceModule(Configuration["ConnectionStrings:DefaultConnection"]);
var kernel = new StandardKernel(ninjectModule, serviceModule);
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext
(options=>options.UseSqlServer(配置[“ConnectionString:DefaultConnection”]);
services.AddControllers();
var mappingConfig=新的MapperConfiguration(mc=>
{
mc.AddProfile(新的AutomapperProfile());
});
IMapper mapper=mappingConfig.CreateMapper();
服务。AddSingleton(映射器);
services.AddMvc();
NinjectModule NinjectModule=新的NinjModule();
NinjectModule serviceModule=新的serviceModule(配置[“ConnectionString:DefaultConnection”]);
var kernel=新的标准内核(ninject模块、serviceModel);
SetResolver(新的NinjectDependencyResolver(内核));
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
app.UseDeveloperExceptionPage();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapControllerRoute(
名称:“默认”,
模式:“{controller=Home}/{action=Index}/{id?}”);
});
}
}
看起来您需要为依赖项注入注册服务来解析类
您必须将所有注入的类注册为Singleton或Transient。这是一个在startup.cs中尝试的示例
services.AddTransient<IDocumentService , DocumentService>();
services.AddTransient();
有关更多信息,请参阅文档
看起来您需要注册服务以进行依赖项注入来解析类 您必须将所有注入的类注册为Singleton或Transient。这是一个在startup.cs中尝试的示例
services.AddTransient<IDocumentService , DocumentService>();
services.AddTransient();
有关更多信息,请参阅文档
我不是舒尔,我需要注册什么我会读dock,但现在我试着做一些类似的事情,但有一个例外是的,谢谢,我提供了服务。AddTransient();services.AddTransient();services.AddTransient();在启动过程中,更改了UnitOfWork Constructor我不是舒尔我需要注册什么我会读dock但现在我试着做一些类似的事情并且有这个例外是的,谢谢,我提供了服务。AddTransient();services.AddTransient();services.AddTransient();在启动中,并更改了UnitOfWork构造函数OK抱歉我将编辑它OK抱歉我将编辑它