【问题标题】:Unable to get my Web API Interface working with my无法让我的 Web API 接口与我的
【发布时间】:2022-08-08 22:16:30
【问题描述】:

这是我尝试在 .NET 上对我的 SQL DB 执行插入操作时收到的错误:

System.InvalidOperationException: Unable to resolve service for type \'MotionPicturesCore.Interfaces.IMotionPictureService\' while attempting to activate \'MotionPicturesCore.Controllers.MotionPictureApiControllerV2\'.
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
   at lambda_method55(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()

除了我认为这可能与我如何设置依赖插入有关这一事实之外,我不确定为什么会出现此错误。最终,这将连接到一个简单的 Vue.js 应用程序,但目前,我构建的这个 API 正在向我回击这个错误。

以下是我认为可能是我设置错误的地方的 sn-ps,但我不确定。我不想发布整个代码块供任何人筛选,但如果有人能指出我正确的方向,那将不胜感激:

namespace MotionPicturesCore.Interfaces
{
    public interface IMotionPictureService
    {
        int AddMotionPicture(MotionPictureAddRequest model);
        void UpdateMotionPicture(MotionPictureUpdateRequest model);
        MotionPicture GetSingleMotionPicture(int id);
        List<MotionPicture> GetAllMotionPictures();
        void DeleteMotionPicture(int id);
    }
}

namespace MotionPicturesCore.StartUp
{
    public class DependencyInjection
    {
        public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            if (configuration is IConfigurationRoot)
            {
                services.AddSingleton<IConfigurationRoot>(configuration as IConfigurationRoot);  
            }

            services.AddSingleton<IConfiguration>(configuration); 

            string connString = configuration.GetConnectionString(\"Default\");

            services.AddSingleton<IDataProvider, SqlDataProvider>(delegate (IServiceProvider provider)
            {
                return new SqlDataProvider(connString);
            }
            );

            services.AddSingleton<IMotionPictureService, IMotionPictureService>();

            GetAllEntities().ForEach(tt =>
            {
                IConfigureDependencyInjection idi = Activator.CreateInstance(tt) as IConfigureDependencyInjection;
                idi.ConfigureServices(services, configuration);
            });
        }

        public static List<Type> GetAllEntities()
        {
            return AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes())
                 .Where(x => typeof(IConfigureDependencyInjection).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract)
                 .ToList();
        }

        public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
        }
    }
}

    标签: c# .net asp.net-mvc


    【解决方案1】:

    在您的 DI 配置中,您设置 IMotionPictureService 以使用 IMotionPictureService 的实例 - 请注意这标志着界面。

    配置通常是这样的:

    services.AddSingleton<IMotionPictureService, MotionPictureService>(); 
    

    这样,您将实现MotionPictureService 绑定到接口IMotionPictureService

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 2012-07-19
      • 1970-01-01
      • 2014-05-28
      • 2021-07-15
      • 1970-01-01
      • 2016-01-06
      相关资源
      最近更新 更多