ASP.NET 核心 Web API InvalidOperationException:无法解析服务
ASP.NET Core Web API InvalidOperationException: Unable to resolve service
我正在尝试构建一个简单的 api 作为对项目未来 api 的测试。但是我一直有这个错误
InvalidOperationException: Unable to resolve service for type
'AspnetCore_WebApi.Models.TarefaContext' while attempting to activate
'AspnetCore_WebApi.Models.TarefaRepositorio'.
这里是TarefaContext.cs
using Microsoft.EntityFrameworkCore;
namespace AspnetCore_WebApi.Models
{
public class TarefaContext : DbContext
{
public TarefaContext(DbContextOptions<TarefaContext> options)
: base(options)
{ }
public DbSet<TarefaItem> TarefaItens { get; set; }
}
}
TarefaRepositorio.cs
using System.Collections.Generic;
using System.Linq;
namespace AspnetCore_WebApi.Models
{
public class TarefaRepositorio : ITarefaRepositorio
{
private readonly TarefaContext _context;
public TarefaRepositorio(TarefaContext context)
{
_context = context;
Add(new TarefaItem { Nome = "Item1" });
}
public IEnumerable<TarefaItem> GetAll()
{
return _context.TarefaItens.ToList();
}
public void Add(TarefaItem item)
{
_context.TarefaItens.Add(item);
_context.SaveChanges();
}
public TarefaItem Find(long key)
{
return _context.TarefaItens.FirstOrDefault(t => t.Chave == key);
}
public void Remove(long key)
{
var entity = _context.TarefaItens.First(t => t.Chave == key);
_context.TarefaItens.Remove(entity);
_context.SaveChanges();
}
public void Update(TarefaItem item)
{
_context.TarefaItens.Update(item);
_context.SaveChanges();
}
}
}
TarefaController.cs
using System.Collections.Generic;
using AspnetCore_WebApi.Models;
using Microsoft.AspNetCore.Mvc;
namespace AspnetCore_WebApi.Controllers
{
[Route("api/[controller]")]
public class TarefaController : Controller
{
private readonly ITarefaRepositorio _tarefaRepositorio;
public TarefaController(ITarefaRepositorio tarefaRepositorio)
{
_tarefaRepositorio = tarefaRepositorio;
}
[HttpGet]
public IEnumerable<TarefaItem> GetAll()
{
return _tarefaRepositorio.GetAll();
}
[HttpGet("{id}", Name = "GetTarefa")]
public IActionResult GetById(long id)
{
var item = _tarefaRepositorio.Find(id);
if (item == null)
{
return NotFound();
}
return new ObjectResult(item);
}
[HttpPost]
public IActionResult Create([FromBody] TarefaItem item)
{
if (item == null)
{
return BadRequest();
}
_tarefaRepositorio.Add(item);
return CreatedAtRoute("GetTarefa", new { id = item.Chave }, item);
}
}
}
我TarefaRepositorio.cs
using System.Collections.Generic;
namespace AspnetCore_WebApi.Models
{
public interface ITarefaRepositorio
{
void Add(TarefaItem item);
IEnumerable<TarefaItem> GetAll();
TarefaItem Find(long key);
void Remove(long key);
void Update(TarefaItem item);
}
}
错误消息描述依赖注入无法解析您的 TarefaContext class。您应该使用 services.AddDbContext
方法在启动 ConfigureServices
方法中注册您的数据库上下文。
有关详细信息,请参阅 Microsoft 文档:https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
As Scott and Artis said in I haven't added my Context Class to the startup.cs file. I had some trouble with the article mentioned by Artis 因为我使用的是 EF Core InMemory 所以我不得不添加
services.AddDbContext<TarefaContext>(options => options.UseInMemoryDatabase());
改为 Startup.cs 的 ConfigureServices
services.AddDbContext<TarefaContext>(options => options.UseSqlite("Data Source=someDB.db"));
谢谢大家的帮助
我正在尝试构建一个简单的 api 作为对项目未来 api 的测试。但是我一直有这个错误
InvalidOperationException: Unable to resolve service for type 'AspnetCore_WebApi.Models.TarefaContext' while attempting to activate 'AspnetCore_WebApi.Models.TarefaRepositorio'.
这里是TarefaContext.cs
using Microsoft.EntityFrameworkCore;
namespace AspnetCore_WebApi.Models
{
public class TarefaContext : DbContext
{
public TarefaContext(DbContextOptions<TarefaContext> options)
: base(options)
{ }
public DbSet<TarefaItem> TarefaItens { get; set; }
}
}
TarefaRepositorio.cs
using System.Collections.Generic;
using System.Linq;
namespace AspnetCore_WebApi.Models
{
public class TarefaRepositorio : ITarefaRepositorio
{
private readonly TarefaContext _context;
public TarefaRepositorio(TarefaContext context)
{
_context = context;
Add(new TarefaItem { Nome = "Item1" });
}
public IEnumerable<TarefaItem> GetAll()
{
return _context.TarefaItens.ToList();
}
public void Add(TarefaItem item)
{
_context.TarefaItens.Add(item);
_context.SaveChanges();
}
public TarefaItem Find(long key)
{
return _context.TarefaItens.FirstOrDefault(t => t.Chave == key);
}
public void Remove(long key)
{
var entity = _context.TarefaItens.First(t => t.Chave == key);
_context.TarefaItens.Remove(entity);
_context.SaveChanges();
}
public void Update(TarefaItem item)
{
_context.TarefaItens.Update(item);
_context.SaveChanges();
}
}
}
TarefaController.cs
using System.Collections.Generic;
using AspnetCore_WebApi.Models;
using Microsoft.AspNetCore.Mvc;
namespace AspnetCore_WebApi.Controllers
{
[Route("api/[controller]")]
public class TarefaController : Controller
{
private readonly ITarefaRepositorio _tarefaRepositorio;
public TarefaController(ITarefaRepositorio tarefaRepositorio)
{
_tarefaRepositorio = tarefaRepositorio;
}
[HttpGet]
public IEnumerable<TarefaItem> GetAll()
{
return _tarefaRepositorio.GetAll();
}
[HttpGet("{id}", Name = "GetTarefa")]
public IActionResult GetById(long id)
{
var item = _tarefaRepositorio.Find(id);
if (item == null)
{
return NotFound();
}
return new ObjectResult(item);
}
[HttpPost]
public IActionResult Create([FromBody] TarefaItem item)
{
if (item == null)
{
return BadRequest();
}
_tarefaRepositorio.Add(item);
return CreatedAtRoute("GetTarefa", new { id = item.Chave }, item);
}
}
}
我TarefaRepositorio.cs
using System.Collections.Generic;
namespace AspnetCore_WebApi.Models
{
public interface ITarefaRepositorio
{
void Add(TarefaItem item);
IEnumerable<TarefaItem> GetAll();
TarefaItem Find(long key);
void Remove(long key);
void Update(TarefaItem item);
}
}
错误消息描述依赖注入无法解析您的 TarefaContext class。您应该使用 services.AddDbContext
方法在启动 ConfigureServices
方法中注册您的数据库上下文。
有关详细信息,请参阅 Microsoft 文档:https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext
As Scott and Artis said in
services.AddDbContext<TarefaContext>(options => options.UseInMemoryDatabase());
改为 Startup.cs 的 ConfigureServices
services.AddDbContext<TarefaContext>(options => options.UseSqlite("Data Source=someDB.db"));
谢谢大家的帮助