Question

I have the below code in my OWIN Startup class:

myiapbuilder.Map("/something/something", doit =>
{
    doit.Use<pipepart1>();
    doit.Use<pipepart2>();
    doit.Use<piprpart3>();
});

If a condition occurs that I don't like in pipepart1, I would like to write a custom text/plain response to the caller within that Middleware, and do not fire pipepart2 or pipepart3. The BranchingPipelines sample on CodePlex shows lots of stuff, but not that.

Is it possible to short-circut a flow or otherwise stop OWIN processing of Middleware based on an earlier Middleware evaluation?

Was it helpful?

Solution

if you plan to respond directly to the client from pipepart1, then you could avoid calling other middlewares in the pipeline. Following is an example. Is this something you had in mind?

Here based on some condition (in my case if querystring has a particular key), I decide to either respond directly to the client or call onto next middleware.

appBuilder.Map("/something/something", doit =>
{
    doit.Use<Pipepart1>();
    doit.Use<Pipepart2>();
});

public class Pipepart1 : OwinMiddleware
{
    public Pipepart1(OwinMiddleware next) : base(next) { }

    public override Task Invoke(IOwinContext context)
    {
        if (context.Request.Uri.Query.Contains("shortcircuit"))
        {
            return context.Response.WriteAsync("Hello from Pipepart1");
        }

        return Next.Invoke(context);
    }
}

public class Pipepart2 : OwinMiddleware
{
    public Pipepart2(OwinMiddleware next) : base(next) { }

    public override Task Invoke(IOwinContext context)
    {
        return context.Response.WriteAsync("Hello from Pipepart2");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top