Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Initialize Glimpse HttpModule after OwinStartup #1016

Open
oruchreis opened this issue Jul 19, 2018 · 0 comments
Open

Initialize Glimpse HttpModule after OwinStartup #1016

oruchreis opened this issue Jul 19, 2018 · 0 comments

Comments

@oruchreis
Copy link

Hi,
We are using owin with asp.net mvc/webapi in most of our projects. The application makes Mvc settings and other registrations in owin startup, and Glimpse's HttpModule starts before owinstartup. So our registrations like our custom viewengine, our custom controllerfactory overrides Glimpse's registrations. So I need to initialize glimpse httpmodule after owinstartup.
If I call Init method after owinstartup, it throws exception because of HttpApplication events can't be register after init method.
For workaround, I did this with reflection and custom events:

private HttpModule glimpseHttpModule;
private void InitializeGlimpse()
{
    glimpseHttpModule = new HttpModule();
    var glimpseHttpModuleType = typeof(HttpModule);
    var glimpseRuntimeMethod = glimpseHttpModuleType.GetMethod("GetRuntime", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
    var glimpseRuntime = (IGlimpseRuntime)glimpseRuntimeMethod.Invoke(glimpseHttpModule, new[] { new HttpApplicationWrapper(WebApplication.Instance).Application });
    if (glimpseRuntime.IsInitialized || glimpseRuntime.Initialize())
    {
        var beginRequestMethod = glimpseHttpModuleType.GetMethod("BeginRequest", BindingFlags.Instance | BindingFlags.NonPublic);
        var beginSessionAccessMethod = glimpseHttpModuleType.GetMethod("BeginSessionAccess", BindingFlags.Instance | BindingFlags.NonPublic);
        var endSessionAccessMethod = glimpseHttpModuleType.GetMethod("EndSessionAccess", BindingFlags.Instance | BindingFlags.NonPublic);
        var endRequestMethod = glimpseHttpModuleType.GetMethod("EndRequest", BindingFlags.Instance | BindingFlags.NonPublic);

        OnBeginRequest += (context, e) => beginRequestMethod.Invoke(glimpseHttpModule, new[] { new HttpContextWrapper(((HttpApplication)context).Context) });
        OnPostAcquireRequestState += (context, e) => beginSessionAccessMethod.Invoke(glimpseHttpModule, new[] { new HttpContextWrapper(((HttpApplication)context).Context) });
        OnPostRequestHandlerExecute += (context, e) => endSessionAccessMethod.Invoke(glimpseHttpModule, new[] { new HttpContextWrapper(((HttpApplication)context).Context) });
        OnPostReleaseRequestState += (context, e) => endRequestMethod.Invoke(glimpseHttpModule, new[] { new HttpContextWrapper(((HttpApplication)context).Context) });
    }
}

I've removed module settings in web.config, and I'm calling this method end of the Configure method of owin startup. It's working, but I rather not to use reflection and custom event handlers. I'm triggering theese event handlers in HttpApplication like this:

public class WebApplcaiton: HttpApplication
{
    protected void Application_PostAcquireRequestState(object sender, EventArgs e)
    {
        Startup.Instance?.TriggerPostAcquireRequestState(sender, e);
    }

    protected void Application_PostRequestHandlerExecute(object sender, EventArgs e)
    {
        Startup.Instance?.TriggerPostRequestHandlerExecute(sender, e);
    }

    protected void Application_PostReleaseRequestState(object sender, EventArgs e)
    {
        Startup.Instance?.TriggerPostReleaseRequestState(sender, e);
    }
}
public class Startup
{
    public event EventHandler OnBeginRequest;
    public event EventHandler OnPostAcquireRequestState;
    public event EventHandler OnPostRequestHandlerExecute;
    public event EventHandler OnPostReleaseRequestState;
    internal void TriggerBeginRequest(object sender, EventArgs e)
    {
        OnBeginRequest?.Invoke(sender, e);
    }
    internal void TriggerPostAcquireRequestState(object sender, EventArgs e)
    {
        OnPostAcquireRequestState?.Invoke(sender, e);
    }
    internal void TriggerPostRequestHandlerExecute(object sender, EventArgs e)
    {
        OnPostRequestHandlerExecute?.Invoke(sender, e);
    }
    internal void TriggerPostReleaseRequestState(object sender, EventArgs e)
    {
        OnPostReleaseRequestState?.Invoke(sender, e);
    }
}

I know that Glimpse doesn't support owin middlewares yet. But I don't need middleware support right now neither. What I need is starting Glimpse module when I want.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant