Exemple #1
0
        public virtual Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
        {
            var log = __log;

            try
            {
                if (context.Exception != null)
                {
                    try
                    {
                        new TelemetryClient().TrackException(context.Exception);
                    }
                    catch
                    {
                        // ignore errors reporting errors
                    }
                }

                var baseController = context.ExceptionContext?.ActionContext?.ControllerContext?.Controller as BaseController;

                if (baseController != null)
                {
                    log = baseController.Log;
                }

                if (context.Exception != null &&
                    ErrorHandlerAttribute.IsAccessDeniedException(context.Exception))
                {
                    context.Result = new UnauthorizedResult(new AuthenticationHeaderValue[0], context.Request);
                }
            }
            catch (Exception ex)
            {
                //We had an error handling the error...
                log.ErrorFormat("Time: {0}, Message: {1}", DateTime.Now, ex);
            }
            return(Task.FromResult(0));
        }
Exemple #2
0
        public void InitializeClient(Type interfaceType)
        {
            ConcurrentDictionary <string, ActionWrapper> concurrentDictionary1;

            if (RestWrapper.cache.TryGetValue(interfaceType, out concurrentDictionary1))
            {
                return;
            }
            ConcurrentDictionary <string, ActionWrapper> concurrentDictionary2 = new ConcurrentDictionary <string, ActionWrapper>();
            IRoutePrefix routePrefix = RestWrapper.GetRoutePrefix(interfaceType);

            this._errorInterceptor = interfaceType.GetCustomAttribute <ErrorHandlerAttribute>();
            this._serviceLocator.GetService <ILogger>()?.Message("Initializing client " + interfaceType.Name);
            RetryAttribute customAttribute = interfaceType.GetCustomAttribute <RetryAttribute>();

            if (interfaceType.GetCustomAttribute <CircuitBreakerAttribute>() != null)
            {
                CircuitBreakerContainer.Register(interfaceType, (ICircuitBreaker) new Stardust.Interstellar.Rest.Client.CircuitBreaker.CircuitBreaker(interfaceType.GetCustomAttribute <CircuitBreakerAttribute>(), this._serviceLocator)
                {
                    ServiceName = interfaceType.FullName
                });
            }
            else
            {
                CircuitBreakerContainer.Register(interfaceType, (ICircuitBreaker) new NullBreaker());
            }
            foreach (MethodInfo methodInfo in interfaceType.GetMethods().Length == 0 ? ((IEnumerable <Type>)interfaceType.GetInterfaces()).First <Type>().GetMethods() : interfaceType.GetMethods())
            {
                try
                {
                    RetryAttribute retryAttribute = methodInfo.GetCustomAttribute <RetryAttribute>() ?? customAttribute;
                    this._serviceLocator.GetService <ILogger>()?.Message("Initializing client action " + interfaceType.Name + "." + methodInfo.Name);
                    IRoute template             = (IRoute)methodInfo.GetCustomAttribute <IRouteAttribute>() ?? (IRoute)methodInfo.GetCustomAttribute <VerbAttribute>();
                    string actionName           = this.GetActionName(methodInfo);
                    List <VerbAttribute> list   = methodInfo.GetCustomAttributes(true).OfType <VerbAttribute>().ToList <VerbAttribute>();
                    ActionWrapper        action = new ActionWrapper()
                    {
                        Name          = actionName,
                        ReturnType    = methodInfo.ReturnType,
                        RouteTemplate = ExtensionsFactory.GetRouteTemplate(routePrefix, template, methodInfo, this._serviceLocator),
                        Parameters    = new List <ParameterWrapper>()
                    };
                    MethodInfo              method           = methodInfo;
                    IServiceProvider        serviceLocator   = this._serviceLocator;
                    List <HttpMethod>       httpMethods      = ExtensionsFactory.GetHttpMethods(list, method, serviceLocator);
                    List <IHeaderInspector> headerInspectors = ExtensionsFactory.GetHeaderInspectors(methodInfo, this._serviceLocator);
                    action.UseXml         = methodInfo.GetCustomAttributes().OfType <UseXmlAttribute>().Any <UseXmlAttribute>();
                    action.CustomHandlers = headerInspectors.Where <IHeaderInspector>((Func <IHeaderInspector, bool>)(h => this.headerHandlers.All <IHeaderHandler>((Func <IHeaderHandler, bool>)(parent => parent.GetType() != h.GetType())))).ToList <IHeaderInspector>();
                    action.ErrorHandler   = this._errorInterceptor;
                    action.Actions        = httpMethods;
                    if (retryAttribute != null)
                    {
                        action.Retry            = true;
                        action.Interval         = retryAttribute.RetryInterval;
                        action.NumberOfRetries  = retryAttribute.NumberOfRetries;
                        action.IncrementalRetry = retryAttribute.IncremetalWait;
                        if (retryAttribute.ErrorCategorizer != (Type)null)
                        {
                            action.ErrorCategorizer = (IErrorCategorizer)Activator.CreateInstance(retryAttribute.ErrorCategorizer, (object)this._serviceLocator);
                        }
                    }
                    ExtensionsFactory.BuildParameterInfo(methodInfo, action, this._serviceLocator);
                    concurrentDictionary2.TryAdd(action.Name, action);
                }
                catch (Exception ex)
                {
                    this._logger?.Error(ex);
                    throw;
                }
            }
            if (RestWrapper.cache.TryGetValue(interfaceType, out concurrentDictionary1))
            {
                return;
            }
            RestWrapper.cache.TryAdd(interfaceType, concurrentDictionary2);
        }