C# 如何将IInterceptionBehavior与构造函数、属性和Unity一起使用?

C# 如何将IInterceptionBehavior与构造函数、属性和Unity一起使用?,c#,asp.net-mvc-4,dependency-injection,unity-container,C#,Asp.net Mvc 4,Dependency Injection,Unity Container,我想构建一个具有IIInterceptionBehavior的缓存类,如本文中所述,并将其用作属性 但是当我测试这个时,我有一个错误:“CacheAction”类型没有一个可访问的构造函数。我需要你的帮助来了解原因。“CacheAction”是我给构造函数的枚举 下面是没有私有方法的缓存类的代码: /// <summary> /// Cache action types. /// </summary> public enum CacheAction { /

我想构建一个具有IIInterceptionBehavior的缓存类,如本文中所述,并将其用作属性

但是当我测试这个时,我有一个错误:“CacheAction”类型没有一个可访问的构造函数。我需要你的帮助来了解原因。“CacheAction”是我给构造函数的枚举

下面是没有私有方法的缓存类的代码:

    /// <summary>
/// Cache action types.
/// </summary>
public enum CacheAction
{
    /// <summary>
    /// Add a new item to cache.
    /// </summary>
    Add,
    /// <summary>
    /// Remove all associated items from cache for the given domain model.
    /// </summary>
    Remove
}

[Serializable]
public class CacheAttribute : FilterAttribute, ICache, IInterceptionBehavior
{
    private readonly ICache _cache;

    [NonSerialized]
    private object _syncRoot;
    public readonly CacheAction _action;

    public CacheAttribute(CacheAction action)
    {
        _cache = new StaticMemoryCache();
        _action = action;
        _syncRoot = new object();
    }

    public IMethodReturn Invoke(IMethodInvocation input,
    GetNextInterceptionBehaviorDelegate getNext)
    {
        if (_action == CacheAction.Add)
        {
            var cacheKey = BuildCacheKey(input);

            // If cache is empty, execute the target method, retrieve the return value and cache it.
            if (_cache[cacheKey] == null)
            {
                lock (_syncRoot)
                {
                    // Execute the target method
                    IMethodReturn returnVal = getNext()(input, getNext);
                    // Cache the return value
                    _cache[cacheKey] = returnVal;

                    return returnVal;
                }
            }
            // Otherwise, return the cache result
            return input.CreateMethodReturn(_cache[cacheKey]);
        }
        else
        {
            var typeName = GetTypeName(input.GetType());

            lock (_syncRoot)
            {
                _cache.Remove(typeName);
            }

            IMethodReturn returnVal = getNext()(input, getNext);
            return returnVal;
        }
    }
//
///缓存操作类型。
/// 
公共枚举缓存操作
{
/// 
///将新项添加到缓存。
/// 
添加
/// 
///从给定域模型的缓存中删除所有关联项。
/// 
去除
}
[可序列化]
公共类CacheAttribute:FilterAttribute、ICache、IInterceptionBehavior
{
专用只读ICache\u缓存;
[非串行化]
私有对象\u syncRoot;
公共只读缓存操作_操作;
公共缓存属性(缓存操作)
{
_缓存=新的StaticMemoryCache();
_行动=行动;
_syncRoot=新对象();
}
公共IMethodReturn调用(IMethodInvoke输入,
GetNextInterceptionBehaviorDelegate getNext)
{
if(_action==CacheAction.Add)
{
var cacheKey=BuildCacheKey(输入);
//如果缓存为空,则执行目标方法,检索返回值并缓存它。
if(_cache[cacheKey]==null)
{
锁定(\u syncRoot)
{
//执行目标方法
IMethodReturn returnVal=getNext()(输入,getNext);
//缓存返回值
_cache[cacheKey]=returnVal;
返回值;
}
}
//否则,返回缓存结果
return input.CreateMethodReturn(_cache[cacheKey]);
}
其他的
{
var typeName=GetTypeName(input.GetType());
锁定(\u syncRoot)
{
_cache.Remove(typeName);
}
IMethodReturn returnVal=getNext()(输入,getNext);
返回值;
}
}
这里是Unity配置的代码(查看InterfaceInterceptor调用):

container.AddNewExtension();
容器
//.RegisterType()
.RegisterType(新PerRequestLifetimeManager())
.RegisterType(
新建PerRequestLifetimeManager(),
新注入构造函数(新对象[]{new RepositoryFactories()})
)
.RegisterType(新PerRequestLifetimeManager())
.RegisterType(
新的拦截器(),
新的侦听行为()
.RegisterType()
.RegisterType(新的PerRequestLifetimeManager());
DependencyResolver.SetResolver(新的Microsoft.Practices.Unity.Mvc.UnityDependencyResolver(容器));
这里是我使用存储库进行测试的地方:

public static class MovieRepository
{
    [Cache(CacheAction.Add)]
    public static int test(this IRepositoryAsync<Movie> repository)
    {
        return 1;
    }
}
公共静态类MovieRepository
{
[缓存(CacheAction.Add)]
公共静态int测试(此IRepositoryAsync存储库)
{
返回1;
}
}
因此,需要帮助了解我缺少什么才能得到错误:类型“CacheAction”没有可访问的构造函数

谢谢,

大卫

public static class MovieRepository
{
    [Cache(CacheAction.Add)]
    public static int test(this IRepositoryAsync<Movie> repository)
    {
        return 1;
    }
}