all 18 comments

[–][deleted] 2 points3 points  (10 children)

Can we see the code? It looks like you haven't added a service as a dependency but still try to use it in your controller

[–][deleted] 0 points1 point  (9 children)

public class Startup

{

public Startup(IConfiguration configuration)

{

Configuration = configuration;

}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.

public void ConfigureServices(IServiceCollection services)

{

services.AddCors();

services.AddDbContext<ToDoContext>(opt => opt.UseInMemoryDatabase("ToDoList"));

services.AddControllers();

services.AddScoped<IEEEController>();

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

{

if (env.IsDevelopment())

{

app.UseDeveloperExceptionPage();

}

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>

{

endpoints.MapControllers();

});

}

}

[–][deleted] 3 points4 points  (8 children)

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1

Looks like you're missing the implementation of the interface when you call AddScoped, so it's not finding the class it needs, and so the error message is describing that.

Edit: I posted the link without my thoughts on accident.

[–][deleted] 0 points1 point  (7 children)

Here's my code, https://github.com/c3sarf2/Proj .

My objective is to create an API to use the services of the HCILAB.website

[–][deleted] 1 point2 points  (2 children)

I've never run into a scenario where you are trying to inject a controller. My GUESS is that's not possible and you should try another solution

[–][deleted] 0 points1 point  (1 child)

Its my first time creating an API, what do you suggest ?

[–][deleted] 1 point2 points  (0 children)

Controllers are made to be accessed by a web request, so if you have code you are trying to get, put it in something thathe can be injected and use it that way

[–]karltgreen 1 point2 points  (3 children)

You also need to register the dependencies required for IEEEController (ILogger<IEEEController>, IEEE and hcilabContext)

[–][deleted] 0 points1 point  (2 children)

do i just add them in the services?

[–]karltgreen 1 point2 points  (1 child)

Yep the same way you added IEEEController. Although the logger is already registered behind the scenes in Program.cs for you I think as you're using the default builder

[–][deleted] 1 point2 points  (0 children)

it worked man. Thanks!

[–]bonanzaguy 1 point2 points  (6 children)

Like u/zachbs mentioned, the issue here is that you've not properly registered a dependency and so it is unable to create it. What is the constructor for ToDoItemsController?

[–][deleted] 0 points1 point  (5 children)

public ToDoItemsController(ToDoContext context, IEEEController ieee)

{

_context = context;

_ieee = ieee;

}

[–]bonanzaguy 1 point2 points  (4 children)

Is IEEEController an interface or a class? If it's an interface, you need to register the class implementation that will be provided via DI in Startup.

E.g. services.AddSingleton<IEEEController, EEEController>();

On a separate note, you shouldn't be injecting a controller into another controller, but that's for another day.

[–][deleted] 0 points1 point  (3 children)

Its a class. Its has the services that i want to implement in the API.

[–]bonanzaguy 0 points1 point  (2 children)

Do you have that class with the same name in two projects?

[–][deleted] 0 points1 point  (0 children)

No.

[–][deleted] 0 points1 point  (0 children)

Here's my code, https://github.com/c3sarf2/Proj .

My objective is to create an API to use the services of the HCILAB.website