2010-04-16 13 views
3

我有經典模式下運行在IIS7下的ASMX web服務。此服務具有以下代碼:NULL引用獲取REMOTE_ADDR

try 
    { 
     env.ExternalIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 
    } 
    catch (Exception ex) 
    { 

     LogWriter.WriteError(ex); 
     env.ExternalIP="000.000.000.000"; 
    } 

這會導致以下堆棧跟蹤。我只修改了用戶代碼調用堆棧的名字在這裏,以保護無辜:

Message: An Exception of type: NullReferenceException occured in method: GetAdditionalServerVar 
ExceptionMsg: Object reference not set to an instance of an object. 
===Stack Trace=== 
    at System.Web.Hosting.ISAPIWorkerRequestInProc.GetAdditionalServerVar(Int32 index) 
    at System.Web.Hosting.ISAPIWorkerRequestInProc.GetServerVariable(String name) 
    at System.Web.HttpRequest.AddServerVariableToCollection(String name) 
    at System.Web.HttpRequest.FillInServerVariablesCollection() 
    at System.Web.HttpServerVarsCollection.Populate() 
    at System.Web.HttpServerVarsCollection.Get(String name) 
    at System.Collections.Specialized.NameValueCollection.get_Item(String name) 
    at MyService.MyMethod() 

我在這裏,因爲這損失是非常基本的普通的香草代碼。

編輯

這會變得陌生了。我已經添加了一些基本代碼,只是想知道我現在可以獲得哪些服務器變量。這種失敗,同樣的異常,當我試圖讓所有的按鍵:

System.NullReferenceException:對象 引用未設置爲一個 對象的實例。在在 系統 System.Web.Hosting.ISAPIWorkerRequestInProc.GetAdditionalServerVar(的Int32 指數)在 System.Web.Hosting.ISAPIWorkerRequestInProc.GetServerVariable(字符串 名)在 System.Web.HttpRequest.AddServerVariableToCollection(字符串 名)。 Web.HttpRequest.FillInServerVariablesCollection() 在 System.Web.HttpServerVarsCollection.Populate() 在 System.Web.HttpServerVarsCollection.get_AllKeys() 在MyService.MyHelper()

當我看着g在框架代碼看起來像這樣可能發生時,緩存服務器變量的數組沒有填充,它看起來像這樣的情況發生時存在空指針某些上下文...這似乎是一個相當核心的框架代碼片段。

我想現在是時候把我們的一張支持票與我們一起燒掉了。

回答

1

通過我的老問題去。我相信這是由.net或IIS中的錯誤引起的。本質上,Web服務被標記爲Oneway。通過這樣做,用戶在執行處理程序之前斷開連接,所以除非在處理程序執行之前設法訪問請求變量,否則將遇到空引用異常。

解決方法是在客戶端發送響應並執行處理程序之前添加一個HTTP模塊,該模塊在管道的早期訪問屬性。