yasserpulido
yasserpulido

Reputation: 218

NET Core InvalidOperationException: Unable to resolve service for type while attempting to activate

I have a problem that you can read on the title of this question. I think is something about Dependency Injection topic, I tried to solve following the solution of others question here in stackoverflow but I couldn't resolved. So, here I am.

My project has Data, Business and Api layers. My Data layer, being specific, the Repository has:

namespace Data.Repository
{
    public class RMovie : IMovie
    {
        private EDBContext db;
        public RMovie()
        {
            db = new EDBContext();
        }

        public List<Tmovie> Read()
        {
            var listMovies = db.Tmovie.ToListAsync().Result;
            return listMovies;
        }
    }
}

In the Business layer, being specific, in the Services:

namespace Business.Services
{
    public class SMovie : IMovie
    {
        private RMovie dataMovie;

        public SMovie()
        {
            dataMovie = new RMovie();
        }

        public List<Tmovie> Read()
        {
            return dataMovie.Read();
        }
    }
}

In the API layer, being specific in the Controller:

using Business.Services;

namespace API.Controllers
{
    [Produces("application/json")]
    [Route("api/Movie")]
    public class MovieController : ControllerBase
    {
        private readonly SMovie _context;
        public MovieController(SMovie context)
        {
            _context = context;
        }

        [HttpGet]
        public IActionResult GetMovie()
        {
            return new ObjectResult(_context.Read());
        }
    }
}

And in the Startup of the Api project I has:

using Business.Interfaces;
using Business.Services;
using Data.Models;

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            var connection = @"Server=NAMEOFTHESQLSERVER;Database=EDB;Trusted_Connection=True;";
            services.AddDbContext<XXXContext>(options => options.UseSqlServer(connection));
            // Add application services.
            services.AddScoped<IMovie, SMovie>();
        }

I thinks everything is fine but it doesn't. When I start the project I got the following message:

InvalidOperationException: Unable to resolve service for type 'Business.Services.SMovie' while attempting to activate 'API.Controllers.MovieController'.

Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

Anyone can help me!, I will appreciate it!

Thanks.

Upvotes: 1

Views: 10708

Answers (1)

Anil Goel
Anil Goel

Reputation: 271

In your controller, you want to inject the interface. So change the controller to

private readonly IMovie _context;
public MovieController(IMovie context)
{
    _context = context;
}

Also make sure you have defined all the method calls and properties in your interfaces so that you can access them in the controller.

Upvotes: 3

Related Questions