2013-11-27 5 views
0

У меня есть веб-приложение asp.net, настроенное на веб-сервере IIS 7.0. У меня возникла проблема интеграции пользовательского HTTPModule в мой web.config. Однако код работает для сборки Visual Studio на веб-сервере. Может кто-нибудь, пожалуйста, взгляните и скажите мне, что я могу делать неправильно?Как интегрировать пользовательский HttpModule для IIS7

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); 
     } 
    } 
} 

Реализация модуля должно быть легко:

<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> 

Я же получаю следующее сообщение об ошибке:

[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 

Я был бы счастлив, если кто-то может помочь мне решить эту проблему. Надеюсь, этот пост поможет коллегам-программистам в нужный день.

ответ

0

Вопрос был с пулом приложений. Я изменил свойство «ManagedPipelineMode» с «Интегрировано» на «Классический» и это устранило проблему.

 Смежные вопросы

  • Нет связанных вопросов^_^