为什么我收到的响应数量超过了对Async Web服务的请求数量? [英] Why am I getting thousand more responses than requests made to Async web service?

查看:71
本文介绍了为什么我收到的响应数量超过了对Async Web服务的请求数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





yay - 更短的帖子^ _ ^



我发布了一个问题我的代码给了我一个无法访问被处置对象的地方。我明白为什么会发生这种情况:



我异步向Web服务发送~1000请求。

我有一个完整的委托,增量每个回复的计数器。

一旦计数器达到调用次数,它就会设置一个WaitHandler。



真正的问题是我我收到了每个请求的数千个回复。其中x max是~1000,y在异常之前变为80k +。 WaitHandle已经发布但响应仍在继续,因此在方法超出范围后不久就会发生错误。



为什么我收到这么多回复?



我删除了我的Web服务的自定义Async组件,并且刚刚使用了服务引用的配置来生成异步方法。



这是我的代码:





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:


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();
}







有人可以帮忙吗?我做错了什么?





非常感谢

Andy




Can anyone help? what am I doing wrong?


Many thanks
Andy

推荐答案

嗯。不知道1000s的异步请求是不是一个好主意,但后来我并没有真正做网络的东西。



但是,这一行:

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!


这篇关于为什么我收到的响应数量超过了对Async Web服务的请求数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆