Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

yay - a much shorter post ^_^

I posted a question where my code was giving me a "cannot access disposed object". I get why that happened now:

I send of ~1000 request to the web service asynchronously.
I have a Complete delegate that increments a counter for each response.
Once the counter reaches the number of calls, it sets a WaitHandler.

The true issue was that I am getting thousands of responses for each request. where x max is ~1000, y goes off to 80k+ before the exception. The WaitHandle is already released but responses keep coming, hence the error occurs shortly after the method is out of scope.

Why am I getting so many responses?

I have removed the custom Async component of my web service and have just used the configuration of the service reference to generate Async methods.

here is my code:


C#
private static void GenerateTests(int numberOfTests, Point itemsPerTest)
{
    int y = 0;

    ManualResetEvent resetEvent = new ManualResetEvent(false);
    ServiceAccessClient client = new ServiceAccessClient();

    for (int x = 0; x < numberOfTests; x++)
    {
        var details = //...NDA

        WriteLine("{0:D4}: Sending to secure access service", x);

        #endregion

        client.SendDetailsCompleted += delegate(object sender, SendDetailsCompletedEventArgs args)
        {
            y++;
            if (args.Error != null)
            {
                WriteLine("ERROR: {0}", args.Error);
            }
            else
            {
                WriteLine("{2:D4}: response {0}: {1}", args.Result.ResponseCode, args.Result.ResponseDetail, y);
            }
            if (y == numberOfTests)
            {
                resetEvent.Set();
            }
        };

        client.SendDetailsAsync(details, "x", Guid.Parse(NDA.TestGuid));

    }
    resetEvent.WaitOne();
}



Can anyone help? what am I doing wrong?


Many thanks
Andy
Posted

1 solution

Hmm. Don't know whether 1000s of async requests is a great idea, but then I don't really do web stuff.

However, this line:
client.SendDetailsCompleted += delegate(object sender, SendDetailsCompletedEventArgs args)


is in the loop, which means that any time the event is fired, the callback will get called the same number of times as the loop iterated.

I'd venture you want that outside of the loop!
 
Share this answer
 
Comments
Andy Lanng 23-Jun-15 12:25pm    
Ha ha ha - I'm a pillock ^_^

This is a test project to simulate "Worst Case" traffic. That's why so many calls :)
Thanks for taking a look. It's much better now :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900