/// <summary>Given a list of middleware, this goes through and sets all of their Application properties</summary> /// <remarks> /// Once you get this list back, you should Invoke the *first* middleware, if any. /// /// You should also set the Application of the last Middleware (which will be null) /// to the actual application that you want to run. /// /// NOTE: This works with the List that its given and modifies it. /// </remarks> public static MiddlewareList SetInnerApplications(MiddlewareList middleware) { for (var i = 0; i < middleware.Count - 1; i++) { middleware[i].Application = middleware[i + 1]; } return(middleware); }
/// <summary>Returns all of the Middleware found in the given assemblies (see <c>AllFromAssembly</c></summary> public static new MiddlewareList AllFromAssemblies(params Assembly[] assemblies) { var middlewares = new MiddlewareList(); foreach (var assembly in assemblies) { middlewares.AddRange(AllFromAssembly(assembly)); } return(middlewares); }
/// <summary>Returns a new List of Middleware sorted based on any sorting rules specified in the [Middleware] attribute</summary> /// <remarks> /// If you specify a Before or After on your [Middleware] and we can't find the middleware you specified, /// we DO NOT include your Middleware in the stack. This method will REMOVE it! You've been warned. /// </remarks> public static MiddlewareList Sort(MiddlewareList middlewares) { var copy = middlewares.ToArray(); // Process Middleware marked with First or Last foreach (var mw in copy) { if (mw.First) { middlewares.MoveToTop(mw); } else if (mw.Last) { middlewares.MoveToBottom(mw); } } // Process Middleware marked with Before or After foreach (var mw in copy) { if (mw.First || mw.Last) { continue; // you can't be First|Last and use Before|After } if (!string.IsNullOrEmpty(mw.Before)) { middlewares.MoveToBefore(mw, mw.Before); } else if (!string.IsNullOrEmpty(mw.After)) { middlewares.MoveToAfter(mw, mw.After); } } return(middlewares); }
/// <summary>Invoke this application given the provided MiddlewareList of middlewares</summary> /// <remarks> /// This runs through all of the provided middleware. We use the MiddlewareList to give /// us middleware that are ordered properly, etc. /// /// This really just invokes MiddlewareList.Invoke(Request, Application) /// </remarks> public virtual Response Invoke(Request request, MiddlewareList middlewares) { return(middlewares.Invoke(request, this)); }