예제 #1
0
        public TOut SafeNetwork <TIn, TOut>(Func <TIn, TOut> iAction, TIn iArgument)
        {
            int tryCounter = 0;

            while (true)
            {
                Notifier.ThrowIfCancellationRequested();
                try
                {
                    return(iAction(iArgument));
                }
                catch (System.ServiceModel.EndpointNotFoundException ex)
                {
                    new SleepWithStopCheck(Notifier).RunSecond(_waitingTimeSecondOnNoNetwork);
                    MyDebug.PrintError("Le web service n'est pas connecté", ex);
                }
                catch (Exception)
                {
                    if (Notifier.IsCanceled)
                    {
                        throw;
                    }

                    if (tryCounter >= _maxRetry)
                    {
                        throw;
                    }
                    else
                    {
                        tryCounter++;
                    }
                    new SleepWithStopCheck(Notifier).RunSecond(10);
                }
            }
        }
예제 #2
0
        private T SafeNetwork <T>(Func <T> iAction)
        {
            int tryCounter = 0;

            while (true)
            {
                Notifier.ThrowIfCancellationRequested();
                try
                {
                    return(iAction());
                }
                catch (StorageException ex)
                {
                    if (Notifier.IsCanceled)
                    {
                        throw;
                    }

                    if (!WithSafeNetwork)
                    {
                        throw;
                    }

                    MyDebug.PrintError("Le BlobContainer n'est pas connnecté", ex);
                    new SleepWithStopCheck(Notifier).RunSecond(RETRYWAITINGTIMESECONDS);
                    continue;
                }
                catch (Exception)
                {
                    if (Notifier.IsCanceled)
                    {
                        throw;
                    }

                    if (!WithSafeNetwork)
                    {
                        throw;
                    }

                    if (tryCounter >= RETRYMAXCOUNT)
                    {
                        throw;
                    }
                    else
                    {
                        tryCounter++;
                        new SleepWithStopCheck(Notifier).RunSecond(RETRYWAITINGTIMESECONDS);
                    }
                }
            }
        }
예제 #3
0
        public async Task <TOut2> SafeNetworkAsync <TIn2, TOut2>(Func <TIn2, Task <TOut2> > iAction, TIn2 iArgument)
        {
            int tryCounter = 0;

            while (true)
            {
                bool waitForNoNetwork = false;
                bool waitForError     = false;

                Notifier.ThrowIfCancellationRequested();
                try
                {
                    return(await iAction(iArgument));
                }
                catch (System.ServiceModel.EndpointNotFoundException ex)
                {
                    waitForNoNetwork = true;
                    MyDebug.PrintError("Le web service n'est pas connecté", ex);
                }
                catch (Exception)
                {
                    if (Notifier.IsCanceled)
                    {
                        throw;
                    }

                    if (tryCounter >= _maxRetry)
                    {
                        throw;
                    }
                    else
                    {
                        tryCounter++;
                    }
                    waitForError = true;
                }

                if (waitForNoNetwork)
                {
                    await Task.Delay(1000 *_waitingTimeSecondOnNoNetwork, Notifier.CancellationTokenSource.Token);
                }

                if (waitForError)
                {
                    await Task.Delay(1000 * 10, Notifier.CancellationTokenSource.Token);
                }
            }
        }
예제 #4
0
        /// <summary>
        /// Execute une action internet avec retour, avec plusieurs tentative
        /// </summary>
        public T Run <T>(Func <T> iAction)
        {
            if (RuntimeConfiguration.GetRuntimeConfiguration() == Tools.Enums.RuntimeConfEnum.Release)
            {
                int tryInternetCount = 1;
                int tryCount         = 1;

                while (true == true)
                {
                    Notifier.ThrowIfCancellationRequested();

                    try
                    {
                        return(iAction());
                    }
                    catch (Exception ex)
                    {
                        if (Notifier.IsCanceled)
                        {
                            throw;
                        }

                        if (tryCount <= _retries)
                        {
                            Uri theUri = null;

                            if (_SiteUrl != null)
                            {
                                theUri = new Uri(_SiteUrl);
                            }

                            if (new WebQuery(Notifier, false, false).IsInternetConnected(theUri) == false)
                            {
                                if (_SiteUrl != null)
                                {
                                    MyDebug.PrintError(" => site '{0}' indisponible => tentative : {1}".FormatString(_SiteUrl, tryInternetCount), ex);
                                }
                                else
                                {
                                    MyDebug.PrintError(" => Internet indisponible => tentative : {0}".FormatString(tryInternetCount), ex);
                                }

                                new SleepWithStopCheck(Notifier).RunSecond(INTERNETSECONDWAITINGTIME);
                                tryInternetCount += 1;
                            }
                            else
                            {
                                MyDebug.PrintError(" => Internet disponible mais chargement impossible => tentative : " + tryCount, ex);
                                new SleepWithStopCheck(Notifier).RunSecond(_sleep);
                                tryCount += 1;
                            }
                        }
                        else
                        {
                            return(default(T));
                        }
                    }
                }
            }
            else
            {
                return(iAction());
            }
        }