hryz / Dapper.Logging

Integration of the DbConnection and Asp.Net Core Logging
MIT License
43 stars 15 forks source link

Exception when using AddDbConnectionFactoryWithCtx #16

Open rlgordey opened 3 years ago

rlgordey commented 3 years ago

This works:

            //Dapper + Logging
            services.AddDbConnectionFactory(
                prv => new SqlConnection(Configuration.GetConnectionString("Database")),
                options => options
                    .WithLogLevel(LogLevel.Debug)
                    .WithSensitiveDataLogging() //to show values of the query parameters
                    .WithConnectionProjector(c => new { c.DataSource, c.Database })
                , ServiceLifetime.Scoped);

This gives an Exception:

//Include extra context into log messages
            services.AddDbConnectionFactoryWithCtx<LoggingContext>(
                prv => new SqlConnection(Configuration.GetConnectionString("Database")),
                options => options
                    .WithLogLevel(LogLevel.Debug)
                    .WithSensitiveDataLogging() //show values of the query parameters
                    .WithConnectionProjector(c => new { c.DataSource, c.Database })
                , ServiceLifetime.Scoped);

System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: EcommerceWebAPI.Services.Interfaces.IItemService Lifetime: Scoped ImplementationType: EcommerceWebAPI.Services.ItemService': Unable to resolve service for type 'Dapper.Logging.IDbConnectionFactory' while attempting to activate 'DataAccessLayer.DapperFunctions'.) (Error while validating the service descriptor 'ServiceType: DataAccessLayer.DapperFunctions Lifetime: Scoped ImplementationType: DataAccessLayer.DapperFunctions': Unable to resolve service for type 'Dapper.Logging.IDbConnectionFactory' while attempting to activate 'DataAccessLayer.DapperFunctions'.)

---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: EcommerceWebAPI.Services.Interfaces.IItemService Lifetime: Scoped ImplementationType: EcommerceWebAPI.Services.ItemService': Unable to resolve service for type 'Dapper.Logging.IDbConnectionFactory' while attempting to activate 'DataAccessLayer.DapperFunctions'.

---> System.InvalidOperationException: Unable to resolve service for type 'Dapper.Logging.IDbConnectionFactory' while attempting to activate 'DataAccessLayer.DapperFunctions'.

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)