InvalidOperationException:尝试激活'XXX.CustomExceptionFilter时无法解析类型'YYY.ILogDataAccess'的服务

时间:2017-12-28 10:47:44

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

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

  

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

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

  1. DataAccess(与数据库交互)
  2. 记录(以多种方式登录)
  3. Api(或服务)
  4. 在我的Api中,来自CustomExceptionFilter的{​​{1}}。我正在处理来自IExceptionFilter的所有异常。

    我需要在OnException()内创建日志。要记录,我需要访问DB。

    为此目的,在OnExceptionStartup.cs中,我注入了ConfigureServices()的依赖关系,因为它需要CustomExceptionFilter方法中的LoggingWrapper。

    以下是代码:

    OnException()

    此处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很好地实例化了。在Startup.cs中完成其他语句之后,当它试图从“logDataAccess”的主入口点出来时, 它显示错误:

    这是错误的堆栈跟踪。

    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() 中添加了CustomExceptionFilter 也是这样。

    ConfigureServices()

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

1 个答案:

答案 0 :(得分:0)

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

变化:

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

为:

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