【发布时间】:2020-08-18 19:38:48
【问题描述】:
我正在创建一个 wep api,这是当前的结构:
API - The Web API (.net core web api project)
DAL - DbContext and Entities (.net core class library)
DTO - Data Transfert Objects - The classes I send to the client without sensible data (.net core class library)
REPO - Contains de Interfaces and Repositories (.net core class library)
有关信息,我拥有同一个项目的所有内容,并决定拆分为多个类库。
到目前为止我所做的:
- 添加了每个项目之间的引用
- 更新使用
- 已将命名空间名称更改为正确的名称
- 解决为 0 个错误
我认为我的问题与依赖注入有关,因为当我尝试从邮递员或浏览器访问控制器时,会发生此错误:
InvalidOperationException:验证服务描述符“ServiceType:FootballManager.REPO.ILeagueRepository Lifetime:Scoped ImplementationType:FootballManager.REPO.LeagueRepository”时出错:尝试激活“FootballManager”时无法解析“FootballManager.DAL.FootballManagerAPIContext”类型的服务.REPO.LeagueRepository'。
我的 Startup.cs 看起来像这样:
using FootballManager.REPO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace FootballManager.API
{
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.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
});
});
services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddScoped<ILeagueRepository, LeagueRepository>();
services.AddScoped<IMatchRepository, MatchRepository>();
services.AddScoped<IPlayerRepository, PlayerRepository>();
services.AddScoped<IRefereeRepository, RefereeRepository>();
services.AddScoped<ITeamRepository, TeamRepository>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
这是我进行注入的控制器代码:
public class LeaguesController : ControllerBase
{
private readonly ILeagueRepository _repo;
public LeaguesController(ILeagueRepository repo)
{
_repo = repo;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<LeagueDto>>> GetLeagues()
{
return await _repo.GetAll();
}
}
对于我的 DbContext 连接,我直接在 DAL 项目上做了这样的(我不认为问题出在这里):
public partial class FootballManagerAPIContext : DbContext
{
public FootballManagerAPIContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(@"Server =.\SQLEXPRESS; Database = FootballManagerAPI; Trusted_Connection = True;");
}
}
}
在网上和 stackoverflow 上几个小时后,我仍然找不到任何可行的解决方案...
我该如何解决这个错误以及为什么会出现这个错误?谢谢。
【问题讨论】:
标签: c# asp.net-core .net-core dependency-injection asp.net-core-webapi