簡體   English   中英

對Web服務的HTTP發布請求。 錯誤:索引超出了數組的范圍

[英]HTTP Post Request to web service . ERROR : Index was outside the bounds of the array

我有以下代碼,用於將多個HTTP Post請求從XML文件發布到Web服務。 這里的問題是,如果我將thread.Join()放在有注釋的地方,則能夠成功地放置所有請求。 但是如果我將第二個內部for循環用於thread.Join() ,我會得到

索引超出數組錯誤的范圍

thread[x] = new Thread(() => function(files[x],p));

在files [x]中。 即main for循環。 我無法找到我要去的地方。 請糾正 。 我正在使用.NET4.0代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Xml;
using System.Net;
namespace ConsoleApplication4
{
    class Program
{
    int flag = 1;
    string destination;
    static void Main(string[] args)
    {

        int n = 0;
        Program p = new Program();
        Console.WriteLine("Enter the number");
        string s = Console.ReadLine();
        int.TryParse(s, out n);
        Console.WriteLine("Enter Destination");
        p.destination = Console.ReadLine();
        string path = "D:\\temp";
        string[] files = null;
        files = Directory.GetFiles(path, "*.xml", SearchOption.TopDirectoryOnly);

        Thread[] thread = new Thread[files.Length];
        int x;
        int len = files.Length;
        for (int i = 0; i<len; i+=n)
        {
            x = i;

            for (int j = 0; j < n && x < len; j++)
            {
                thread[x] = new Thread(() => function(files[x],p));
                thread[x].Start();
                //thread[x].Join();
                x++;
            }
            int y = x - n;
            for (; y < x; y++)
            {
                int t = y;
                thread[t].Join();
            }

        }

        // thread[0] = new Thread(() => function(files[0]));
        //thread[0].Start();
        Console.ReadKey();

    }
    public static void function(string temp,Program p)
    {

        XmlDocument doc = new XmlDocument();
        doc.Load(temp);

        string final_d=p.destination + "response " + p.flag + ".xml";
        p.flag++;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://10.76.22.135/wpaADws/ADService.asmx");
        request.ContentType = "text/xml;charset=\"utf-8\"";
        request.Accept = "text/xml";
        request.Method = "POST";
        Stream stream = request.GetRequestStream();
        doc.Save(stream);
        stream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (StreamReader rd = new StreamReader(response.GetResponseStream()))
        {
            string soapResult = rd.ReadToEnd();
            doc.LoadXml(soapResult);
            File.WriteAllText(final_d, doc.DocumentElement.InnerText);

            //XmlTextWriter xml=new XmlTextWriter(
            Console.WriteLine(soapResult);
            //Console.ReadKey();
        }
    }
}

}

這是因為表達式() => function(files[x],p) 第一個內部循環完成之后求值,並且x在該循環中遞增。 因此,您總是得到x = len的超出范圍的值。

為了解決這個問題,您需要在匿名函數聲明之前聲明另一個局部變量,並為其分配x的值,如下所示:

var localx=x;
thread[x] = new Thread(() => function(files[localx],p));

這是更深入地說明這種情況發生的原因的鏈接: 有人可以用簡單的術語解釋C#中的“訪問修改后的閉包”嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM