Title: Can't inject IRepository
Open Graph Title: Can't inject IRepository
X Title: Can't inject IRepository
Description: Hi, My first post here and I just discovered SharpRepository and trying to use it in our projects. Sorry for the lengthy post, I am trying to provide as much details as possible on all the information I've gathered. I'm using .Net Core 2...
Open Graph Description: Hi, My first post here and I just discovered SharpRepository and trying to use it in our projects. Sorry for the lengthy post, I am trying to provide as much details as possible on all the informat...
X Description: Hi, My first post here and I just discovered SharpRepository and trying to use it in our projects. Sorry for the lengthy post, I am trying to provide as much details as possible on all the informat...
Opengraph URL: https://github.com/SharpRepository/SharpRepository/issues/235
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Can't inject IRepository\u003cXXXXX, int\u003e(but RepositoryFactory and ConfigurationBasedRepository works fine)","articleBody":"Hi,\r\n\r\nMy first post here and I just discovered SharpRepository and trying to use it in our projects.\r\n\r\nSorry for the lengthy post, I am trying to provide as much details as possible on all the information I've gathered.\r\n\r\nI'm using .Net Core 2.2 and I have followed the example of SharpRepository.Samples.CoreMvc. I have changed the .Net version of that code and I made it use 2.2 (it is using 2.1 when download from GitHub). In that project, using .Net Core 2.2 it is possible to inject properly ConfigurationBasedRepository and IRepository\u003cXXXX, int\u003e.\r\n\r\n My issue is that I'm having some problems to inject into my services a repository of type \"IRepository\u003cAppUser, int\u003e\". However a ConfigurationBasedRepository works fine.\r\n\r\nIf I inject a UserRepository, the injector works well:\r\n```\r\n\tpublic class UserRepository : ConfigurationBasedRepository\u003cAppUser, int\u003e {\r\n\tpublic UserRepository(ISharpRepositoryConfiguration configuration, string repositoryName = null) : base(configuration, repositoryName){}\r\n\t}\r\n\tpublic UserService(ApplicationDbContext context, ILogger\u003cUserService\u003e logger){\r\n\t _userRepository = userRepository;\r\n\t}\r\n```\r\n\r\nAlso, when I call directly the RepositoryFactory, I'm able to build a repository of the type I'm interested. The code below works fine as well:\r\n\r\n```\r\npublic UserService(ApplicationDbContext context, ILogger\u003cUserService\u003e logger)\r\n{\r\n _context = context;\r\n _logger = logger;\r\n\r\n var dbContext = _context;\r\n var config = new SharpRepositoryConfiguration();\r\n var coreRepoconfig = new EfCoreRepositoryConfiguration(\"default\", dbContext);\r\n coreRepoconfig.Attributes.Add(\"dbContextType\", \"Base.DataAccessLayer.Identity.ApplicationDbContext, Base.DataAccessLayer\");\r\n config.AddRepository(coreRepoconfig);\r\n var repos = RepositoryFactory.GetInstance\u003cAppUser, int\u003e(config);\r\n}\r\n```\r\n\r\nHOWEVER, if I want to inject the Repository, it crashes and it throws an exception:\r\n\r\n```\r\nprivate IRepository\u003cAppUser, int\u003e _userRepository;\r\n\r\n public UserService(ApplicationDbContext context, ILogger\u003cUserService\u003e logger, IRepository\u003cAppUser, int\u003e userRepository)\r\n {\r\n _userRepository = userRepository;\r\n _context = context;\r\n _logger = logger;\r\n }\r\n```\r\n\r\n\r\n \r\n```\r\n2019-05-31 15:46:57.500 +02:00 [{ IpAddress: \"0.0.0.1\" }] [Error] [] Connection id \"\"0HLN5PPF1NBND\"\", Request id \"\"0HLN5PPF1NBND:00000003\"\": An unhandled exception was thrown by the application.\r\nSystem.InvalidOperationException: Unable to resolve service for type 'SharpRepository.Repository.IRepository`2[Base.DataAccessLayer.Models.AppUser,System.Int32]' while attempting to activate 'Base.BusinessLayer.UserService.UserService'.\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(Type serviceType, Type implementationType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType)\r\n at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)\r\n at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)\r\n at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)\r\n at lambda_method(Closure , IServiceProvider , Object[] )\r\n at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.\u003c\u003ec__DisplayClass4_0.\u003cCreateActivator\u003eb__0(ControllerContext controllerContext)\r\n at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.\u003c\u003ec__DisplayClass5_0.\u003cCreateControllerFactory\u003eg__CreateController|0(ControllerContext controllerContext)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State\u0026 next, Scope\u0026 scope, Object\u0026 state, Boolean\u0026 isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State\u0026 next, Scope\u0026 scope, Object\u0026 state, Boolean\u0026 isCompleted)\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()\r\n at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()\r\n at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)\r\n at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)\r\n at Base.WebServices.Middlewares.JwtTokenMiddleware.Invoke(HttpContext context) in E:\\SampleCode\\Base.Services\\Middlewares\\JwtTokenMiddleware.cs:line 41\r\n at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.InvokeCore(HttpContext context)\r\n at Microsoft.AspNetCore.Server.IISIntegration.IISMiddleware.Invoke(HttpContext httpContext)\r\n at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)\r\n```\r\n\r\n \r\n\r\n\r\n\r\nThis is the code in my startup.cs:\r\n```\r\n public void ConfigureServices(IServiceCollection services)\r\n {\r\n services.AddMvc();\r\n\r\n // Add framework services.\r\n services.AddDbContext\u003cApplicationDbContext\u003e(options =\u003e\r\n options.UseSqlServer(Configuration[\"ConnectionString\"],\r\n sqlOptions =\u003e\r\n {\r\n sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);\r\n //Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency \r\n sqlOptions.EnableRetryOnFailure(15, TimeSpan.FromSeconds(30), null);\r\n sqlOptions.MigrationsAssembly(\"Base.DataAccessLayer\");\r\n }), ServiceLifetime.Transient);\r\n\r\n services.AddIdentity\u003cAppUser, AppRole\u003e()\r\n .AddEntityFrameworkStores\u003cApplicationDbContext\u003e().AddDefaultTokenProviders();\r\n\r\n // configure DI for application services\r\n services.AddScoped\u003cIUserService, UserService\u003e();\r\n\r\n services.AddSingleton\u003cIHttpContextAccessor, HttpContextAccessor\u003e();\r\n\r\n\r\n services.AddTransient\u003cUserRepository\u003e(\r\n r =\u003e new UserRepository(\r\n \tRepositoryFactory.BuildSharpRepositoryConfiguation(Configuration.GetSection(\"sharpRepository\")), \"efCore\"\r\n ));\r\n\r\n // Adding Sharp Repository\r\n services.UseSharpRepository(Configuration.GetSection(\"sharpRepository\"), \"efCore\"); // for Ef Core\r\n\r\n }\r\n```\r\n\r\nAnd finally, this is my appsettings.json:\r\n\r\n```\r\n \"sharpRepository\": {\r\n \"repositories\": {\r\n \"default\": \"efCore\",\r\n \"efCore\": {\r\n \"factory\": \"SharpRepository.EfCoreRepository.EfCoreConfigRepositoryFactory, SharpRepository.EfCoreRepository\",\r\n \"dbContextType\": \"Base.DataAccessLayer.Identity.ApplicationDbContext, Base.DataAccessLayer\",\r\n \"cachingStrategy\": \"none\",\r\n \"cachingProvider\": \"noCachingProvider\"\r\n }\r\n },\r\n \"cachingProviders\": {\r\n \"default\": \"inMemoryProvider\",\r\n \"inMemoryProvider\": {\r\n \"factory\": \"SharpRepository.Repository.Caching.InMemoryConfigCachingProviderFactory, SharpRepository.Repository\"\r\n },\r\n \"noCachingProvider\": {\r\n \"factory\": \"SharpRepository.Repository.Caching.NoCachingConfigCachingProviderFactory, SharpRepository.Repository\"\r\n }\r\n },\r\n \"cachingStrategies\": {\r\n \"default\": \"standard\",\r\n \"standard\": {\r\n \"factory\": \"SharpRepository.Repository.Caching.StandardConfigCachingStrategyFactory, SharpRepository.Repository\",\r\n \"generational\": \"true\",\r\n \"writeThrough\": \"false\"\r\n },\r\n \"timeout\": {\r\n \"factory\": \"SharpRepository.Repository.Caching.TimeoutConfigCachingStrategyFactory, SharpRepository.Repository\",\r\n \"timeout\": \"200\"\r\n },\r\n \"none\": {\r\n \"factory\": \"SharpRepository.Repository.Caching.NoCachingConfigCachingStrategyFactory, SharpRepository.Repository\"\r\n }\r\n }\r\n }\r\n```\r\n\r\n Any advice on how to debug/fix this issue?\r\n\r\n Thanks,\r\n\r\n Juan","author":{"url":"https://github.com/jhuerta","@type":"Person","name":"jhuerta"},"datePublished":"2019-05-31T14:09:58.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/235/SharpRepository/issues/235"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f1d40f24-2760-0d4e-cc0c-cb682e2ddc25 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AE44:1494FD:39E1658:4C52C66:697468A7 |
| html-safe-nonce | 70a45484763e02151273fdbd72ac9d6bfd1040bbeed96991d0afa64f7b81b76c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRTQ0OjE0OTRGRDozOUUxNjU4OjRDNTJDNjY6Njk3NDY4QTciLCJ2aXNpdG9yX2lkIjoiNjM0NzYzOTE4NjIyNzc1MTA3OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3fc9513d45c7de421150ab13712a82ef3bbf6f0abf1e02b095f8f1c3992131b8 |
| hovercard-subject-tag | issue:450818180 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/SharpRepository/SharpRepository/235/issue_layout |
| twitter:image | https://opengraph.githubassets.com/03e067723d869bf0a8ad9b2db27ffb479bba919311001726b4d1cd300f10e6e0/SharpRepository/SharpRepository/issues/235 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/03e067723d869bf0a8ad9b2db27ffb479bba919311001726b4d1cd300f10e6e0/SharpRepository/SharpRepository/issues/235 |
| og:image:alt | Hi, My first post here and I just discovered SharpRepository and trying to use it in our projects. Sorry for the lengthy post, I am trying to provide as much details as possible on all the informat... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jhuerta |
| hostname | github.com |
| expected-hostname | github.com |
| None | e0b95d743b7672c9ac0e1032d5f117950182dc164a83434a7db86510e8f0b37c |
| turbo-cache-control | no-preview |
| go-import | github.com/SharpRepository/SharpRepository git https://github.com/SharpRepository/SharpRepository.git |
| octolytics-dimension-user_id | 1801360 |
| octolytics-dimension-user_login | SharpRepository |
| octolytics-dimension-repository_id | 4514047 |
| octolytics-dimension-repository_nwo | SharpRepository/SharpRepository |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 4514047 |
| octolytics-dimension-repository_network_root_nwo | SharpRepository/SharpRepository |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 56fe7e2e8de6e57740bca50402351ea656f7a4bf |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width