示例#1
0
        public override object Execute(IRequestContext requestContext, object instance, T request)
        {
            for (var attempt = 1; attempt <= MaxAttempts; attempt++)
            {
                try
                {
                    try
                    {
                        var customerId  = requestContext.GetHeader("CustomerId");
                        var environment = ConfigurationManager.AppSettings["environment"];
                        if (environment != "Debug")
                        {
                            Membership.ApplicationName = customerId;
                            Roles.ApplicationName      = customerId;
                        }
                        if (instance.GetType() != typeof(AuthService))
                        {
                            var clientApplicationType = requestContext.GetHeader("ClientApplicationType");

                            string conn;
                            if ((conn = GetCustomerConnectionString(customerId)) == null)
                            {
                                throw new Exception("Invalid customer: " + customerId);
                            }

                            TenantConnectionProvider.DynamicString   = conn;
                            TenantConnectionProvider.ApplicationType = Int32.Parse(clientApplicationType);
                        }
                        else
                        {
                            TenantConnectionProvider.DynamicString = ConfigurationManager.ConnectionStrings["TrexBase"].ConnectionString;
                        }

                        var watch    = Stopwatch.StartNew();
                        var response = base.Execute(requestContext, instance, request);
                        if (attempt > 1)
                        {
                            Log.InfoFormat("Succeded after at least one retry exception. Attempts: {0}. Last exception: {1}",
                                           attempt, GetExceptionInfo(_lastException));
                        }
                        watch.Stop();
                        Log.InfoFormat("Request={1};ElapsedMs={0}", watch.ElapsedMilliseconds, request.GetType());
                        return(response);
                    }
                    catch (InvalidStateException ex)
                    {
                        var invalidValues = ex.GetInvalidValues().GroupBy(i => i.Entity);
                        throw new DomainException(@"Could not save an entity because of the following invalid values: {0}", string.Join(Environment.NewLine, invalidValues.Select(GenerateEntityErrorDescriptions)));
                    }
                }
                catch (Exception ex)
                {
                    _sessionManager.ErrorHappened();
                    Log.DebugFormat("Got exception when in Handle() (exception was handled): {0}", GetExceptionInfo(ex));

                    if (attempt == MaxAttempts)
                    {
                        Log.ErrorFormat("Giving up after {0} attempts. ExceptionInfo: {1}", attempt, GetExceptionInfo(ex));
                        return(FinalExceptionHandling(requestContext, request, ex));
                    }

                    if (!IsRetryException(ex))
                    {
                        if (IsAuthenticationError(ex))
                        {
                            Log.Info("Got authentification denail", ex);
                        }
                        else if (!IsDomainException(ex)) // only log non-domain exceptions as errors
                        {
                            Log.ErrorFormat("Got exception that should not be retried and is rethrown: {0}", GetExceptionInfo(ex));
                        }
                        else
                        {
                            Log.WarnFormat("Got domain exception: {0}", GetExceptionInfo(ex));
                        }

                        return(FinalExceptionHandling(requestContext, request, ex));
                    }
                    _lastException = ex;
                    var sleeptime = SleeptimeMs();
                    Log.DebugFormat("Retrying in {0} ms", sleeptime);
                    Thread.Sleep(sleeptime); // Sleep a bit before trying again. Raises the chances of success next time.
                }
                finally
                {
                    _sessionManager.Close();
                }
            }
            throw new Exception("Reached point that should be unreachable!!");
        }