问题描述
有时我需要在 Silverlight 中调用 WCF 服务并阻止 UI,直到它返回.当然,我可以分三步完成:
Sometimes I need to call WCF service in Silverlight and block UI until it returns. Sure I can do it in three steps:
- 设置处理程序和阻止 UI
- 呼叫服务
- 完成所有操作后取消阻止 UI.
但是,我想将 DoSomethingSync 方法添加到服务客户端类,并在需要时调用它.
However, I'd like to add DoSomethingSync method to service client class and just call it whenever I need.
有可能吗?真的有人实现过这样的方法吗?
Is it possible? Has anyone really implemented such a method?
更新:看起来答案是根本不使用同步调用.将为异步调用寻找一些易于使用的模式.看看 这篇 帖子(取自来自评论)了解更多.
UPDATE:Looks like the answer is not to use sync calls at all. Will look for some easy to use pattern for async calls. Take a look at this post (taken from comments) for more.
推荐答案
重点来了;您不应该在 Silverlight 中执行同步 IO.别打了!相反:
Here's the point; you shouldn't do sync IO in Silverlight. Stop fighting it! Instead:
- 禁用 UI 的任何关键部分
- 使用回调启动异步 IO
- (...)
- 在回调中,处理数据并更新/重新启用 UI
碰巧的是,我正在积极研究使异步模式更易于使用的方法(特别是考虑到 Silverlight).这是第一次尝试,但我有更好的方法袖子;-p
As it happens, I'm actively working on ways to make the async pattern more approachable (in particular with Silverlight in mind). Here's a first stab, but I have something better up my sleeve ;-p
这篇关于如何在 SIlverlight 中实现对 WCF 服务的同步调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!