我如何能实现在Silverlight中的同步调用WCF服务?

问题描述:

有时候,我需要调用WCF服务在Silverlight并阻止用户界面,直到它返回。当然,我能做到这一点的三个步骤:

Sometimes I need to call WCF service in Silverlight and block UI until it returns. Sure I can do it in three steps:


  1. 设置处理程序和块UI

  2. 呼叫服务

  3. 阻止用户界面,当一切都已经完成。

不过,我想补充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.

这里的点;您不该做同步IO在Silverlight。停止战斗吧!相反:

Here's the point; you shouldn't do sync IO in Silverlight. Stop fighting it! Instead:


  • 禁用UI

  • 的任何关键部件开始异步IO与回调

  • (...)

  • 在回调,处理数据并更新/重新启用UI

  • disable any critical parts of the UI
  • start async IO with callback
  • (...)
  • in the callback, process the data and update/re-enable the 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