繁体   English   中英

使用.Net的系统聚合异常

[英]System Aggregate Exception using .Net

我正在开发一个 mvc .net 应用程序。 当我运行dotnet build时,解决方案正常构建,没有错误。 但是,当我运行解决方案以使用 swagger 显示 api 时,它会引发系统聚合异常并且运行失败。 在我的 Program.cs 文件中的某个部分引发了异常。 Program.cs 文件如下所示:

using Example.Api.Data;
using Example.Services;
using Example.Services.Interfaces;
using Example.Core;
using Example.Core.Interfaces;
using Example.Core.Repositories;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerUI;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddAutoMapper(typeof(StartupBase));
builder.Services.AddControllersWithViews();
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddTransient<IApplicantService, ApplicantService>();
builder.Services.AddTransient<IApplicantSurveyChoicesService, ApplicantSurveyChoicesService>();
builder.Services.AddTransient<IApplicantSurveyService, ApplicantSurveyService>();
builder.Services.AddTransient<IChoiceService,ChoiceService>();
//I basically did add transient for everyone of my services 
builder.Services.AddSwaggerGen();
var app = builder.Build(); //this is where the exception is being thrown

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
    /*app.UseSwagger();
    app.UseSwaggerUI();*/
}
else
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}
    app.UseSwagger();
    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
        options.RoutePrefix = string.Empty;
    });

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();

app.Run(); 

它引发以下异常:Microsoft.Extensions.DependencyInjection.dll 消息中的“System.AggregateException”=无法构造某些服务(验证服务描述符“ServiceType:Example.Services.Interfaces.IApplicantService Lifetime:Transient ImplementationType”时出错:Example.Services.ApplicantService':尝试激活'Example.Services.ApplicantService'时无法解析类型'Example.Core.Interfaces.IUnitOfWork'的服务。)并且每次调用AddTransient时都会出现错误我相当dotnet 框架的新手,我仍然是初学者,非常感谢您的帮助!

IUnitOfWork 文件包含以下内容:

    public interface IUnitOfWork : IDisposable
    {
        IApplicantRepository Applicants { get; }

        IApplicantSurveyChoicesRepository ApplicantSurveyChoices { get; }

        IApplicantSurveyRepository ApplicantSurveys { get; }

        IChoiceRepository Choices{ get; }

        Task<int> CommitAsync();
    }

UnitOfWork class 只是接口的实现,如下所示:

  public UnitOfWork(DbContext context)
        {
            this._context = context;
        }
        public UnitOfWork(DbContext context, DbContext context1, IHostingEnvironment _environment, IHttpContextAccessor httpContextAccessor)
        {
            this._context = context;
            this._environment = _environment;
            _httpContextAccessor = httpContextAccessor;
        }

        public IApplicantRepository Applicants => _applicantRepository = _applicantRepository ?? new ApplicantRepository(_context, Config, _httpContextAccessor);

        public IApplicantSurveyChoicesRepository ApplicantSurveyChoices => _applicantsurveychoicesrepository = _applicantsurveychoicesrepository ?? new ApplicantSurveyChoicesRepository(_context, Config, _httpContextAccessor);

        public IApplicantSurveyRepository ApplicantSurveys => _applicantsurveysrepository  = _applicantsurveysrepository ?? new ApplicantSurveyRepository(_context, Config, _httpContextAccessor);

        public IChoiceRepository Choices => _choicerepository = _choicerepository ?? new ChoiceRepository(_context, Config, _httpContextAccessor);
        public async Task<int> CommitAsync()
        {
            return await _context.SaveChangesAsync();
        }

        public void Dispose()
        {
            _context.Dispose();
        }

我相信 swagger 正在通过您的 controller 端点运行,并将激活一些服务,而一些服务仍然只是注册但尚未解决。 因此,在我的理论中,当您使用申请人服务的逻辑时,也会发生错误。

该错误表示在尝试激活申请人服务时无法解决 IUnitOfWork 的实现。 所以我猜,您错过了 class UnitOfWork 或 class UnitOfWork 中引用的接口的注册。 但那将是我要看的地方。

Singelton、Scope 和 Transient 在这个阶段应该没有任何关联。

我弄清楚了这个问题。 我的一个控制器没有正确路由,导致抛出异常! 谢谢你们每一个人的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM