public void RunBefores(nspec instance) { // parent chain RecurseAncestors(c => c.RunBefores(instance)); // class (method-level) if (BeforeInstance != null && BeforeInstanceAsync != null) { throw new ArgumentException("A single class cannot have both a sync and an async class-level 'before_each' set, please pick one of the two"); } BeforeInstance.SafeInvoke(instance); BeforeInstanceAsync.SafeInvoke(instance); // context-level if (Before != null && BeforeAsync != null) { throw new ArgumentException("A single context cannot have both a 'before' and an 'beforeAsync' set, please pick one of the two"); } Before.SafeInvoke(); BeforeAsync.SafeInvoke(); }
public void RunBefores(nspec instance) { // parent chain RecurseAncestors(c => c.RunBefores(instance)); // class (method-level) if (BeforeInstance != null && BeforeInstanceAsync != null) { throw new AsyncMismatchException( "A spec class with all its ancestors cannot set both sync and async " + "class-level 'before_each' hooks, they should either be all sync or all async"); } BeforeInstance.SafeInvoke(instance); BeforeInstanceAsync.SafeInvoke(instance); // context-level if (Before != null && BeforeAsync != null) { throw new AsyncMismatchException( "A single context cannot set both a 'before' and an 'beforeAsync', please pick one of the two"); } if (Before != null && Before.IsAsync()) { throw new AsyncMismatchException( "'before' cannot be set to an async delegate, please use 'beforeAsync' instead"); } Before.SafeInvoke(); BeforeAsync.SafeInvoke(); }
/// <summary> /// To hndle a request/response context. authorize, before/after filters /// </summary> /// <param name="rsc">the resource path</param> /// <param name="ac">ActionContext</param> /// <exception cref="AuthorizeException">Thrown when authorization is required and false is returned by checking</exception> /// <seealso cref="AuthorizeAttribute.Check"/> internal async Task HandleAsync(string rsc, ActionContext ac) { ac.Work = this; // authorize and filters if (!DoAuthorize(ac)) { throw AuthorizeEx; } Before?.Do(ac); if (BeforeAsync != null) { await BeforeAsync.DoAsync(ac); } int dot = rsc.LastIndexOf('.'); if (dot != -1) // file { // try in cache if (!Service.TryGiveFromCache(ac)) { DoFile(rsc, rsc.Substring(dot), ac); Service.Cache(ac); // try cache it } } else // action { string name = rsc; int subscpt = 0; int dash = rsc.LastIndexOf('-'); if (dash != -1) { name = rsc.Substring(0, dash); ac.Subscript = subscpt = rsc.Substring(dash + 1).ToInt(); } ActionInfo ai = string.IsNullOrEmpty(name) ? @default : GetAction(name); if (ai == null) { ac.Give(404); // not found return; } ac.Doer = ai; // action's authorization check if (!ai.DoAuthorize(ac)) { throw AuthorizeEx; } // action's before filtering ai.Before?.Do(ac); if (ai.BeforeAsync != null) { await ai.BeforeAsync.DoAsync(ac); } // try in cache if (!Service.TryGiveFromCache(ac)) { // method invocation if (ai.IsAsync) { await ai.DoAsync(ac, subscpt); // invoke action method } else { ai.Do(ac, subscpt); } Service.Cache(ac); // try cache it } // action's after filtering ai.After?.Do(ac); if (ai.AfterAsync != null) { await ai.AfterAsync.DoAsync(ac); } ac.Doer = null; } After?.Do(ac); if (AfterAsync != null) { await AfterAsync.DoAsync(ac); } ac.Work = null; }