如何在 SIlverlight 中实现对 WCF 服务的同步调用?

发布于 2024-07-15 06:39:31 字数 447 浏览 5 评论 0原文

有时我需要在 Silverlight 中调用 WCF 服务并阻止 UI 直到它返回。 当然,我可以通过三个步骤来完成:

  1. 设置处理程序并阻止 UI
  2. 调用服务
  3. 当一切完成后取消阻止 UI。

但是,我想将 DoSomethingSync 方法添加到服务客户端类,并在需要时调用它。

是否可以? 有人真正实施过这样的方法吗?

更新: 看起来答案是根本不使用同步调用。 将寻找一些易于使用的异步调用模式。 看看这篇帖子(采取来自评论)了解更多。

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

  1. Setup handlers and block UI
  2. Call service
  3. Unblock UI when everything is done.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月依秋水 2024-07-22 06:39:32

这就是重点; 你不应该在Silverlight中进行同步IO。 别再战斗了! 相反:

  • 禁用 UI 的任何关键部分,
  • 在回调中使用回调
  • (...)
  • 启动异步 IO ,处理数据并更新/重新启用 UI

碰巧,我正在积极研究制作异步模式的方法更平易近人(特别是考虑到 Silverlight)。 这是第一次尝试,但我有更好的东西套筒;-p

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

  • disable any critical parts of the UI
  • start async IO with callback
  • (...)
  • in the callback, process the data and update/re-enable the UI

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

月野兔 2024-07-22 06:39:32

我不同意 Marc 的观点,确实存在需要进行同步 Web 服务调用的情况。 然而,您可能应该避免的是 UI 线程上的阻塞,因为这会造成非常糟糕的用户体验。

同步实现服务调用的一种非常简单的方法是使用 ManualResetEvent。

ManualResetEvent m_svcMRE = new ManualResetEvent(false);
MyServiceClient m_svcProxy = new MyServiceClient(binding, address);
m_svcProxy.DoSomethingCompleted += (sender, args) => {  m_svcMRE.Set(); };

public void DoSomething()
{
    m_svcMRE.Reset();
    m_svcProxy.DoSomething();
    m_svcMRE.WaitOne();
}

I'd disagree with Marc there are genuine cases where you need to do synchronous web service calls. However what you probably should avoid is blocking on the UI thread as that creates a very bad user experience.

A very simple way to implement a service call synchronously is to use a ManualResetEvent.

ManualResetEvent m_svcMRE = new ManualResetEvent(false);
MyServiceClient m_svcProxy = new MyServiceClient(binding, address);
m_svcProxy.DoSomethingCompleted += (sender, args) => {  m_svcMRE.Set(); };

public void DoSomething()
{
    m_svcMRE.Reset();
    m_svcProxy.DoSomething();
    m_svcMRE.WaitOne();
}
我ぃ本無心為│何有愛 2024-07-22 06:39:32

下面是一个类,可让您在 SL 中同步调用 WCF 服务:
http://johnleitch.blogspot.com/ 2010/03/easy-way-synchronously-call-wcf.html

Here's a class that will let you synchronously call WCF services in SL:
http://johnleitch.blogspot.com/2010/03/easy-way-to-synchronously-call-wcf.html

忆梦 2024-07-22 06:39:32

使用 SL4 构建业务应用程序本身就是一种错误的方法。 我在 SL4 客户端中遇到异步调用问题。 不仅是这个问题。 一旦您在登录 SL 应用程序后停留 15 分钟,中断后您的 SL 应用程序将无法保留所有数据。 它有时会非常频繁地发生。

Using SL4 building business application itself a wrong approach. I am facing problem with async call in SL4 client. Not only this issue. Once you go for a 15mins beeak after login SL application, after break you SL app can not retain all data at all. Its happen sometimes very frequently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文