Null Reference getting REMOTE_ADDR

Posted by Josh on Stack Overflow See other posts from Stack Overflow or by Josh
Published on 2010-04-16T17:38:11Z Indexed on 2010/04/18 23:53 UTC
Read the original article Hit count: 704

Filed under:
|

I have an ASMX web service running under IIS7 in classic mode. This service has the following code:

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

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

This results in the following stack trace. I only modified the names of usercode call stack here to protect the innocent:

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

I'm at a loss here as this is very basic plain vanilla code.

EDIT

This gets even stranger. I have added some basic code just wondering what server variables I can get at this point. This fails with the same exception when I try and get all the keys:

System.NullReferenceException: Object reference not set to an instance of an object. 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_AllKeys() at MyService.MyHelper()

When I was looking at the framework code looks like this could happen when the array which caches the server variables isn't populated and it looks like this occurs when there a null pointer to some Context...which seems like a fairly core piece of the framework code.

I suppose it's time to burn up one of our support tickets with Microsoft.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about iis7