3

Update: Problem solved. Read on.

Any idea why it's apparently not possible anymore to add custom IHttpModules?

My question is related to: HttpModule.Init - safely add HttpApplication.BeginRequest handler in IIS7 integrated mode However this question is rather old, unanswered and has no SharePoint context. I CAN add my HttpModule to any standard ASP.NET WebForms page.

SharePoint is being hosted in IIS 8. AppPool runs in Integrated Mode. Framework level is 4.0+.

namespace My.Namespace
{
    using System;
    using System.Web;

    public class CustomHttpModule : IHttpModule
    {
        private static readonly object mutex = new object();
        private static bool _isInitialized;

        public void Init(HttpApplication context)
        {
            if (!_isInitialized)
            {
                lock (mutex)
                {
                    if (_isInitialized) return;

                    context.BeginRequest += BeginRequest;
                    _isInitialized = true;
                }
            }
        }

        private void BeginRequest(object sender, EventArgs e)
        {   
        }

        public void Dispose()
        {
        }
    }
}

Result:

[NullReferenceException: Object reference not set to an instance of an object.] System.Web.PipelineModuleStepContainer.GetEventCount(RequestNotification notification, Boolean isPostEvent) +30
System.Web.PipelineStepManager.ResumeSteps(Exception error) +1098
System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +135
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +604

The web.config looks as follows:

<system.webServer>
<!-- further elements omitted -->
<modules runAllManagedModulesForAllRequests="true">
  <remove name="AnonymousIdentification" />
  <remove name="FileAuthorization" />
  <remove name="Profile" />
  <remove name="WebDAVModule" />
  <remove name="Session" />
  <add name="SPNativeRequestModule" preCondition="integratedMode" />
  <add name="SPRequestModule" preCondition="integratedMode" type="Microsoft.SharePoint.ApplicationRuntime.SPRequestModule, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  <add name="SharePoint14Module" preCondition="integratedMode" />
  <add name="StateServiceModule" type="Microsoft.Office.Server.Administration.StateModule, Microsoft.Office.Server, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="PublishingHttpModule" type="Microsoft.SharePoint.Publishing.PublishingHttpModule, Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="DesignHttpModule" preCondition="integratedMode" type="Microsoft.SharePoint.Publishing.Design.DesignHttpModule, Microsoft.SharePoint.Publishing, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="FederatedAuthentication" type="Microsoft.SharePoint.IdentityModel.SPFederationAuthenticationModule, Microsoft.SharePoint.IdentityModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="SessionAuthentication" type="Microsoft.SharePoint.IdentityModel.SPSessionAuthenticationModule, Microsoft.SharePoint.IdentityModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="SPWindowsClaimsAuthentication" type="Microsoft.SharePoint.IdentityModel.SPWindowsClaimsAuthenticationHttpModule, Microsoft.SharePoint.IdentityModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="SPApplicationAuthentication" type="Microsoft.SharePoint.IdentityModel.SPApplicationAuthenticationModule, Microsoft.SharePoint.IdentityModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
  <add name="Session" type="System.Web.SessionState.SessionStateModule" />
  <add name="CustomModule" type="My.Namespace.CustomHttpModule, My.Namespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=066b2229567b6747" />
</modules>
<!-- further elements omitted -->
</system.webServer>

As soon as I don't attach to the BeginRequest event anymore the page works again. But obviously my http module is rendered useless.

Edit 2013.09.19: Init() is being called twice upon application start. If I attach my event only on the second call the application works but the event doesn't fire.

Edit 2013.09.20: Problem presumably solved. The fact that my Init() method is triggered twice (and no Dispose() is called in between) led me to the assumption there might actually be two co-existing instances of my IHttpModule. And while my previous observation showed that the second instance can have the event attached (not firing, but no negative impact on the application as well) - it apparently is not true vice versa (which is the case with my static _isInitialized "lock").

**So both instances of the IHttpModule need to have the same "configuration" (eventhandlers attached). It is totally normal to have more than one HttpApplication instance running. That's something ASP.NET (or IIS) does internally for optimization purposes. That's the key thing to remember :)

Community
  • 1
  • 1
lapsus
  • 2,915
  • 2
  • 32
  • 61

1 Answers1

0

Problem solved. Edited question. See Edit 2013.09.20.

lapsus
  • 2,915
  • 2
  • 32
  • 61