sebvst
sebvst

Reputation: 753

Resolution of the dependency failed

I have the following code:

CancellationPolicyService

using MyApp.Model.Models;
using Repository.Pattern.Repositories;
using Service.Pattern;

namespace MyApp.Service
{
    public interface ICancellationPolicyService :  IService<CancellationPolicy>
    {
    }

    public class CancellationPolicyService : Service<CancellationPolicy>, ICancellationPolicyService
    {
        public CancellationPolicyService(IRepositoryAsync<CancellationPolicy> repository)
        : base(repository)
        {
        }
    }
}

Inside UnityConfig.cs:

.RegisterType<ICancellationPolicyService, CancellationPolicyService>()

In DataCacheService:

namespace MyApp.Service
{
    public class DataCacheService
    {
    private ICancellationPolicyService CancellationPolicyService
        {
            get { return _container.Resolve<ICancellationPolicyService>(); }
        }

        public DataCacheService(IUnityContainer container)
        {
            _container = container;

            MainCache = new MemoryCache("MainCache");

            GetCachedItem(CacheKeys.CancellationPolicies);
        }
    public object GetCachedItem(CacheKeys CacheKeyName)
        {
            lock (_lock)
            {
                if (!MainCache.Contains(CacheKeyName.ToString()))
                {
                    switch (CacheKeyName)
                    {
                        case CacheKeys.CancellationPolicies:
                            var cancellationpolicies = CancellationPolicyService.Queryable().ToList();
                            UpdateCache(CacheKeys.CancellationPolicies, cancellationpolicies);
                            break;
                        }
                };
                return MainCache[CacheKeyName.ToString()] as Object;
            }
        }
    }
}

And when I call DataCacheService I get an error saying the following:

InvalidOperationException - The current type, Repository.Pattern.Repositories.IRepositoryAsync`1[MyApp.Model.Models.CancellationPolicy], is an interface and cannot be constructed. Are you missing a type mapping?

Do you have an idea, why that is? I would be thankful for any kind of hint.

Upvotes: 1

Views: 3154

Answers (2)

smoksnes
smoksnes

Reputation: 10851

It sounds like you haven't registered IRepositoryAsync<CancellationPolicy>. Add that registration to your unity registration as well.

Assuming that the implementation of IRepositoryAsync<CancellationPolicy> is CancellationPolicyRepository:

.RegisterType<IRepositoryAsync<CancellationPolicy>, CancellationPolicyRepository>()

Or someting like this if you have a generic repository.

.RegisterType<IRepositoryAsync<CancellationPolicy>, MyGenericRepository<CancellationPolicyRepository>>()

Upvotes: 1

MRebati
MRebati

Reputation: 599

use this one:

RegisterType<yourInterface>().As<yourClass>().AsSelf();

it might work.

Upvotes: 1

Related Questions