Skip to content

IUpstreamCommunicator

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

Middleware should implement this interface if it provides an upstream communication mechanism.

This is an example of middleware that implements IUpstreamCommunicator<T>:

    public class InProcessSession : IMiddleware<ISession>, IUpstreamCommunicator<IUpstreamSession>
    {
        public Task RouteRequest(IOwinContext context, Func<Task> next)
        {
            context.SetFeature<IUpstreamSession>(new UpstreamSession());
            return next();
        }

        public Task Invoke(IOwinContext context, Func<Task> next)
        {
            var upstreamSession = context.GetFeature<IUpstreamSession>() as UpstreamSession;
            var sessionRequired = upstreamSession != null && upstreamSession.SessionRequired;

            return next();
        }
    }

Below is an example of how downstream middleware can use the IUpstreamSession placed in the OWIN context by the example above:

    public class TemplatePageRendering : IMiddleware<IPresentation>, IRoutingProcessor
    {
        public Task RouteRequest(IOwinContext context, Func<Task> next)
        {
            var upstreamSession = context.GetFeature<IUpstreamSession>();
            if (upstreamSession != null)
                upstreamSession.EstablishSession();

            return next();
        }
    }

Clone this wiki locally