mongodb ASP.NET错误:内部服务器错误:系统操作无效异常:尝试激活时无法解析类型的服务

bd1hkmkf  于 2022-12-12  发布在  Go
关注(0)|答案(1)|浏览(123)

我按照这个youtube指南https://www.youtube.com/watch?v=MNepwvCcKXA设置简单的视频商店,但我得到这个错误,而试图执行获取视频:

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)

这是我的代码:
视频控制器.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; }

    }
}

而Program.cs是我认为我搞砸的地方:

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();

当视频中显示在Startup.cs中写入此行时,问题开始出现

builder.Services.AddTransient<IVideoServices, VideoServices>();

但是startup.cs文件在较新的ASP.NET版本中丢失了,我把它放在了Program.CS中,但是我得到了错误。我不知道现在如何修复它,有人能帮助我吗?

6kkfgxo0

6kkfgxo01#

builder.Services.AddSwaggerGen();行之后添加builder.Services.AddTransient<IVideoServices, VideoServices>();

########## 检查代码后,您已经创建了两个接口IVideoServices,一个在控制器中,一个在服务器中。您在服务器中注册了IVideoServices,但在控制器中使用了IVideoServices。

相关问题