ASP.NET(.asmx)webservices中的客户端IP地址

我在Silverlight中使用ASP.NET(.asmx)Web服务.由于无法在Silverlight中找到客户端IP地址,因此我必须在服务端登录.
这些是我尝试过的一些方法:
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();

以上所有方法在我的本地系统上运行正常,但是当我在生产服务器上发布我的服务时,它开始给出错误,

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

解决方法

如果您在System.Web.Hosting.ISAPIWorkerRequestInProc.GetAdditionalServerVar代码中使用Reflector查看,那就是我们所看到的:
private string GetAdditionalServerVar(int index)
{
    if (this._additionalServerVars == null)
    {
        this.GetAdditionalServerVariables();
    }
    return this._additionalServerVars[index - 12];
}

我看到为什么会引发NullReferenceException的两个原因:

1)_additionalServerVars成员存在多线程问题.我不认为这可能发生,因为A)我不明白为什么在测试期间服务器上会有很大的负载,而且B)ISAPIWorkerRequestInProc实例可能与一个线程有关.

2)您的服务器不是最新的,生产中的代码与我在机器上看到的代码不同.

所以我要做的就是检查服务器,确保它与.NET Framework dll保持同步.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....