【问题标题】:Is this public static get IP function thread safe ? usage of HttpContext这个公共静态获取 IP 函数线程安全吗? HttpContext的使用
【发布时间】:2012-12-09 07:21:14
【问题描述】:

IIS 7.5 ASP.NET 4.0 C# 4.0

这里的函数是线程安全的吗?假设同时来自不同访客的 1000 个不同的电话

public static string ReturnIpAddress()
{
    string srUserIp = "";
    try
    {
        if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
        {
            srUserIp = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
        }
        if (string.IsNullOrEmpty(srUserIp))
        {
            srUserIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
        }
    }
    catch
    {

    }
    return srUserIp;
}

【问题讨论】:

    标签: c# function thread-safety public httpcontext


    【解决方案1】:

    这是超级安全的。您不会更改任何数据,它是静态的,它会查询一个不可变的请求对象。

    【讨论】:

      猜你喜欢
      • 2012-11-29
      • 1970-01-01
      • 2016-07-16
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      相关资源
      最近更新 更多