Resolved Possible Issue with Registering a Service?

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:
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:
Are you sure you are even running the code that you just wrote? How is this bit of code successfully compiling?
C#:
public PeopleController(PeopleService _peopleService)
{
    _peopleService = peopleService;
}

where is peopleService defined?
 
Are you sure you are even running the code that you just wrote? How is this bit of code successfully compiling?
C#:
public PeopleController(PeopleService _peopleService)
{
    _peopleService = peopleService;
}

where is peopleService defined?
I suspect that there should be no underscore on the parameter declaration but, as you say, it looks like it should not compile as it is.
 
I suspect that there should be no underscore on the parameter declaration but, as you say, it looks like it should not compile as it is.
Yeah, sorry, there actually is no underscore in the parameter.

So is it wrong in saying the order should be
1. Inject services in the Startup file that connects to the specific service interface and service class
2. Create a service that inherits from the interface and run the required method. (Runs info from the ApplicationDb context)
3. Use DI on the controller to connect to the service class
4. Reference the methods from the service class to display info from the database

Model looks like something like this:
C#:
public class People
{
    [Key]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

And it does have info in the table
 
Last edited:
Figured it out. It was a syntax thing.
Error on the Controller

Should have been:
C#:
public PeopleController(IPeopleService _peopleService)
{
    _peopleService = peopleService;
}

I was referencing the actual service, and not the interface
 
Still looks kind of wrong... You seem to be overwriting your incoming _peopleService with some peopleService that looks to be undefined.
 
C#:
private readonly IPeopleService _peopleService;

public PeopleController(IPeopleService peopleService)
{
    _peopleService = peopleService;
}
Sorry, copy and pasted. This is what it should be
 
Ah!
 
Back
Top Bottom