程序.cs上的AutoMapper错误在.net 6 EF中生成错误



我一直收到一个错误-TY提前寻求这个的帮助

出现异常:CLR/系统。聚合异常类型为"System"的未处理异常。Microsoft中出现AggregateException。扩展。DependencyInjection.dll:"无法构造某些服务"发现内部异常,有关详细信息,请参阅变量窗口中的$exception。最内部的异常系统。InvalidOperationException:无法解析类型"AutoMapper"的服务。试图激活"ApplicationAdmin"时映射器"。EntityFrameworkLayer。服务。ApplicationService"。在微软。扩展。DependencyInjection。ServiceLookup。CallSiteFactory。CreateArgumentCallSites(Type implementationType,CallSiteChain CallSiteChain,ParameterInfo[]参数,Boolean throwIfCallSiteNotFound(在微软。扩展。DependencyInjection。ServiceLookup。CallSiteFactory。CreateConstructorCallSite(ResultCache生存期,类型serviceType,类型implementationType,CallSiteChain CallSiteChain(在微软。扩展。DependencyInjection。ServiceLookup。CallSiteFactory。TryCreateExact(ServiceDescriptor描述符,类型serviceType,CallSiteChain CallSiteChain,Int32插槽(在微软。扩展。DependencyInjection。ServiceLookup。CallSiteFactory。GetCallSite(ServiceDescriptor ServiceDescriptor,CallSiteChain CallSiteChain(在微软。扩展。DependencyInjection。服务提供商。ValidateService(ServiceDescriptor描述符(

这是我的程序.cs

using ApplicationAdmin.BusinessLayer.Interfaces;
using ApplicationAdmin.BusinessLayer.Services;
using ApplicationAdmin.EntityFrameworkLayer.Data.Contexts;
using ApplicationAdmin.EntityFrameworkLayer.Interfaces;
using ApplicationAdmin.EntityFrameworkLayer.Services;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
ConfigurationManager config = builder.Configuration;
// Add services to the container.
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<dbContext>(options => options.UseSqlServer(config.GetConnectionString("default")));
// builder.Services.AddAutoMapper(config => config.AddProfile<ApplicationProfile>());
// builder.Services.AddAutoMapper(config => config.AddProfile<FieldProfile>());
builder.Services.AddScoped<IApplicationService,ApplicationService>();
builder.Services.AddScoped<IFieldService,FieldService>();
builder.Services.AddScoped<IPageService,PageService>();
builder.Services.AddScoped<ISectionService,SectionService>();
builder.Services.AddScoped<ISubsectionService,SubsectionService>();
builder.Services.AddScoped<IApplicationBLService,ApplicationBLService>();
builder.Services.AddScoped<IFieldBLService,FieldBLService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

这是我的ApplicationService

using ApplicationAdmin.EntityFrameworkLayer.Data.Models;
using ApplicationAdmin.EntityFrameworkLayer.Data.Contexts;
using ApplicationAdmin.BusinessLayer.DataModels.Application;
using ApplicationAdmin.EntityFrameworkLayer.Interfaces;
using Microsoft.EntityFrameworkCore;
using ApplicationAdmin.Helper;
using AutoMapper;
namespace ApplicationAdmin.EntityFrameworkLayer.Services;
public class ApplicationService : IApplicationService
{
private dbContext _dbContext;
private Mapper _mapper;
public ApplicationService (dbContext dbContext, Mapper mapper)
{
_dbContext = dbContext;
_mapper = mapper;
}
public async Task<List<Application>> Get(GetApplicationRequest request, CancellationToken xlT)
{
Guid guidApplicationId = Guid.Empty;
Guid.TryParse(request.ApplicationId, out guidApplicationId); 
return await _dbContext.Applications
.Where(i => 
i.ApplicationId == (request.ApplicationId == null ? i.ApplicationId : guidApplicationId) && 
i.CreatedById == (request.CreatedById ?? i.CreatedById) && 
i.CreatedOnUtc == (request.CreatedOnUtc ?? i.CreatedOnUtc) && 
EF.Functions.Like(i.Description,(StringConverter.ConvertToLike(request.Description) ?? i.Description)) && 
i.IsEnabled == (request.IsEnabled ?? i.IsEnabled) && 
i.UpdatedById == (request.UpdatedById ?? i.UpdatedById) && 
i.UpdatedOnUtc == (request.UpdatedOnUtc ?? i.UpdatedOnUtc)
)
.ToListAsync(xlT);
}
public async Task<List<Application>> Post(List<PostApplicationRequest> requests, bool saveNow, CancellationToken xlT)
{
List<Application> applications = new List<Application>();
foreach(PostApplicationRequest request in requests)
{
Application application = new Application()
{
ApplicationId = Guid.NewGuid(),
Description = request.Description, 
IsEnabled = request.IsEnabled, 
CreatedById = request.CreatedById, 
CreatedOnUtc = DateTime.UtcNow
};
applications.Add(application);
}
_dbContext.AddRange(applications);
if(saveNow)
{
await _dbContext.SaveChangesAsync(xlT);            
}
return applications;
}
public async Task<List<Application>> Put(PutApplicationRequest request, bool saveNow, CancellationToken xlT)
{
List<Application> applications = await Get(_mapper.Map<GetApplicationRequest>(request), xlT);
foreach(Application application in applications)
{
application.Description = (request.Description ?? application.Description); 
application.IsEnabled = (request.IsEnabled ?? application.IsEnabled); 

if(_dbContext.ChangeTracker.HasChanges())
{
application.UpdatedById = request.UpdatedById;
application.UpdatedOnUtc = DateTime.UtcNow;
};
};
if(saveNow && _dbContext.ChangeTracker.HasChanges())
{
await _dbContext.SaveChangesAsync(xlT);            
}
return applications;
}
}

这是我的界面

using ApplicationAdmin.EntityFrameworkLayer.Data.Models;
using ApplicationAdmin.BusinessLayer.DataModels.Application;
namespace ApplicationAdmin.EntityFrameworkLayer.Interfaces;
public interface IApplicationService
{
public Task<List<Application>> Get(GetApplicationRequest request, CancellationToken xlT);
public Task<List<Application>> Put(PutApplicationRequest request, bool saveNow, CancellationToken xlT);
public Task<List<Application>> Post(List<PostApplicationRequest> requests, bool saveNow, CancellationToken xlT);
}

您必须注入接口IMapper,而不是类Mapper

最新更新