InvalidOperationException:尝试激活“DocumentController”时无法解析“IDocumentService”类型的服务

Posted

技术标签:

【中文标题】InvalidOperationException:尝试激活“DocumentController”时无法解析“IDocumentService”类型的服务【英文标题】:InvalidOperationException: Unable to resolve service for type 'IDocumentService' while attempting to activate 'DocumentController' 【发布时间】:2021-02-23 18:47:46 【问题描述】:

现在我正在开发 Web api Asp.net 核心,但我对 DI 感到困惑。 当我尝试向我的 api 发送请求时,出现此错误

我使用 Ninject,我认为我对此有疑问 GitLab 和我的项目https://gitlab.com/ValeriiDmytryshyn/sharedocument

启动文件可能有问题

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?");
        );
    

【问题讨论】:

好的对不起我会编辑它 【参考方案1】:

看来您需要为依赖注入注册服务才能解析您的类。

您必须将所有注入的类注册为 Singleton 或 Transient。这是在 startup.cs 中尝试的示例

services.AddTransient<IDocumentService , DocumentService>();

有关详细信息,请参阅文档 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-5.0

【讨论】:

我不知道我需要注册什么我会读码头但现在我试着做一些这样的想法并有这个例外ibb.co/ZNLVBVZ 是的,谢谢,我做了 services.AddTransient(); services.AddTransient(); services.AddTransient();在 Startup 中,并更改了 UnitOfWork 构造函数

以上是关于InvalidOperationException:尝试激活“DocumentController”时无法解析“IDocumentService”类型的服务的主要内容,如果未能解决你的问题,请参考以下文章

InvalidOperationException:未找到名为“Bearer”的 AuthorizationPolicy

为啥这个 OdbcConnection 会抛出 System.InvalidOperationException?

System.InvalidOperationException: '绑定实例已经被关联到监听

使用 PerformanceCounters 时获取 InvalidOperationException [关闭]

System.InvalidOperationException: 'XML 文档 (1, 1) 中存在错误。'

InvalidOperationException:LINQ 表达式“GroupByShaperExpression: