No tienes acceso a esta clase

隆Contin煤a aprendiendo! 脷nete y comienza a potenciar tu carrera

Creando controladores

20/22
Recursos

Aportes 11

Preguntas 9

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad?

o inicia sesi贸n.

Como recomendaci贸n la propiedad del servicio deber铆a ser declarada como protected y de solo lectura de la siguiente forma

protected readonly ITareasService _tareasService;

es un est谩ndar que recomienda .Net

Falt贸 explicar en el curso los niveles en una Api REST

Hola, para quienes al momento de compilar el codigo se les presenta el error:

Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 鈥楽erviceType: webapi.Services.ICategoriaService Lifetime: Scoped ImplementationType: webapi.Services.CategoriaService鈥: Unable to resolve service for type 鈥榳ebapi.TareasContext鈥 while attempting to activate 鈥榳ebapi.Services.CategoriaService鈥.)
鈥> System.InvalidOperationException: Error while validating the service descriptor 鈥楽erviceType: webapi.Services.ICategoriaService Lifetime: Scoped ImplementationType: webapi.Services.CategoriaService鈥: Unable to resolve service for type 鈥榳ebapi.TareasContext鈥 while attempting to activate 鈥榳ebapi.Services.CategoriaService鈥.

No es un error en el codigo en realidad, ya que al momento de seguir las clases siguientes y agregar el codigo que falta este 鈥渆rror鈥 se soluciona. Muy segurmente no funciona por la falta de codigo adicional

CategoriaController

using Microsoft.AspNetCore.Mvc;
using webapi.Models;
using webapi.Services;

namespace webapi.Controllers;

[Route("api/[controller]")]
public class CategoriaController : ControllerBase
{
    ICategoriaService categoriaService;

    public CategoriaController(ICategoriaService service) {
        categoriaService = service;
    }

    [HttpGet]
    public IActionResult Get() {
        return Ok(categoriaService.Get());
    }

    [HttpPost]
    public IActionResult Post([FromBody] Categoria categoria) {
        categoriaService.Save(categoria);
        return Ok();
    }

    [HttpPut("{id}")]
    public IActionResult Put(Guid id, [FromBody] Categoria categoria) {
        categoriaService.Update(id, categoria);
        return Ok();
    }

    [HttpDelete("{id}")]
    public IActionResult Delete(Guid id) {
        categoriaService.Delete(id);
        return Ok();
    }
}

TareaController

using Microsoft.AspNetCore.Mvc;
using webapi.Models;
using webapi.Services;

namespace webapi.Controllers;

[Route("api/[controller]")]
public class TareaController : ControllerBase
{
    ITareaService tareaService;

    public TareaController(ITareaService service) {
        tareaService = service;
    }

    [HttpGet]
    public IActionResult Get() {
        return Ok(tareaService.Get());
    }

    [HttpPost]
    public IActionResult Post([FromBody] Tarea tarea) {
        tareaService.Save(tarea);
        return Ok();
    }

    [HttpPut("{id}")]
    public IActionResult Put(Guid id, [FromBody] Tarea tarea) {
        tareaService.Update(id, tarea);
        return Ok();
    }

    [HttpDelete("{id}")]
    public IActionResult Delete(Guid id) {
        tareaService.Delete(id);
        return Ok();
    }
}
Que hace el atributo APIController?

VSC me marcaba error porque no ten铆a los m茅todos en la Interfaz de TareasService, solo asegurense de que est茅n definidos:

public interface ITareasService
{
  IEnumerable<Tarea> Get();
  Task Save(Tarea tarea);
  Task Update(Guid id, Tarea tarea);
  Task Delete(Guid id);
}

Y ya con eso queda el archivo de TareaController:

using Microsoft.AspNetCore.Mvc;
using webapi.Models;
using webapi.Services;

namespace webapi.Controllers;

[Route("/api/[controller]")]
public class TareaController : ControllerBase
{
  protected readonly ITareasService _tareasService;

  public TareaController(ITareasService service)
  {
    _tareasService = service;
  }

  [HttpGet]
  public IActionResult Get()
  {
    return Ok(_tareasService.Get());
  }

  [HttpPost]
  public IActionResult Post([FromBody] Tarea tarea)
  {
    _tareasService.Save(tarea);
    return Ok("Tarea creada");
  }

  [HttpPut("{id}")]
  public IActionResult Put(Guid id, [FromBody] Tarea tarea)
  {
    _tareasService.Update(id, tarea);
    return Ok("Tarea actualizada");
  }

  [HttpDelete("{id}")]
  public IActionResult Delete(Guid id)
  {
    _tareasService.Delete(id);
    return Ok("Tarea eliminada");
  }
}

![](

![](

Mi TareaController 馃槉

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using webapi.Models;
using webapi.Services;

namespace proyectapirest.Controllers;
[Route("api/[controller]")]
public class TareaController : ControllerBase
{
    ITareasService tareasService;

    public TareaController(TareasService service)
    {
        tareasService = service;
    }

    [HttpGet]
    public IActionResult Get()
    {
        return Ok(tareasService.Get());
    }
    [HttpPost]
    public IActionResult Post([FromBody] Tarea tarea)
    {
        tareasService.Save(tarea);
        return Ok();
    }
    [HttpPut("{id}")]
    public IActionResult Post(Guid id, [FromBody] Tarea tarea)
    {
        tareasService.Update(id, tarea);
        return Ok();
    }
    [HttpDelete("{id}")]
    public IActionResult Post(Guid id)
    {
        tareasService.Delete(id);
        return Ok();
    }
}

Bien explicado.

Excelente buena explicacion como siempre!