我正在尝试对存储库控制器'PeoppleRepositoryController.cs进行测试。 我收到以下错误,我无法弄清楚它到底在抱怨什么。 谁能解释一下解决此问题所需采取的措施?
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp,Type type,type requiredBy,bool isDefaultParameterRequired)
完整的堆栈跟踪可以在下图看到:
控制器为:
function toggle(id) {
var div1 = document.getElementById(id);
if(div1.hidden) {
div1.hidden = false;
} else {
div1.hidden = true;
}
}
IUnitOfWork文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Personkartotek.DAL;
using Personkartotek.Models;
using Personkartotek.Persistence;
namespace Personkartotek.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PeopleRepositoryController : ControllerBase
{
private readonly IUnitOfWork _uoWork;
public PeopleRepositoryController(IUnitOfWork uoWork)
{
_uoWork = uoWork;
}
// GET: api/PeopleRepository
[HttpGet]
public IEnumerable<Person> GetPersons()
{
return _uoWork._People.GetAll();
}
// GET: api/PeopleRepository/5
[HttpGet("{id}")]
public async Task<IActionResult> GetPerson([FromRoute] int id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var person = _uoWork._People.Get(id);
if (person == null)
{
return NotFound();
}
return Ok(person);
}
//GET: api/PeopleRepository/
[HttpGet("AtAdr/{id}")]
public IActionResult GetPersonsResidingAtAddress([FromRoute] int AddressId)
{
var ResidingPersons = _uoWork._People.GetAllPersonsById(AddressId);
return Ok(ResidingPersons);
}
// PUT: api/PeopleRepository/5
[HttpPut("{id}")]
public async Task<IActionResult> PutPerson([FromRoute] int id, [FromBody] Person person)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != person.PersonId)
{
return BadRequest();
}
if (!PersonExists(id))
{
return NotFound();
}
_uoWork._People.Put(person);
return NoContent();
}
// POST: api/PeopleRepository
[HttpPost]
public async Task<IActionResult> PostPerson([FromBody] Person person)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_uoWork._People.Add(person);
_uoWork.Complete();
return CreatedAtAction("GetPerson", new { id = person.PersonId }, person);
}
// DELETE: api/PeopleRepository/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeletePerson([FromRoute] int id)
{
if (!ModelState.IsValid) {
return BadRequest(ModelState);
}
var person = _uoWork._People.Get(id);
if (person == null) {
return NotFound();
}
_uoWork._People.Remove(person);
_uoWork.Complete();
return Ok(person);
}
private bool PersonExists(int id)
{
return _uoWork.Exist(id);
}
}
}
我的Startup.cs文件设置:
using Personkartotek.DAL.IRepositories;
namespace Personkartotek.DAL
{
public interface IUnitOfWork : IDisposable
{
IPeopleRepository _People { get; }
int Complete();
bool Exist(int id);
}
}
答案 0 :(得分:2)
它抱怨是因为它不知道如何创建IUnitOfWork
对象,该对象依赖于您的控制器。
因此,要解决此问题,您需要指示框架您要使用IUnitOfWork
的哪种实现。通常,您是在Startup.ConfigureServices
方法中执行此操作的。例如:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IUnitOfWork, UnitOfWorkImplementation>();
}
}
UnitOfWorkImplementation
是实现IUnitOfWork