govin
govin

Reputation: 6733

OWIN Self host - hook into begin request, end request events

In ASP.NET OWIN self host, how do you hook into the BeginRequest, EndRequest, Application Start and Application End events since there is no need for Global.asax.cs?

Upvotes: 7

Views: 3957

Answers (2)

Rajiv
Rajiv

Reputation: 108

Add a simple owin middleware at the start of the pipeline to handle begin and end request.

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

    public override async Task Invoke(IOwinContext context)
    {
        Debug.WriteLine("Begin Request");//Add begin request logic
        await Next.Invoke(context);
        Debug.WriteLine("End Request");//Add end request logic
    }
}

Upvotes: 6

MichaelS
MichaelS

Reputation: 3831

In WebAPI you can use filters for that. You can override OnActionExecuting and OnActionExecuted. If you don't want to annotate every single controller, you can add your filter als a global filter:

GlobalConfiguration.Configuration.Filters.Add(new MyFilterAttribute());

As replacement for ApplicationStart you can execute your code in your OwinStartup class. I don't know whether there is something similar to ApplicationEnd.

Upvotes: 1

Related Questions