public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF RunAsync(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, Action completeCallback) { return(aspect.Combine(work => work.BeginInvoke(asyncresult => { work.EndInvoke(asyncresult); completeCallback(); }, null))); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF CacheRetry <TReturnType>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ICacheWrapper cacheResolver, ILogger logger, string key) { return(aspect.Combine(work => { try { Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached); } catch (Exception x) { logger.Error(x); System.Threading.Thread.Sleep(1000); //Retry try { Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached); } catch (Exception ex) { logger.Error(ex); throw; } } })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF HowLong(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger, Action invokeThisAfterLogedStartMessage, string startMessage, string endMessage) { return(aspect.Combine(work => { Stopwatch watch = new Stopwatch(); watch.Start(); logger.Info(startMessage); aspect.TrapLog().Do(invokeThisAfterLogedStartMessage); try { work(); } finally { watch.Stop(); TimeSpan duration = watch.Elapsed; logger.Info(string.Format(endMessage, duration.TotalMilliseconds, duration.TotalSeconds, duration.TotalMinutes, duration.TotalHours, duration.TotalDays)); } })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Delay(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, int milliseconds) { return(aspect.Combine(work => { System.Threading.Thread.Sleep(milliseconds); work(); })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Log(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger, string logMessage, params object[] arg) { return(aspect.Combine(work => { logger.Info(string.Format(logMessage, arg)); work(); })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF While(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, Func <bool> test) { return(aspect.Combine(work => { while (test()) { work(); } })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF CacheList <TItemType, TListType>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ICacheWrapper cacheResolver, string listCacheKey, Func <TItemType, string> getItemKey) where TListType : IList <TItemType>, new() { return(aspect.Combine(work => { var workDelegate = aspect.WorkDelegate as Func <TListType>; // Replace the actual work delegate with a new delegate so that // when the actual work delegate returns a collection, each item // in the collection is stored in cache individually. Func <TListType> newWorkDelegate = () => { if (workDelegate != null) { TListType collection = workDelegate(); foreach (TItemType item in collection) { string key = getItemKey(item); cacheResolver.Save(key, item); } return collection; } return default(TListType); }; aspect.WorkDelegate = newWorkDelegate; // Get the collection from cache or real source. If collection is returned // from cache, resolve each item in the collection from cache Cache <TListType>(aspect, cacheResolver, listCacheKey, work, cached => { // Get each item from cache. If any of the item is not in cache // then discard the whole collection from cache and reload the // collection from source. var itemList = new TListType(); foreach (TItemType cachedItem in cached.Select(item => cacheResolver.Get <TItemType>(getItemKey(item)))) { if (null != cachedItem) { itemList.Add(cachedItem); } else { // One of the item is missing from cache. So, discard the // cached list. return default(TListType); } } return itemList; }); })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF WhenTrue(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, params Func <bool>[] conditions) { return(aspect.Combine(work => { if (conditions.Any(condition => !condition())) { return; } work(); })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF TrapLog(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger) { return(aspect.Combine(work => { try { work(); } catch (Exception x) { logger.Error(x); } })); }
///<summary> /// Transaction /// (The "Distributed Transaction Coordinator" service must be started) ///</summary> ///<param name="aspect"></param> ///<param name="transactionScopeOption"></param> ///<param name="transactionOptions"></param> ///<param name="complete"></param> ///<returns></returns> public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Transaction(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, TransactionScopeOption transactionScopeOption, TransactionOptions transactionOptions, bool complete = true) { return(aspect.Combine(work => { using (var scope = new TransactionScope(transactionScopeOption, transactionOptions)) { work(); if (complete) { scope.Complete(); } } })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF TrapLog(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger, Action <Exception> errorhandler) { return(aspect.Combine(work => { try { work(); } catch (Exception x) { Let.Us.TrapLog().Do(() => errorhandler.Invoke(x)); } })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Expected <TException>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect) where TException : Exception { return(aspect.Combine(work => { try { work(); } catch (TException x) { Debug.WriteLine(x.ToString()); } })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF TrapLogThrow(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, Action <Exception> errorhandler) { return(aspect.Combine(work => { try { work(); } catch (Exception x) { errorhandler.Invoke(x); throw; } })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF MustBeNonNull(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, Exception thrownExp, params object[] args) { return(aspect.Combine(work => { for (var i = 0; i < args.Length; i++) { var arg = args[i]; if (arg == null) { throw thrownExp; } } work(); })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Log(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger, Action beforeAction, Action afterAction) { return(aspect.Combine(work => { try { Let.Us.TrapLog().Do(beforeAction); Let.Us.TrapLogThrow().Do(work); } finally { Let.Us.TrapLog().Do(afterAction); } })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF MustBeNonNull(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, params object[] args) { return(aspect.Combine(work => { for (var i = 0; i < args.Length; i++) { var arg = args[i]; if (arg == null) { throw new ArgumentException( string.Format("Parameter at index {0} is null", i)); } } work(); })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF MustBeNonDefault <T>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, params T[] args) where T : IComparable { return(aspect.Combine(work => { for (var i = 0; i < args.Length; i++) { T arg = args[i]; if (arg == null || arg.Equals(default(T))) { throw new ArgumentException( string.Format("Parameter at index {0} is null", i)); } } work(); })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Log(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ILogger logger, string beforeMessage, string afterMessage) { return(aspect.Combine(work => { bool hasError = true; try { logger.Info(beforeMessage); Let.Us.Do(work); hasError = false; } finally { afterMessage = hasError ? afterMessage + "(Error Occured !!!)" : afterMessage; logger.Info(afterMessage); } })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF HowLong(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, Action <TimeSpan> onSuccessHandler, Action <TimeSpan, Exception> onFailHandler, Action <Exception> onErrorhandler = null) { var retValue = aspect.Combine(work => { Stopwatch watch = new Stopwatch(); watch.Start(); var errorOccured = false; Exception exc = null; try { work(); } catch (Exception exception) { exc = exception; errorOccured = true; onErrorhandler?.Invoke(exception); } finally { watch.Stop(); TimeSpan duration = watch.Elapsed; if (errorOccured) { onFailHandler(duration, exc); } else { onSuccessHandler(duration); } } }); return(retValue); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF CallWithTimeout(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, TimeSpan timeout) { return(aspect.Combine(work => { Thread threadToKill = null; Action wrappedAction = () => { threadToKill = Thread.CurrentThread; work(); }; Stopwatch watch = new Stopwatch(); watch.Start(); IAsyncResult result = wrappedAction.BeginInvoke(null, null); if (result.AsyncWaitHandle.WaitOne(timeout)) { wrappedAction.EndInvoke(result); watch.Stop(); } else { threadToKill.Abort(); watch.Stop(); throw new TimeoutException(string.Format("Elapsed Time (sec):{0}", watch.Elapsed.TotalSeconds)); } //var thread = new Thread(() => work()); //thread.Start(); //var Completed = thread.Join(timeout); //if (!Completed) //{ // thread.Abort(); // throw new TimeoutException(); //} })); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Cache <TReturnType>(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect, ICacheWrapper cacheResolver, string key) { return(aspect.Combine(work => Cache <TReturnType>(aspect, cacheResolver, key, work, cached => cached))); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF RunAsync(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspect) { return(aspect.Combine(work => work.BeginInvoke(work.EndInvoke, null))); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Retry(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspects, Action <IEnumerable <Exception> > failHandler, ILogger logger) { return(aspects.Combine(work => Retry(1000, 1, error => DoNothing(error), x => DoNothing(), work, logger))); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Retry(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspects, int retryDuration, ILogger logger) { return(aspects.Combine(work => Retry(retryDuration, 1, error => DoNothing(error), x => DoNothing(), work, logger))); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Retry(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspects, int retryDuration, int retryCount, Action <Exception> errorHandler, ILogger logger) { return(aspects.Combine(work => Retry(retryDuration, retryCount, errorHandler, x => DoNothing(), work, logger))); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Retry(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspects, int retryDuration, int retryCount, Action <Exception> errorHandler, Action <IEnumerable <Exception> > retryFailed, ILogger logger) { return(aspects.Combine(work => Retry(retryDuration, retryCount, errorHandler, retryFailed, work, logger))); }
public static LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF Retry(this LOGI.Framework.Toolkit.OpenSource.AspectF.AspectF aspects, int retryCount, int retryDuration = 500) { return(aspects.Combine(work => Retry(retryDuration, retryCount, x => DoNothing(), x => DoNothing(), work, Let.Logger()))); }