如何使用Web服务,坚持基于事件的异步模式? [英] How to consume web service adheres to the Event-based Asynchronous Pattern?

查看:108
本文介绍了如何使用Web服务,坚持基于事件的异步模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面从

http://msdn.microsoft.com/en-us/library/ 8wy069k1.aspx

使用基于事件的异步模式以消耗实现的Web服务(由第三方)。

to consume a web service implemented (by 3rd party) using the Event-based Asynchronous Pattern.

不过,我的程序需要多个调用尽到 DoStuffAsync()因此会尽快尽可能多的 DoStuffCompleted 。我选择这需要一个额外的参数的重载 - 对象userState 来区分它们。

However, my program needs to do multiple calls to the DoStuffAsync() hence will get back as many DoStuffCompleted. I chose the overload which takes an extra parameter - Object userState to distinguish them.

我的第一个问题是:
它是有效的投 GUID 来如下,其中GUID是用来生成唯一的TaskID对象吗?

My first question is: Is it valid to cast a GUID to Object as below, where GUID is used to generate unique taskID?

Object userState = Guid.NewGuid();

其次,我需要产卵关闭每个 DoStuffAsync()调用一个新的线程,因为我多次打电话呢?

Secondly, do I need to spawn off a new thread for each DoStuffAsync() call, since I am calling it multiple times?

此外,将是不错的对这个问题的一些网上的例子或教程。 (我一直在使用Google的这整整一天,并没有得到多少回)

Also, would be nice to have some online examples or tutorials on this subject. (I've been googling for it the whole day and didn't get much back)

非常感谢

新问题:我能否埋葬委托回调在AsyncCompletedEventArgs.UserState
刚发现我需要一个回调给调用者做善后......哎呀!

New Question: Can I bury a delegate call back in AsyncCompletedEventArgs.UserState? Just found out I need a callback to the caller to do the aftermaths...oops!

推荐答案

传递一个GUID听起来不错,只要你保持基准周围所以当Aync方法完成你知道如何把它放在正确的背景下。

Passing a Guid sounds fine as long as you are keeping the reference around so when the Aync method completes you know how to put it in the right context.

异步方法确实线程你。除非你想一个线程,使你的所有的异步调用不要做另一个线程。但我建议得到它做这样的事情之前的工作。建立起来的步骤。

The Async method does the threading for you. Don't do another thread unless you want one thread that makes all your Async calls. But I'd suggest getting it working before doing anything like that. Build it up in steps.

这篇关于如何使用Web服务,坚持基于事件的异步模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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