如何在 Rx 中处理多个事件(具有依赖关系)
我有一些 WCF 调用,比如说 A、B、C、D、E。它们位于 SL 应用程序中(所以我想我必须小心线程等)。
我希望 B 在 A 完成后运行,而 C 可以与它们同时运行。我希望D在他们三个都完成后就跑。另外,基于一个条件(简单的 if...)我希望 E 也运行(与上述任何一个同时运行)。
我刚刚下载了 Rx,感觉它就是完成这项工作的工具。然而我还没有掌握所有的操作符和技巧等等。
这可以做到吗?如何?请解释为此所需的运算符。
编辑添加:为了简单起见,假设所有这些服务都采用字符串(或整数)作为参数,并返回实现复杂接口的东西,因此事件是简单的事件处理程序(非通用),然后我将它们转换为处理程序代码中的特殊接口。
有些输入在开始时就可用,有些来自先前服务的结果(例如,B 的输入字符串是根据 A 的结果计算的)
I have a few WCF calls, lets say A, B, C, D, E. They are in a SL application (so I guess I have to be careful with threading and such).
I want B to run after A has completed, while C can run concurrent with them. I want D to run after all three of them finished. And additionally based on a condition (simple if...) I want E to run too (concurrent with any of the above).
I just downloaded Rx and feel that it is the tool for this job. However I don't yet grasp all the operators and the tricks and such.
Can this be done? How? Please explain the operators required for this.
EDIT to add: For simplicity's sake let's say all of these services take a string (or an int) as a parameter, and returns something which implements a complex interface, so the events are simple EventHandlers (non-generic), and I cast them to their special interface in the handler code.
Some inputs are available at the start, some come from result of previous services (e.g. B's input string is computed based on A's result)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经考虑了编辑并制定了解决方案。
我不得不说 Microsoft 使得在 Silverlight 中编写简单的 WCF 调用变得非常困难。处理事件以从异步调用获取返回数据非常痛苦。
话虽如此,通过一些“复制粘贴”样板代码,Rx 在消除复杂性方面做得很好。
这是我想出的最终客户端代码:
一方面,这个代码并不像我希望的那么漂亮,但另一方面,我认为我无法让它变得更好。希望您能看到您的要求是如何得到满足的。
现在有坏消息 - 必须为每个服务代理创建扩展方法。根据
EventArgs
的定义方式,如果不使用特定服务引用类作为扩展方法的this
参数,就无法获取结果。从好的方面来说,虽然代码主要是“复制和粘贴”,几乎没有修改。
此外,您确实需要创建与您使用的参数一样多的扩展方法加一。
以下是我需要为示例代码定义的扩展方法:
就像我说的,很痛苦。
您当然不必创建这些类型的扩展方法来在 Silverlight 中将 Rx 与 WCF 一起使用,但我的感觉是,一旦创建了它们,客户端代码就会变得非常非常容易使用。
如果您需要更多解释,请告诉我。
I've taken the edit into account and put together a solution.
I do have to say that Microsoft has made it awfully hard to write simple WCF calls in Silverlight. Handling events to get the return data from async calls is just painful.
Having said that, with a little bit of "copy-and-paste" boiler-plate code, Rx does a great job of removing the complexity.
Here's what I came up with as the final client code:
On one hand this code isn't as pretty as I had hoped, but on the other I don't think I could get it any better. Hopefully you can see how your requirements are being met.
Now for the bad news - the extension methods must be created for every service proxy. The way the
EventArgs
are defined there is no way to get the results out without using the specific service reference class as thethis
parameter for the extension methods.On the plus side though the code is mostly "copy-and-paste" with very little modification.
Also, you do need to create as many extension methods as many parameters you use plus one.
Here are the extension methods I needed to define for my sample code:
Like I said, painful.
You certainly don't have to create these type of extension methods to use Rx with WCF in Silverlight, but it's my feeling that once you have created them then the client code becomes much, much easier to work with.
Let me know if you need more explanation on anything.