'System.AggregateException error When running .NET Core web API
I am getting this error while executing a .NET Core v6.0 web API project. Can anyone show me how to register a controller?
System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: WebAPIStructure.WebApp._2_Services.ServiceInterfaces.IMovies Lifetime: Transient ImplementationType: WebAPIStructure.WebApp._2_Services.ServiceLogic.MoviesService': Unable to resolve service for type 'WebAPIStructure.WebApp._4_DataAcess.Context' while attempting to activate 'WebAPIStructure.WebApp._4_DataAcess.Repository.MovieRepository'.) (Error while validating the service descriptor 'ServiceType: WebAPIStructure.WebApp._3_BusinessLogic.BusinessLogicInterfaces.IMoviesBusinessLogic Lifetime: Transient ImplementationType: WebAPIStructure.WebApp._3_BusinessLogic.BusinessLogic.MoviesBusinessLogic': Unable to resolve service for type 'WebAPIStructure.WebApp._4_DataAcess.Context' while attempting to activate 'WebAPIStructure.WebApp._4_DataAcess.Repository.MovieRepository'.) (Error while validating the service descriptor 'ServiceType: WebAPIStructure.WebApp._4_DataAcess.IRepository.IMovieRepository Lifetime: Transient ImplementationType: WebAPIStructure.WebApp._4_DataAcess.Repository.MovieRepository': Unable to resolve service for type 'WebAPIStructure.WebApp._4_DataAcess.Context' while attempting to activate 'WebAPIStructure.WebApp._4_DataAcess.Repository.MovieRepository'.)'
Program.cs
using WebAPIStructure.WebApp._2_Services.ServiceInterfaces;
using WebAPIStructure.WebApp._2_Services.ServiceLogic;
using WebAPIStructure.WebApp._3_BusinessLogic.BusinessLogic;
using WebAPIStructure.WebApp._3_BusinessLogic.BusinessLogicInterfaces;
using WebAPIStructure.WebApp._4_DataAcess.IRepository;
using WebAPIStructure.WebApp._4_DataAcess.Repository;
using Microsoft.EntityFrameworkCore;
using WebAPIStructure.WebApp._4_DataAcess;
using System.Configuration;
var builder = WebApplication.CreateBuilder(args);
{
var services = builder.Services;
services.AddControllers();
}
builder.Services.AddTransient<IMovies, MoviesService>();
builder.Services.AddTransient<IMoviesBusinessLogic, MoviesBusinessLogic>();
builder.Services.AddTransient<IMovieRepository, MovieRepository>();
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
{
app.MapControllers();
}
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.Run();
MovieController.cs
using Microsoft.AspNetCore.Mvc;
using WebAPIStructure.Common.Models;
using WebAPIStructure.WebApp._2_Services.ServiceInterfaces;
namespace WebAPIStructure.WebApp._1_WebAPI.Controllers
{
[ApiController]
[Route("api/Movie")]
public class MovieController : Controller
{
private readonly IMovies iMovie;
public MovieController(IMovies _iMovie)
{
iMovie = _iMovie;
}
[HttpGet]
[ProducesResponseType(200, Type = typeof(List<Movie>))]
public async Task<IActionResult> Get(MovieSearchingParameters movieSearchParameters)
{
var movies = await iMovie.SearchMoviesAsync(movieSearchParameters);
if (movies == null || !movies.Any())
{
return NotFound();
}
return Json(movies);
}
}
}
Solution 1:[1]
You should register services in the Startup.cs file for your web api. Then will then be correctly passed into the controller.
public void ConfigureServices(IServiceCollection services)
{
services.Add(new ServiceDescriptor(typeof(IMovies), new MoviesService()));
services.Add(new ServiceDescriptor(typeof(IMoviesBusinessLogic), new MoviesBusinessLogic()));
services.Add(new ServiceDescriptor(typeof(IMovieRepository), new MovieRepository()));
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nathelol |
