'System.InvalidOperationException problem cannot use CRUD methods
community!
I am trying to make a booking system with C# full stack.
I made all CRUD operations for APIs, but I am constantly receiving this problem: System.InvalidOperationException: Unable to resolve service for type 'WebAPI.Data.BookingService' while attempting to activate 'WebAPI.Controllers.BookingController' when trying to use PUT method.
PUT method:
[HttpPost]
public async Task<ActionResult<Booking>> AddBooking([FromBody] Booking booking)
{
try
{
Booking added = await _bookingService.AddBookingAsync(booking);
return Created($"/{booking.Id}", booking);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return StatusCode(500, e.Message);
}
}
I also added the services into Program.cs
builder.Services.AddScoped<BookingDBContext>();
builder.Services.AddScoped<IResourceService, ResourceService>();
builder.Services.AddScoped<IBookingService, BookingService>();
How can I solve it? Does anyone know other reasons why it throws me an exception? As I read on previous posts, the problem could be on Program.cs because I didn't add the services, but, in my case, they are.
**Github link: ** https://github.com/BaicoianuIoanSorin/Simple-Booking-System/tree/main/BookingResourcesApp/WebAPI
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
