简体   繁体   中英

Asynchronous Web Service Calls

I have written this code which accesses the Delete method on Web Service x, I will be creating wrappers for each of the methods but I am hitting a blocker on something probably very simple.

My code consists of three methods:

  • One to make the delete call, this simply passes the params to an Async method.
  • The second is the event handler.
  • The third processes the results into a data table

Here is my current code:

public void Delete(string[] Identifiers, string ObjectType)
{
    service.deleteAsync(ObjectType, Identifiers);
    service.deleteCompleted += new deleteCompletedEventHandler(service_deleteCompleted);
}

void service_deleteCompleted(object sender, deleteCompletedEventArgs e)
{
    StoreResults(e.Result);
    if (resultsTable.Rows.Count == totalRecords)
    {
        CSVFile myFile = new CSVFile(",", true);
        myFile.Save(resultsTable, outputPath);
        Console.WriteLine("Tasks completed");
    }
}

public void StoreResults(DeleteResult[] ResultSet)
{
    if (resultsTable.Columns.Count < 1)
    {
        resultsTable.Columns.Add("ID");
        resultsTable.Columns.Add("Errors");
        resultsTable.Columns.Add("Success");
    }

    foreach (DeleteResult r in ResultSet)
    {
        StringBuilder errors = new StringBuilder();
        object[] newRow = new object[3];

        newRow[0] = r.id;
        if (r.errors != null)
        {
            newRow[1] = errors[0].ToString();
        }
        else
            newRow[1] = "No Errors to Report";

        newRow[2] = r.success.ToString();

        resultsTable.Rows.Add(newRow);
    }
}

A restriction of the webservice is that I can only pass 50 ID's per call, so I have some background code which manages the chunking of source data, what I need to acheive now is to store all of those results in a data table and pass it back.

You have to subscribe first:

public void Delete(string[] Identifiers, string ObjectType)
{
    service.deleteCompleted += service_deleteCompleted;
    service.deleteAsync(ObjectType, Identifiers);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM