【问题标题】:InvalidOperationException: Unable to resolve service for type 'YYY.ILogDataAccess' while attempting to activate 'XXX.CustomExceptionFilterInvalidOperationException:尝试激活“XXX.CustomExceptionFilter”时无法解析“YYY.ILogDataAccess”类型的服务
【发布时间】:2018-06-08 22:01:44
【问题描述】:

我知道互联网上有很多关于这个问题的问题和信息。但是搜索了4-5个小时,我无法解决问题。寻找直接或间接的帮助/方向。

我正在使用:VS 2017 ASP .Net Core 2.0

比如说,我在一个解决方案中有 3 个项目。

  1. DataAccess(与数据库交互)
  2. 记录(以多种方式登录)
  3. API(或服务)

在我的 Api 中,有一个来自 IExceptionFilterCustomExceptionFilter。我正在处理来自OnException() 的所有异常。

我需要在OnException 中创建日志。而要记录,我需要访问数据库。

为此,我在Startup.csConfigureServices() 中注入了CustomExceptionFilter 的依赖项,因为它需要OnException() 方法中的LoggingWrapper。

代码如下:

services.AddSingleton<IExceptionFilter>(
new CustomExceptionFilter(
new LogDataAccess(
appServiceConfig.ConnectionString, appServiceConfig.Database)));

这里的LogDataAccess 是将日志插入数据库的类。

我已经覆盖了CustomExceptionFilter 类中的构造函数,例如:

private ILogDataAccess logDataAccess;

public CustomExceptionFilter(ILogDataAccess logDataAccess)
{
    this.logDataAccess = logDataAccess;
}

构建项目很好。当我跑步时,startup.cs 到目前为止执行得很好。 logDataAccess 很好地实例化了。完成 Startup.cs 中的其他语句后,当它试图从“program.cs”的主入口点出来时, 它显示错误:

这是错误的堆栈跟踪。

    System.InvalidOperationException: Unable to resolve service for type 'DataAccess.ILogDataAccess' while attempting to activate 'QuestionService.Common.CustomExceptionFilter'.

   at Microsoft.Extensions.Internal.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)

   at lambda_method(Closure , IServiceProvider , Object[] )

   at Microsoft.AspNetCore.Mvc.TypeFilterAttribute.CreateInstance(IServiceProvider serviceProvider)

   at Microsoft.AspNetCore.Mvc.Internal.DefaultFilterProvider.ProvideFilter(FilterProviderContext context, FilterItem filterItem)

   at Microsoft.AspNetCore.Mvc.Internal.DefaultFilterProvider.OnProvidersExecuting(FilterProviderContext context)

   at Microsoft.AspNetCore.Mvc.Internal.FilterFactory.CreateUncachedFiltersCore(IFilterProvider[] filterProviders, ActionContext actionContext, List`1 filterItems)

   at Microsoft.AspNetCore.Mvc.Internal.FilterFactory.GetAllFilters(IFilterProvider[] filterProviders, ActionContext actionContext)

   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvokerCache.GetCachedResult(ControllerContext controllerContext)

   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvokerProvider.OnProvidersExecuting(ActionInvokerProviderContext context)

   at Microsoft.AspNetCore.Mvc.Internal.ActionInvokerFactory.CreateInvoker(ActionContext actionContext)

   at Microsoft.AspNetCore.Mvc.Internal.MvcAttributeRouteHandler.<>c__DisplayClass12_0.<RouteAsync>b__0(HttpContext c)

   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.<Invoke>d__7.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.<Invoke>d__6.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.<Invoke>d__7.MoveNext()

我已经用同样的方法添加了CustomExceptionFilterConfigureServices() 也是这样。

   services.AddMvc(
            config =>
            {
                config.Filters.Add(typeof(CustomExceptionFilter));
            }
        );

这是为全局异常过滤器添加的。

【问题讨论】:

  • 似乎是依赖问题,您在ConfigureServices() startup.cs 中注册了您的ILogDataAccess 吗?类似services.AddScoped&lt;ILogDataAccess, IMPLEMENTEDCLASS&gt;();
  • 到目前为止我发现,由于CustomExceptionFilter的构造函数,错误正在显示。

标签: c# dependency-injection asp.net-core asp.net-core-2.0


【解决方案1】:

您需要在 DI 中注册 LogDataAccess 而不是 CustomExceptionFilter

改变:

services.AddSingleton<IExceptionFilter>(new CustomExceptionFilter(new LogDataAccess(
    appServiceConfig.ConnectionString, appServiceConfig.Database)));

到:

services.AddSingleton<ILogDataAccess>(new LogDataAccess(
    appServiceConfig.ConnectionString, appServiceConfig.Database));

【讨论】:

    猜你喜欢
    • 2020-02-08
    • 2021-04-29
    • 2020-07-01
    • 2021-02-23
    • 2020-05-18
    • 2018-07-07
    • 2019-08-21
    • 2020-08-21
    • 1970-01-01
    相关资源
    最近更新 更多