-1

我有 5 个不同的 IP 需要绑定不同的 Web 请求。

请参阅下面的示例代码。Call1() 完美运行。但是当我尝试在 Call2() 中使用 List<Task> 时,它会将错误的 IP 绑定到请求(我在所请求站点的 IIS 日志中仔细检查了 IP)。

请帮忙!!!

    class Program
    {

        private static string[] IPs = new string[] { "xxx.xxx.xx.xxx", "xxx.xxx.xx.xxx", "xxx.xxx.xx.xxx", "xxx.xxx.xx.xxx", "xxx.xxx.xx.xxx" };
        private static string Url = "http://myfakedomain.com/{0}.aspx";

        static void Main(string[] args)
        {
            Call1();
            Call2();
        }

        static void Call1()
        {
            WebHandler web = new WebHandler();

                foreach (var ip in IPs)
                {
                    web.MakeWebRequest(String.Format(Url,ip), ip);
                }
        }

        static void Call2()
        {
            WebHandler web = new WebHandler();
            List<Task> tasks = new List<Task>();

                foreach (var ip in IPs)
                {
                   var ipCopy = ip;
                    tasks.Add(Task<bool>.Factory.StartNew(() => web.MakeWebRequest(String.Format(Url, ipCopy ), ipCopy )));
                }

                Task.WaitAll(tasks.ToArray());
        }

    }

    public class WebHandler
    {
        public bool MakeWebRequest(string Url, string IP)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) => new IPEndPoint(IPAddress.Parse(IP), 0);

            request.KeepAlive = false;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
                {
                    string html = reader.ReadToEnd();
                }
            }

            return true;
        }
    }

下面是 Call2() 的 IIS 日志

2012-10-23 01:53:20 xxx.xxx.xx.xxx GET /xxx.xxx.xx.244.aspx - 80 - xxx.xxx.xx.242 - 200 0 0 1185
2012-10-23 01:53:20 xxx.xxx.xx.xxx GET /xxx.xxx.xx.241.aspx - 80 - xxx.xxx.xx.242 - 200 0 0 1201
2012-10-23 01:53:21 xxx.xxx.xx.xxx GET /xxx.xxx.xx.243.aspx - 80 - xxx.xxx.xx.242 - 200 0 0 608
2012-10-23 01:53:21 xxx.xxx.xx.xxx GET /xxx.xxx.xx.242.aspx - 80 - xxx.xxx.xx.242 - 200 0 0 624
2012-10-23 01:53:21 xxx.xxx.xx.xxx GET /xxx.xxx.xx.245.aspx - 80 - xxx.xxx.xx.242 - 200 0 0 218
4

1 回答 1

0

在处理 lambda 表达式时,这与 foreach 循环中的变量闭包很常见。本质上,如果您将 Call2() 代码更改为下面的代码,它应该可以按预期工作。如果您对它发生的确切原因感兴趣,请查看Eric Lippert 的博客文章以获得出色的解释。

static void Call2()
{
    WebHandler web = new WebHandler();
    List<Task> tasks = new List<Task>();

    foreach (var ip in IPs)
    {
        var ipCopy = ip;
        tasks.Add(Task<bool>.Factory.StartNew(() => web.MakeWebRequest(String.Format(Url, ipCopy), ipCopy)));
    }

    Task.WaitAll(tasks.ToArray());
}
于 2012-10-24T03:50:15.583 回答