Base solution for your next web application
Open Closed

DI Error when initializing a generic class #6826


User avatar
0
antonis created

I have the following code and I am getting the following error when I call a method of the AppService1: MyProject.Application.Extended.Settings.AppService1' is waiting for the following dependencies:

  • Service 'MyProject.Application.Extended.Caches.GenericCache`3[[MyProject.Core.Extended.Country, MyProject.Core.Extended, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[MyProject.Application.Extended.Countrys.Dto.CountryDto, MyProject.Application.Extended, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.Int64, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' which was not registered.
public interface IGenericCache : ITransientDependency
    {
    }
    
      public interface IGenericCache<TEntity, TModelDto, TPrimaryKey> 
        : IGenericCache where TEntity : class, IEntity<TPrimaryKey>
    {
        TModelDto Get(TPrimaryKey key);
        IList<TModelDto> GetAll();
    }
    
    
    public class GenericCache<TEntity, TModelDto, TPrimaryKey> : IGenericCache<TEntity, TModelDto, TPrimaryKey> 
        where TEntity : class, IEntity<TPrimaryKey>
    {
        public readonly IList<TModelDto> CacheItemsAsList;
        public readonly ConcurrentDictionary<TPrimaryKey, TModelDto> CacheItems;

        //-------------------------------------------------------------------------------------
        public GenericCache(IObjectMapper objectMapper,
                                IRepository<TEntity, TPrimaryKey> repo)
        {
            var tempList = new List<TModelDto>();
            CacheItems = new ConcurrentDictionary<TPrimaryKey, TModelDto>();

            IList<TEntity> dbEntities = repo.GetAllList();
            if (dbEntities != null)
            {
                foreach (var dbEntity in dbEntities)
                {
                    TModelDto mappedItem = objectMapper.Map<TModelDto>(dbEntity);
                    CacheItems.TryAdd(dbEntity.Id, mappedItem);
                    tempList.Add(mappedItem);
                }
            }
            CacheItemsAsList = tempList;
        }

        //-------------------------------------------------------------------------------------
        public IList<TModelDto> GetAll()
        {
            return CacheItemsAsList;
        }

        //-------------------------------------------------------------------------------------
        public TModelDto Get(TPrimaryKey key)
        {
            CacheItems.TryGetValue(key, out TModelDto modelDto);
            return modelDto;
        }
    }
    
        public class AppService1 : KYCAppServiceBase
    {
        private readonly GenericCache<Country, CountryDto, int> genericCache;

        //-----------------------------------------------------------------------------------------------------------------------
        public AppService1(GenericCache<Country, CountryDto, int> genericCache)
        {
            this.genericCache = genericCache;
        }

Can anyone help?

Thanks in advance


2 Answer(s)
  • User Avatar
    0
    maliming created
    Support Team

    Try to manually register to the DI system

    IocManager.Register(typeof(IGenericCache<,,>),
                    typeof(GenericCache<,,>),
                    DependencyLifeStyle.Transient);
    

    see: https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2125

  • User Avatar
    0
    antonis created

    Thanks you. That did the trick