Domanda

I have asp.net webapplication configured on IIS 7.0 webserver. I am having problem integrating a custom HTTPModule into my web.config. The code however works for the Visual studio build in webserver. Can someone please take a look and tell me what I might be doing wrong?

using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Web;

namespace Realpage.HTTPModule.UnhandledException
{
    using System.Configuration;
    using System.Reflection;

    using Elmah;

    public class ElmahLogger : IHttpModule
    {
        static int unhandledExceptionCount;

        static readonly object InitLock = new object();
        static bool initialized;

        private static string elmahDirectory;
        private static readonly FieldInfo elmahLogPathField =
            typeof(XmlFileErrorLog).GetField("_logPath", BindingFlags.NonPublic | BindingFlags.Instance);
        public void Init(HttpApplication httpApplication)
        {
            // Do this one time for each AppDomain.
            if (initialized)
            {
                return;
            }
            lock (InitLock)
            {
                if (initialized)
                {
                    return;
                }
                var webenginePath = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), "webengine.dll");
                if (!File.Exists(webenginePath))
                {
                    throw new Exception(String.Format(CultureInfo.InvariantCulture, 
                        "Failed to locate webengine.dll at '{0}'.  This module requires .NET Framework 2.0.", webenginePath));
                }

                AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
                httpApplication.BeginRequest += Application_BeginRequest;
                initialized = true;
                elmahDirectory = ConfigurationManager.AppSettings["ElmahLogPath"];
            }
        }

        static void Application_BeginRequest(Object sender, EventArgs e)
        {
            var xmlFileErrorLog = (XmlFileErrorLog)ErrorLog.GetDefault(HttpContext.Current);
            elmahLogPathField.SetValue(xmlFileErrorLog, elmahDirectory);
        }

        public void Dispose()
        {
        }

        static void OnUnhandledException(object o, UnhandledExceptionEventArgs e)
        {
            // Let this occur one time for each AppDomain.
            if (Interlocked.Exchange(ref unhandledExceptionCount, 1) != 0)
            {
                return;
            }

            var xmlFileErrorLog = (XmlFileErrorLog)ErrorLog.GetDefault(HttpContext.Current);
            elmahLogPathField.SetValue(xmlFileErrorLog, elmahDirectory);

            var exception = (Exception)e.ExceptionObject;
            var baseException = exception.GetBaseException();

            var error = new Error(baseException);
            xmlFileErrorLog.Log(error);
        }
    }
}

Implementing the module should have been easy :

<configuration>
<system.web>
<httpModules>
      <add name="Realpage.HTTPModule.UnhandledException" type="Realpage.HTTPModule.UnhandledException.ElmahLogger" />
            <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah"/>
            <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah"/>
        </httpModules>
</system.web>

<system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
            <remove name="ScriptModule"/>
      <add name="RealpageHTTPModuleUnhandledException" type="Realpage.HTTPModule.UnhandledException.ElmahLogger, Realpage.HTTPModule.UnhandledException"
           preCondition="managedHandler"/><!--This is the handler causing the issue-->
      <add name="ScriptModule" preCondition="managedHandler" 
           type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler"/>
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler"/>
            <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler"/>
        </modules>
    </system.webServer>

</configuration>

I however get the following error:

[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) +332
   System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +113
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +616

I would be happy if someone could help me solve this issue. Hopefully this post will help fellow programmers on their day of need.

È stato utile?

Soluzione

The issue was with the Application pool. I changed the "ManagedPipelineMode" property from "Integrated" to "Classic" and that fixed the issue.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top