.NET 6 and AutoMapper

108 Views Asked by At

I'm trying to implement AutoMapper in my project of Hotels in ASP.NET; in .NET 5 it was working fine, but as I moved to .NET 6, I cannot figure out how to exchange this.GetType() in builder.Services.AddAutoMapper(this.GetType().Assembly);.

I get this error

JsonException: A possible object cycle was detected

Program.cs

namespace Hotelum
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // Add services to the container.
            builder.Services.AddControllers();
            builder.Services.AddDbContext<HotelsDbContext>();
            builder.Services.AddScoped<HotelsSeeder>();
            builder.Services.AddAutoMapper(this.GetType().Assembly);

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            using (var scope = app.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var seeder = services.GetRequiredService<HotelsSeeder>();
                    seeder.Seed();
                }
                catch (Exception ex)
                {
                    // Handle any exceptions while seeding
                    Console.WriteLine($"An error occurred while seeding the database: {ex.Message}");
                }
            }
            
            app.UseHttpsRedirection();

            //app.UseAuthorization();

            app.MapControllers();

            app.Run();
        }
    }
}

Mapping file

namespace Hotelum
{
    public class HotelsMappingProfile : Profile
    {
        public HotelsMappingProfile()
        {
            CreateMap<Hotels, HotelsDto>()
                .ForMember(m => m.City, c => c.MapFrom(s => s.Address.City))
                .ForMember(m => m.Street, c => c.MapFrom(s => s.Address.Street))
                .ForMember(m => m.PostalCode, c => c.MapFrom(s => s.Address.PostalCode));

            CreateMap<Rooms, RoomsDto>();
        }
    }
}

Controller

namespace Hotelum.Controllers
{
    [Route("api/hotelum")]
    public class HotelController : ControllerBase
    {
        private readonly HotelsDbContext _dbcontext;
        private readonly IMapper _mapper;

        public HotelController(HotelsDbContext dbContext, IMapper mapper)
        {
            _dbcontext = dbContext;
            _mapper = mapper;
        }

        public ActionResult<IEnumerable<HotelsDto>> GetAll()
        {
            var hotels = _dbcontext
                .Hotels
                .Include(r => r.Address)
                .Include(r => r.Rooms)
                .ToList();

            var hotelsDtos = _mapper.Map<List<HotelsDto>>(hotels);

            return Ok(hotelsDtos);
        }

        [HttpGet("{id}")]
        public ActionResult<Hotels> Get([FromRoute] int id)
        {
            var hotels = _dbcontext
                .Hotels
                .Include(r => r.Address)
                .Include(r => r.Rooms)
                .FirstOrDefault(h => h.Id == id);

            if (hotels == null)
            {
                return NotFound();
            }

            var hotelsDtos = _mapper.Map<HotelsDto>(hotels);

            return Ok(hotelsDtos);
        }
    }
}

I have tried to change it for typeof() but seems to not working with anything.

1

There are 1 best solutions below

0
On BEST ANSWER

Using builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly())fixed problem