コード例 #1
0
            protected override IAsyncPolicy GetAsyncPolicy(CallContextBase context, PolicyAttribute attribute)
            {
                var testAttribute = (TestPolicyAttribute)attribute;
                var policy        = new AsyncTestPolicy(testAttribute.Name, testAttribute.Order, false);

                return(policy);
            }
コード例 #2
0
 public virtual ISyncPolicy GetSyncPolicy(CallContextBase context, PolicyAttribute attribute)
 {
     if (attribute is TAttribute)
     {
         return(GetSyncPolicy(context, (TAttribute)attribute));
     }
     return(null);
 }
コード例 #3
0
        public override IAsyncPolicy GetAsyncPolicy(CallContextBase context, CacheAttribute attribute)
        {
            var methodInfo       = context.Method;
            var cacheKeyStrategy = GetCacheKeyStrategy(methodInfo, attribute);
            var ttlStrategy      = GetTimeToLiveStrategy(methodInfo, attribute);

            return(Policy.CacheAsync(_asyncCacheProvider, ttlStrategy, cacheKeyStrategy, OnCacheGet, OnCacheMiss, OnCachePut, OnCacheGetError, OnCachePutError));
        }
コード例 #4
0
        public IAsyncPolicy GetAsyncPolicy(CallContextBase context)
        {
            var iface = DispatchProxy.Create <IAsyncPolicy, Proxy <IAsyncPolicy> >();
            var proxy = iface as Proxy <IAsyncPolicy>;

            proxy.Callback       = _callback;
            proxy.Implementation = Policy.NoOpAsync();
            return(iface);
        }
コード例 #5
0
        protected virtual IAsyncPolicy GetAsyncPolicy(CallContextBase context, PolicyAttribute attribute)
        {
            var policies = _attributePolicyProviders
                           .Select(provider => provider.GetAsyncPolicy(context, attribute))
                           .Where(policy => policy != null)
                           .ToArray();

            if (!policies.Any())
            {
                return(null);
            }
            if (policies.Length == 1)
            {
                return(policies.Single());
            }

            return(Policy.WrapAsync(policies));
        }
コード例 #6
0
        private TPolicy GetPolicy <TPolicy>(CallContextBase context, TAttribute attribute, ConcurrentDictionary <object, TPolicy> policies, Func <CallContextBase, TAttribute, TPolicy> getNewPolicy) where TPolicy : IsPolicy
        {
            var scopeId = Guid.NewGuid();

            while (!_scopes.TryAdd(scopeId, null)) // Reserve the id so we can't get a collision due to a race
            {
                scopeId = Guid.NewGuid();
            }

            var scope = _scopeProvider.GetScope(attribute, context, () => OnScopeComplete(scopeId));

            _scopes[scopeId] = scope; // Now record the scope that matches with this id

            lock (scope)              // Because GetOrAdd can call the add func and still fail
            {
                var policy = policies.GetOrAdd(scope, _ => getNewPolicy(context, attribute));
                return(policy);
            }
        }
コード例 #7
0
        public object GetScope(PolicyAttribute attribute, CallContextBase context, Action onScopeComplete)
        {
            switch (_scope)
            {
            case Scopes.Global:
                return(GlobalScopeProvider.Instance.GetScope(attribute, context, onScopeComplete));

            case Scopes.OnePerInterfaceType:
                return(OnePerInterfaceTypeScopeProvider.Instance.GetScope(attribute, context, onScopeComplete));

            case Scopes.OnePerConcreteType:
                return(OnePerConcreteTypeScopeProvider.Instance.GetScope(attribute, context, onScopeComplete));

            case Scopes.OnePerMethod:
                return(OnePerMethodScopeProvider.Instance.GetScope(attribute, context, onScopeComplete));

            default:
                throw new NotSupportedException($"Unsupported {nameof(Scopes)} value {_scope}");
            }
        }
コード例 #8
0
        public ISyncPolicy GetSyncPolicy(CallContextBase context)
        {
            var methodInfo = context.Method;
            var attributes = methodInfo.GetCustomAttributes <PolicyAttribute>();
            var policies   = attributes
                             .OrderBy(attribute => attribute.GetOrder())
                             .Select(attribute => GetSyncPolicy(context, attribute))
                             .Where(policy => policy != null)
                             .ToArray();

            if (!policies.Any())
            {
                return(null);
            }

            if (policies.Length == 1)
            {
                return(policies.Single());
            }

            return(Policy.Wrap(policies));
        }
コード例 #9
0
        public IAsyncPolicy GetAsyncPolicy(CallContextBase context)
        {
            var methodInfo = context.Method;
            var attributes = methodInfo.GetCustomAttributes <PolicyAttribute>();
            var policies   = attributes
                             .GroupBy(attribute => attribute.GetOrder())
                             .OrderBy(g => g.Key)    // Always use the specified order, if there is one
                             .SelectMany(CustomSort) // Then sort to break the ties
                             .Select(attribute => GetAsyncPolicy(context, attribute))
                             .Where(policy => policy != null)
                             .ToArray();

            if (!policies.Any())
            {
                return(null);
            }

            if (policies.Length == 1)
            {
                return(policies.Single());
            }

            return(Policy.WrapAsync(policies));
        }
コード例 #10
0
 public ISyncPolicy GetSyncPolicy(CallContextBase context)
 {
     return(null);
 }
コード例 #11
0
 public IAsyncPolicy GetAsyncPolicy(CallContextBase context, PolicyAttribute attribute)
 {
     return(null);
 }
コード例 #12
0
 public IAsyncPolicy GetAsyncPolicy(CallContextBase context)
 {
     return(Policy.NoOpAsync());
 }
コード例 #13
0
 public abstract IAsyncPolicy GetAsyncPolicy(CallContextBase context, TAttribute attribute);
コード例 #14
0
 public IAsyncPolicy GetAsyncPolicy(CallContextBase context, TestAttribute attribute)
 {
     throw new NotImplementedException();
 }
コード例 #15
0
 public ISyncPolicy GetSyncPolicy(CallContextBase context, PolicyAttribute attribute)
 {
     throw new NotImplementedException();
 }
コード例 #16
0
 public ISyncPolicy GetSyncPolicy(CallContextBase context)
 {
     return(Policy.NoOp());
 }
コード例 #17
0
 public override IAsyncPolicy GetAsyncPolicy(CallContextBase context, TAttribute attribute)
 {
     return(GetPolicy(context, attribute, _asyncPolicies, (ctx, attr) => _inner.GetAsyncPolicy(ctx, attribute)));
 }
コード例 #18
0
 public object GetScope(PolicyAttribute attribute, CallContextBase context, Action onScopeComplete)
 {
     return(context.ServiceType);
 }
コード例 #19
0
 public string GetCacheKey(CallContextBase context)
 {
     return(GetCacheKey(context.Method, context.ParameterTypes, context.Arguments));
 }
コード例 #20
0
 public override IAsyncPolicy GetAsyncPolicy(CallContextBase context, TestAttribute attribute)
 {
     return(Policy.NoOpAsync());
 }
コード例 #21
0
 public object GetScope(PolicyAttribute attribute, CallContextBase context, Action onScopeComplete)
 {
     return(typeof(object));
 }
コード例 #22
0
 public object GetScope(PolicyAttribute attribute, CallContextBase context, Action onScopeComplete)
 {
     return(context.Instance.GetType());
 }
コード例 #23
0
 private void OnCacheMiss(CallContextBase context, string key)
 {
     OnCacheMiss(context, context.Method, context.ParameterTypes, context.Arguments, key);
 }
コード例 #24
0
 private void OnCacheGetError(CallContextBase context, string key, Exception exception)
 {
     OnCacheGetError(context, context.Method, context.ParameterTypes, context.Arguments, key, exception);
 }