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";
}
The result is the following stack trace. I just changed the user code call stack names 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 don’t understand here, as this is a very simple simple vanilla code.
EDIT
It gets even weirder. I added some basic code just wondering which server variables I can get at this point. This happens with the same exception when I try to get all the keys:
System.NullReferenceException: . System.Web.Hosting.ISAPIWorkerRequestInProc.GetAdditionalServerVar(Int32 ) System.Web.Hosting.ISAPIWorkerRequestInProc.GetServerVariable(String ) System.Web.HttpRequest.AddServerVariableToCollection(String ) System.Web.HttpRequest.FillInServerVariablesCollection() System.Web.HttpServerVarsCollection.Populate() System.Web.HttpServerVarsCollection.get_AllKeys() MyService.MyHelper()
, , , , , , , , ... .
, Microsoft.