解决ASP.NET核心中的依赖注入

时间:2018-03-03 11:42:41

标签: dependency-injection asp.net-core-webapi

我有三个Repository类,它们是:

 1. ITechniqueRepository 
 2. ITechniqueItemRepository
 3. ITechniqueAssesstmentRepository

还有另一个Service类,即:

ITechniqueService

在ITechniqueService构造函数中,我有以下代码来解析依赖注入。

private readonly ITechniqueRepository _techniqueRepository;
private readonly ITechniqueItemRepository _techniqueItemRepository;
private readonly ITechniqueAssesstmentRepository _techniqueAssesstmentRepository;

    public TechniqueService(            
        ITechniqueRepository techniqueRepository,
        ITechniqueItemRepository techniqueItemRepository,
        ITechniqueAssesstmentRepository techniqueAssesstmentRepository
      ) : base(unitOfWork, settings, logger)
    {
        _techniqueRepository = techniqueRepository;
        _techniqueItemRepository = techniqueItemRepository;
        _techniqueAssesstmentRepository = techniqueAssesstmentRepository;
    }

在我的控制器构造函数中,我有以下代码来解决依赖关系。

private readonly ITechniqueService _techniqueService;
    public TechniqueController(
        ITechniqueService techniqueService
        )
    {
        _techniqueService = techniqueService;
    }

在我的Startup课程中,我有以下代码:

services.AddScoped<ITechniqueRepository, TechniqueRepository>();
services.AddScoped<ITechniqueItemRepository, TechniqueItemRepository>();
services.AddScoped<ITechniqueAssessmentRepository, TechniqueAssessmentRepository>();
services.AddScoped<ITechniqueService, TechniqueService>();  

问题是当我执行我的服务时,我得到以下异常作为响应结果。

  

{

     

“StatusCode”:3,

     

“消息”:“尝试激活时无法解析类型'Data.Repositories.ITechniqueAssesstmentRepository'的服务   'Services.TechniqueService'。“,

     

“MessageDetail”:“at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type   serviceType,Type implementationType,ISet 1 callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type serviceType, Type implementationType, ISet 1 callSiteChain)\ r \ n at   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor   描述符,类型serviceType,ISet 1 callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, ISet 1 callSiteChain)\ r \ n at   Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(类型   serviceType,ISet 1 callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType, ServiceProvider serviceProvider)\r\n at System.Collections.Concurrent.ConcurrentDictionaryExtensions.GetOrAdd[TKey,TValue,TArg](ConcurrentDictionary 2   字典,TKey键,Func`3 valueFactory,TArg arg)\ r \ n at   Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(类型   serviceType)\ r \ n at   Microsoft.Extensions.Internal.ActivatorUtilities.GetService(的IServiceProvider   sp,Type type,Type requiredBy,Boolean   isDefaultParameterRequired)\ r \ n在lambda_method(闭包,   IServiceProvider,Object [])\ r \ n at   Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider&LT;&GT; c__DisplayClass5_0.g__CreateController | 0(ControllerContext   controllerContext)\ r \ n at   Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(州及放大器;   接下来,范围&amp;范围,对象&amp; state,Boolean&amp; isCompleted)\ r \ n at   Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__14.MoveNext(个)\ r \ n ---   从抛出异常的先前位置开始的堆栈跟踪结束   --- \ r \ n在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\ r \ n \ n
  在   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__23.MoveNext()”,       “错误”:{}}

我在哪里弄错了?

0 个答案:

没有答案