Mitchelln11
Active member
- Joined
- Apr 10, 2020
- Messages
- 39
- Programming Experience
- Beginner
I am just creating services for the first time, and my application errors out whenever I try to go to the view of the controller I am working on.
A Service Interface for People:
The Service Itself:
The PeopleController:
Startup file:
The error I'm receiving is:
InvalidOperationException: Unable to resolve service for type 'architectureProject.Services.PeopleService' while attempting to activate 'architectureProject.Controllers.PeopleController'.
I've got to be connecting incorrectly.
My guess would be that it doesn't have anything to do with the Startup, because when I run the program, it does in fact build and displays my home page. I have another Model, View, and Controller, which I can still go to those views just fine.
Once I try to go to the index of my People View, that's when I get the error.
A Service Interface for People:
C#:
public interface IPeopleService
{
List<Person> GetPeople();
}
The Service Itself:
C#:
public class PeopleService : IPeopleService
{
private readonly ApplicationDbContext _context;
public PeopleService(ApplicationDbContext context)
{
_context = context;
}
public List<Person> GetPeople() => _context.People.ToList();
}
The PeopleController:
C#:
public class PeopleController : Controller
{
private readonly PeopleService _peopleService;
public PeopleController(PeopleService peopleService)
{
_peopleService = peopleService;
}
public ActionResult Index()
{
var people = _peopleService.GetPeople();
return View(people);
}
}
Startup file:
C#:
services.AddTransient<IPeopleService, PeopleService>();
The error I'm receiving is:
InvalidOperationException: Unable to resolve service for type 'architectureProject.Services.PeopleService' while attempting to activate 'architectureProject.Controllers.PeopleController'.
I've got to be connecting incorrectly.
My guess would be that it doesn't have anything to do with the Startup, because when I run the program, it does in fact build and displays my home page. I have another Model, View, and Controller, which I can still go to those views just fine.
Once I try to go to the index of my People View, that's when I get the error.
Last edited: