I am following this youtube guide https://www.youtube.com/watch?v=MNepwvCcKXA to setup simple video store but I am getting this error while trying to execute get Videos:
System.InvalidOperationException: Unable to resolve service for type 'Wypozyczalnia.WebApi.Controllers.IVideoServices' while attempting to activate 'Wypozyczalnia.WebApi.Controllers.VideosController'.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method3(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass7_0.<CreateActivator>b__0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
This is my code:
Videos Controller.cs:
using Microsoft.AspNetCore.Mvc;
namespace Wypozyczalnia.WebApi.Controllers
{
[ApiController]
[Route("[controller]")]
public class VideosController : ControllerBase
{
private readonly IVideoServices _videoServices;
public VideosController(IVideoServices videoServices)
{
_videoServices = videoServices;
}
[HttpGet]
public IActionResult GetVideos()
{
return Ok(_videoServices.GetVideos());
}
}
}
VideoServices.cs:
`
namespace Wypozyczalnia.Core
{
public class VideoServices : IVideoServices
{
public List<Video> GetVideos()
{
return new List<Video>
{
new Video
{
Title = "Test",
Genre = "Fantasy"
}
};
}
}
}
IVideoServices.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wypozyczalnia.Core
{
public interface IVideoServices
{
List<Video> GetVideos();
}
}
Video.cs:
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wypozyczalnia.Core
{
public class Video
{
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
public string Id { get; set; }
public string Title { get; set; }
public string Genre { get; set; }
public string Director { get; set; }
public double Time { get; set; }
public double Rating { get; set; }
public string Description { get; set; }
public string Cast { get; set; }
public string Added { get; set; }
}
}
And the Program.cs is where i think i messed something up:
using Wypozyczalnia.Core;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddTransient<IVideoServices, VideoServices>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
The problem started when in the video it was shown to write this line in Startup.cs
builder.Services.AddTransient<IVideoServices, VideoServices>();
but the startup.cs file is missing in the newer ASP.NET versions, instead i've put it in the Program.CS but i get the error. I'm not sure how to fix it now, can anybone help me with this?
CodePudding user response:
add builder.Services.AddTransient<IVideoServices, VideoServices>();
after line builder.Services.AddSwaggerGen();
.
########## after check the code, you have create two interface IVideoServices, one in controller, one in service. you register IVideoServices in service, but use IVideoServices in controller.