开发者

C# Asynchronous Web Service Calls - Tainted Results

开发者 https://www.devze.com 2023-03-20 04:25 出处:网络
I am calling a webservice asynchronously, sending multiple requests and waiting for a response from the platform.

I am calling a webservice asynchronously, sending multiple requests and waiting for a response from the platform.

I have identified the issue I have but cannot see the logical error which is causing it, I am very new to async calls so please excuse my poor coding!

The issue is that the result set I am getting back contains the same 50 records for each batch that is created. I am confused!

Any help is greatly appreciated and vote ups will be handed out, this is urgent!

The code I have written is below:

    public DataTable Delete(DataTable Data, string ObjectType)
    {
        rowCount = Data.Rows.Count;
        List<string[]> Batches = new List<string[]>();
        int time = 0;

        Batches = BatchBuilder(Data);

        results.Columns.Add("ID");
        results.Columns.Add("Success");
        results.Columns.Add("Errors");

        foreach (string[] batch in Batches)
        {
            string guid = Guid.NewGuid().ToString();
            guids.Add(guid);
            service.deleteCompleted += new deleteCompletedEventHandler(service_deleteCompleted);
            service.deleteAsync("Account", batch, guid);
        }

        while (processedCount != Batches.Count)
        {
            if (time == int.MaxValue - 1)
            {
                time = 0;
            }
            time++;
        }

        return results;
    }

    void service_deleteCompleted(object sender, deleteCompletedEventArgs e)
    {
        foreach (DeleteResult deleteResult in e.Result)
        {
            object[] resultRow = new object[3];
            if (deleteResult.id != null)
                resultRow[0] = deleteResult.id;

            resultRow[1] = deleteResult.success;

            if (deleteResult.errors != null)
                resultRow[2] = deleteResult.errors[0].Message;

            results.Rows.Add(resultRow);
        }
        proces开发者_开发技巧sedCount++;
    }

    public List<string[]> BatchBuilder(DataTable table)
    {
        List<string[]> Batches = new List<string[]>();

        while (!batchingDone)
        {
            string[] IDs = new string[50];
            int arrayCrawler = 0;

            for (int v = 0; v < 50; v++)
            {
                IDs[arrayCrawler] = table.Rows[tableCrawler].ItemArray[0].ToString();

                arrayCrawler++;
                tableCrawler++;

                if (tableCrawler == table.Rows.Count)
                {
                    batchingDone = true;
                    break;
                }
                if (arrayCrawler == 50)
                {
                    arrayCrawler = 0;
                    break;
                }
            }

            Batches.Add(IDs);
        }

        return Batches;
    }


I suspect your problem comes from your service global variable that is reused for each call. Instanciate a new service instance each time.

0

精彩评论

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

关注公众号