I am using ASP.NET (.asmx) web services with Silverlight. Since there is no way to find the client IP address in Silverlight, I had to log this on the service end. These are some methods I have tried:
Request.ServerVariables("REMOTE_HOST")
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
Request.UserHostAddress()
Request.UserHostName()
string strHostName = Dns.GetHostName();
string clientIPAddress = Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
All the above methods work fine on my local system, but when I publish my service on a production server, it starts giving errors,
Error: Object reference not set to an instance of an object. StackTrace:
at System.Web.Hosting.ISAPIWorkerRequestInProc.GetAdditionalServerVar(Int32 index)
at System.Web.Hosting.ISAPIWorkerRequestInProc.GetServerVariable(String name)
at System.Web.Hosting.ISAPIWorkerRequest.GetRemoteAddress()
at System.Web.HttpRequest.get_UserHostAddress()
If you know how to access your command line or terminal emulator, you can use the ping command to find and get your IP address for your domain: 1. At the prompt, type ping, press the spacebar, and then type the relevant domain name or the server hostname.
Client IP addresses describe only the computer being used, not the user. If multiple users share the same computer, they will be indistinguishable. Many Internet service providers dynamically assign IP addresses to users when they log in.
You should try to find out exactly where the NullReferenceException
is coming from. Change your code to understand that certain things can return null. For instance, in
HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
HttpContext.Current
could retrun null, or .Request
could return null, or .ServerVariables["REMOTE_ADDR"]
could return null. Also, in
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
the GetHostAddresses(strHostName)
could return null, or the .GetValue(0)
could return null.
If a method or property could return null, then you should check for null before dereferencing it. For instance,
IPAddress[] hostAddresses = System.Net.Dns.GetHostAddresses(strHostName);
string clientIPAddress;
if (hostAddresses != null)
{
object value = hostAddresses.GetValue(0);
if (value != null)
{
clientIPAddress = value.ToString();
}
}
P.S. I don't know why you'd use GetValue(0)
. Use hostAddresses[0]
instead.
If you take a look using Reflector at the System.Web.Hosting.ISAPIWorkerRequestInProc.GetAdditionalServerVar code, that's what we see:
private string GetAdditionalServerVar(int index)
{
if (this._additionalServerVars == null)
{
this.GetAdditionalServerVariables();
}
return this._additionalServerVars[index - 12];
}
I see two reasons why this could raise a NullReferenceException:
1) there is a multithreading issue on the _additionalServerVars member. I don't think this could happen because A) I don't see why there would be big load on your server during test, and B) the ISAPIWorkerRequestInProc instance is probably tied to a thread.
2) your server is not up to date and the code in production is not the same as the one I'm seeing on my machine.
So what I would do is check the server making sure it's up to date with the .NET Framework dlls.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With