tags:

views:

77

answers:

2

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.

A: 

Have you tried getting the ip directly from HttpContext.Current.Request.UserHostAddress?

klausbyskov
Yes I did which led to the code above;-)
JoshBerke
A: 

It seems that it is not always populated depending on the proxies... http://haacked.com/archive/2006/10/11/A_Gotcha_Identifying_the_Users_IP_Address.aspx

Montane
Yes but it shouldn't cause a null reference exception. It would just return the proxies IP address which is fine...I want something not an exception
JoshBerke