Skip to content

IMiddleware

Martin Halliday edited this page Jul 14, 2016 · 2 revisions

All middleware must implement IMiddleware<T> to work with the pipeline builder. The T defines what kind of middleware this is. For example implementing IMiddleware<ISession> indicates that this is session middleware.

There are places in this solution where it is necessary to store a collection of middleware. It is not possible to define a List<T> where T is a generic type. i.e. we can not define IList<IMidleware<T>> myMiddleware;. To address this issue this solution defines interface IMiddleware that is inherited by IMiddleware<T> so that we can create IList<IMiddleware> and store IMiddleware<IIdentification> and IMiddleware<IAuthorization> in the same list.

For generic middleware that is no specific type you should implement IMiddleware<object>.

The example below is a minimal implementation of IMiddleware<T> that can be used as a template for your own middleware.

    public class MyMiddleware : IMiddleware<object>
    {
        public string Name { get; set; }

        private readonly IList<IDependency> _dependencies = new List<IDependency>();
        public IList<IDependency> Dependencies { get { return _dependencies; } }

        public Task Invoke(IOwinContext context, Func<Task> next)
        {
            return next();
        }
    }

Clone this wiki locally