开发者

Client IP address in ASP.NET (.asmx) webservices

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜