解决依赖性失败

时间:2016-06-06 19:18:06

标签: asp.net-mvc unity-container

我有以下代码:

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)
        {
        }
    }
}

UnityConfig.cs

.RegisterType<ICancellationPolicyService, CancellationPolicyService>()

在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;
            }
        }
    }
}

当我调用DataCacheService时,出现以下错误:

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?

你知道吗,为什么会这样?我会感激任何暗示。

2 个答案:

答案 0 :(得分:1)

使用这个:

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

它可能有用。

答案 1 :(得分:1)

听起来你没有注册IRepositoryAsync<CancellationPolicy>。将该注册添加到您的团结注册中。

假设IRepositoryAsync<CancellationPolicy>的实施是CancellationPolicyRepository

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

如果您有通用存储库,或者像这样。

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