在 .net 核心 NLayer 架构中处理请求时发生未处理的异常
Posted
技术标签:
【中文标题】在 .net 核心 NLayer 架构中处理请求时发生未处理的异常【英文标题】:An unhandled exception occurred while processing the request in .net core NLayer architecture 【发布时间】:2021-01-21 19:08:40 【问题描述】:我正在尝试使用 NLayer 架构和通用存储库在 .net 核心中开发一个 api。
当我尝试获取数据时,我看到了一个错误。
我的错误 处理请求时发生未处理的异常。 InvalidOperationException:尝试激活“Fenerbahce.API.Controllers.PlayerController”时无法解析“Fenerbahce.Business.Abstract.IPlayerManager”类型的服务。
IPlayerManger:
public interface IPlayerManager
Task<IEnumerable<Player>> GetAllPlayers();
Task<Player> GetPlayerById(int id);
Task<Player> CreatePlayer(Player player);
Task UpdatePlayer(Player playerToBeUpdated,Player player);
Task DeletePlayer(Player player);
播放器管理器
私有只读 IUnitOfWork _unitOfWork;
public PlayerManager(IUnitOfWork unitOfWork)
this._unitOfWork = unitOfWork;
public async Task<Player> CreatePlayer(Player player)
await _unitOfWork.Players.AddAsync(player);
await _unitOfWork.CommitAsync();
return player;
public async Task DeletePlayer(Player player)
_unitOfWork.Players.Remove(player);
await _unitOfWork.CommitAsync();
public async Task<IEnumerable<Player>> GetAllPlayers()
return await _unitOfWork.Players.GetAllAsync();
public async Task<Player> GetPlayerById(int id)
return await _unitOfWork.Players.GetByIdAsync(id);
public async Task UpdatePlayer(Player playerToBeUpdated, Player player)
playerToBeUpdated.updatedAt = DateTime.Now;
await _unitOfWork.CommitAsync();
IUnitOfWork
public interface IUnitOfWork : IDisposable
IBranchDal Branchs get;
ICoachDal Coachs get;
IPlayerDal Players get;
ITeamDal Teams get;
Task<int> CommitAsync();
EfUnitOfWork.cs
public class EfUnitOfWork : IUnitOfWork
private readonly FBDbContext _context;
private EfBranchDal _branchDal;
private EfCoachDal _coachDal;
private EfPlayerDal _playerDal;
private EfTeamDal _teamDal;
public EfUnitOfWork(FBDbContext context)
this._context = context;
public IBranchDal Branchs => _branchDal = _branchDal ?? new EfBranchDal(_context);
public ICoachDal Coachs => _coachDal = _coachDal ?? new EfCoachDal(_context);
public IPlayerDal Players => _playerDal = _playerDal ?? new EfPlayerDal(_context);
public ITeamDal Teams => _teamDal = _teamDal ?? new EfTeamDal(_context);
public async Task<int> CommitAsync()
return await _context.SaveChangesAsync();
public void Dispose()
_context.Dispose();
PlayerController.cs
public class PlayerController:ControllerBase
private readonly IPlayerManager _playerManager;
private readonly IMapper _mapper;
public PlayerController(IPlayerManager playerManager, IMapper mapper)
this._playerManager = playerManager;
this._mapper = mapper;
[HttpGet]
public async Task<ActionResult<IEnumerable<PlayerDTO>>> ListAllPlayers()
var players = await _playerManager.GetAllPlayers();
var playerResources = _mapper.Map<IEnumerable<Player>,
IEnumerable<PlayerDTO>>(players);
return Ok(playerResources);
感谢您的帮助。
【问题讨论】:
它准确地告诉你出了什么问题Unable to resolve service for type 'Fenerbahce.Business.Abstract.IPlayerManager' while attempting to activate 'Fenerbahce.API.Controllers.PlayerController
- 你需要一个类型来实现注册到你的依赖注入框架的接口。
【参考方案1】:
显然,依赖注入容器丢失了。您可以为此使用 unity/ninject/autofac,您可以从 google 学习如何使用。
还有,HER ZAMAN HER YERDE..
【讨论】:
是的,我在服务中添加了瞬态并且这个工作..正如你所说的 bütün bu alemde en büyüksün fenerbahçe..以上是关于在 .net 核心 NLayer 架构中处理请求时发生未处理的异常的主要内容,如果未能解决你的问题,请参考以下文章