C# 对于我的ASP.NET核心MVVM Blazor应用程序,依赖项注入无法正常工作

C# 对于我的ASP.NET核心MVVM Blazor应用程序,依赖项注入无法正常工作,c#,asp.net-core,dependency-injection,blazor,repository-pattern,C#,Asp.net Core,Dependency Injection,Blazor,Repository Pattern,我在尝试应用依赖注入时遇到问题。经过大量研究,查看YouTube上的各种视频并回答堆栈溢出问题,我的ITaskRepository不断返回null,而不是我的存储库实例。看看我的代码,似乎我已经添加了所有正确的东西,以使依赖注入工作 我的基本存储库接口 使用portfolio_backend.Data.Base; 使用System.Collections.Generic; 命名空间公文包_backend.Business.Repositories.Base { 公共接口IBaseReposito

我在尝试应用依赖注入时遇到问题。经过大量研究,查看YouTube上的各种视频并回答堆栈溢出问题,我的ITaskRepository不断返回null,而不是我的存储库实例。看看我的代码,似乎我已经添加了所有正确的东西,以使依赖注入工作

我的基本存储库接口

使用portfolio_backend.Data.Base;
使用System.Collections.Generic;
命名空间公文包_backend.Business.Repositories.Base
{
公共接口IBaseRepository,其中tenty:BaseModel
{
void-Add(tenty模型);
void-Delete(tenty模型);
bool存在(int-Id);
TEntity-Get(int-Id);
IEnumerable GetAll();
void更新(int-Id,TEntity模型);
}
}
我的BaseRepository类


使用Microsoft.EntityFrameworkCore.Internal;
使用portfolio_backend.Data;
使用portfolio_backend.Data.Base;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间公文包_backend.Business.Repositories.Base
{
公共类BaseRepository:IBaseRepository其中tenty:BaseModel
{
受保护的PortfolioContext\u上下文;
公共基础存储库(PortfolioContext上下文)
{
_上下文=上下文;
}
公共空间添加(可伸缩性模型)
{
如果(!存在(model.Id))
{
_context.Set().Add(model);
_SaveChanges();
}
}
公共无效删除(可能性模型)
{
如果(存在(型号Id))
{
_context.Set().Remove(model);
_SaveChanges();
}
}
公共bool存在(int-Id)
{
返回_context.Set().Any(model=>model.Id==Id);
}
公共TEntity Get(int-Id)
{
返回_context.Set().FirstOrDefault(model=>model.Id==Id);
}
公共IEnumerable GetAll()
{
返回_context.Set().ToList();
}
公共无效更新(int Id,TEntity模型)
{
var modelToFind=Get(Id);
_context.Set().Update(modelToFind);
_SaveChanges();
}
}
}
我的ITaskRepository接口

使用公文包_backend.Business.Repositories.Base;
使用portfolio_backend.Data;
使用System.Collections.Generic;
命名空间公文包_backend.Business.Repositories
{
公共接口ITaskRepository:iBaserRepository
{
IEnumerable GetTaskByProjects(int projectd);
}
}
TaskRepository实现

使用公文包_backend.Business.Repositories.Base;
使用portfolio_backend.Data;
使用System.Collections.Generic;
使用System.Linq;
命名空间公文包_backend.Business.Repositories
{
公共类TaskRepository:BaseRepository、ITaskRepository
{
公共任务存储库(PortfolioContext上下文):基本(上下文)
{
}
公共IEnumerable GetTaskByProjects(int ProjectId)
{
返回_context.Tasks.OrderByDescending(task=>task.Project.Id==ProjectId).ToList();
}
}
}
我的创业班:

使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.EntityFrameworkCore;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Hosting;
使用公文包_backend.Business.Repositories.Base;
使用公文包_backend.Business.Repositories;
使用portfolio_backend.Data;
使用Blazorise;
使用Blazorise.Bootstrap;
使用Blazorise.Icons.font;
命名空间公文包_backend.Presentation
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
services.AddDbContext(选项=>
options.UseMySQL(Configuration.GetConnectionString(“公文包”);
addScope(typeof(IBaseRepository)、typeof(BaseRepository));
services.addScope();
services.AddBlazorise(选项=>{
options.ChangeTextOnKeyPress=true;})
.AddBootstrapProviders()
.AddFontAwesomeIcons();
services.AddRazorPages();
AddServerSideBlazor();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.ApplicationServices
.UseBootstrapProviders()的
.UseFontAwesomeIcons();
app.UseEndpoints(端点=>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage(“/_主机”);
});
}
}
}
我尝试将依赖项注入应用于以下两个类:

Tasks.razor.cs(用于
System.NullReferenceException: 'Object reference not set to an instance of an object.'

MissingMethodException: No parameterless constructor defined for type 'portfolio_backend.Presentation.Pages.Tasks'.

services.AddScoped<ITaskRepository, TaskRepository>();

or 

services.AddTransient<ITaskRepository, TaskRepository>();
        services.AddScoped<Tasks>();
using Microsoft.AspNetCore.Components;