Null Reference getting REMOTE_ADDR - asp.net

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.

Going through my old questions. I believe this was caused by a bug in either .net or IIS. Essentially, the web service was marked as Oneway. By doing so the user was disconnected before the handler was executed so unless you managed to access the Request variables prior to the handler executing you would encounter a null reference exception.
The workaround was to add an HTTP module which accessed the property earlier in the pipeline before the client was sent the response and the handler was executed.

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

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

Related

Server was unable to process request. in web service method call

Through my project i am making an web service call by adding web reference.
In my project i am creating object like below
PortalService portalService=new PortalService();
if (portalService != null)
{
DataSet ds = portalService.getStateList(3);
}
Its going inside if loop after creating object. But when calling web service method its giving me exception like below
[SoapException: Server was unable to process request. ---> Object reference not set to an instance of an object.]
Why this exception occuring? How do i make web service call?
The code is ok. It looks like there is an exception happening inside the service itself. I would look at the service code, and try to track down the issue. Perhaps the input is not valid?

Step into reference.cs file

I have a SOAP based web service with an associated reference.cs file (not WCF). The service is returning an invalid response. The reference.cs file has a testRequest method that I would like to step into. I only have limited control of the service so I want to override the results[0] line with my own test response to try & determine the exact reason that the response is invalid. I can't however, step into testRequest. Why not? what exactly is results[0]? I tried to do a cast on my own with the Soap XML in a string, but I get a message saying I can't cast from a string to that object. What is results[0] at this point then if it is not a string?
[System.Web.Services.Protocols.SoapHeaderAttribute("SecurityValue")]
[System.Web.Services.Protocols.SoapHeaderAttribute("MessageHeaderValue")]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
[return: System.Xml.Serialization.XmlElementAttribute("TestResponse", Namespace="http:/test/Services")]
public TestResponse testRequest([System.Xml.Serialization.XmlElementAttribute("TestRequest", Namespace="http://Test.com/TestServices")] TestRequestTestRequest1) {
object[] results = this.Invoke("TestRequest", new object[] {
TestRequest1});
return ((TestResponse)(results[0]));
}
UPDATE #1
You can step into response.cs by going to Tools => Options => Debugging => General &
unchecking "Enable Just My Code". The issue is that the request gets to my PC, but the "results" variable is null after this.Invoke is called. My question now is how can I intercept the call that this.Invoke is making & insert my own response. I DON"T have developer access to the actual service. Maybe I need to write a test service...
I created a test service that just returned a hard-coded SOAP response. That helped me diagnose the problem with the response. The problem ended up being a missing namespace

Need parameter values of method where exception is thrown for logging without specific code in each method

How do I get current parameter values when an exception occurs inside a random method without having specific code in each method to write out the values?
I need this for ASP.NET MVC and WCF IErrorHandler?
For example given the following code:
public void SomeRandomMethod(Request request, string someRandomString)
{
throw new Exception();
}
is there a way for an IErrorHandler in WCF or MVC global.asax HttpApplication's Application_Error & Elmah to get the value of the Request object and someRandomString without specifically catching the exception and writing custom logic for each of N number of methods then throwing again?
Maybe this should be broken into 2 questions one for WCF and one for ASP>NET MVC?
The request hierarchies seem very similar to me and so I was hoping for a single unified answer.
I would recommend using Elmah. It's a wonderful logging tool for ASP.NET applications. It will include the parameters passed along with the HTTP request.

Is it possible to write a base class to handle nullReferenceException from all pages?

I am using .net framework 4.0, plain asp.net and working with webform. Currently I having a base class to handle all parameter passing and redirect. I wonder is it possible to write a base class to handle nullRefeerenceException from all pages in once, lets say redirect end user to somewhere or display particular error message.
Scenario: For example, some pages must come along with parameter, if no parameter captured, I would like to redirect them to somewhere.
You can try to control the ProcessRequest. You need to test it to see if can do the work you ask for, but this is a good point to capture all errors of your page.
public override void ProcessRequest(HttpContext context)
{
try
{
base.ProcessRequest(context);
}
catch (Exception x)
{
// handle here your error from the page...
}
}
Some more notes
I was use this code on one critical page, but I do not use it for all my page. Even tho can capture the errors, some times you can not do nothing else here other than throw again the final error, so end up that is better to log your unknown and unhandled errors from globa.asax Application_Error, and on page make sure that you use try/catch to handle them where they happens.
After some think maybe is not good practice to use it. Good practice is to use try/catch in the place that you may have throws and not a general one like that.
Last
You also get throw error here when the user close the connection before the end of the render, but if you log the errors you get the same on Application_Error - this is not a page error.
Exception of type 'System.Web.HttpUnhandledException' was thrown. --->
System.Web.HttpException: The remote host closed the connection.
The error code is 0x80072746.
In you Global.asax, handle Application_Error.
When a NullReferenceException is handled by the server a 500 response is created. Redirect all of your server 500 messages however you want. This guide will help.
Definitive Guide to Handling 500 Errors in IIS6, IIS7, ASP.NET MVC3 with Custom Page
You can hook up to every uncatched NullReference Exception, depending on what you want to do.
For instance you can use the global.asax, to be specific the Application_Error Event. You can get a reference to the exception, look for the type and perform a redirect there.
Another way to get ahold of exceptions would be to write your custom error provider, but that wouldn't give you the possibility to perform a redirect.

Error while calling webservice (Server was unable to process request. ---> PARAMETER_NAME)

I get this error while calling the web service hosted on the server.
The service has one web method with as string input and returns string (will be in XML format). The service is working fine but once in a while I get this error and can't predict. Can someone help me to get through this error.
Attached the screen shot of the error. Please let me know if you need additional info
I've never seen this error before, but I suggest you look at the event log on the server side. I suspect you are getting an unhandled exception on the client because you have one on the server.
Also, you should never return XML as a string. Use the XmlElement data type to return XML.

Resources