C# 如何在 SIlverlight 中实现对 WCF 服务的同步调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/647481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I can implement sync calls to WCF services in SIlverlight?
提问by Nikolay R
Sometimes I need to call WCF service in Silverlight and block UI until it returns. Sure I can do it in three steps:
有时我需要在 Silverlight 中调用 WCF 服务并阻止 UI,直到它返回。当然,我可以分三步完成:
- Setup handlers and block UI
- Call service
- Unblock UI when everything is done.
- 设置处理程序和块 UI
- 呼叫服务
- 完成所有操作后取消阻止 UI。
However, I'd like to add DoSomethingSync method to service client class and just call it whenever I need.
但是,我想将 DoSomethingSync 方法添加到服务客户端类,并在需要时调用它。
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 thispost (taken from comments) for more.
更新:看起来答案是根本不使用同步调用。将为异步调用寻找一些易于使用的模式。看看这篇文章(取自评论)了解更多。
采纳答案by Marc Gravell
Here's the point; you shouldn'tdo sync IO in Silverlight. Stop fighting it! Instead:
这就是重点;你不应该在 Silverlight 中做同步 IO。别打了!反而:
- disable any critical parts of the UI
- start async IO with callback
- (...)
- in the callback, process the data and update/re-enable the UI
- 禁用 UI 的任何关键部分
- 使用回调启动异步 IO
- (……)
- 在回调中,处理数据并更新/重新启用 UI
As it happens, I'm actively working on ways to make the async pattern more approachable (in particular with Silverlight in mind). Here'sa first stab, but I have something better up my sleeve ;-p
碰巧的是,我正在积极研究使异步模式更易于使用的方法(特别是考虑到 Silverlight)。这是第一次刺伤,但我有更好的东西;-p
回答by sipwiz
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.
我不同意 Marc 的观点,在某些情况下您需要进行同步 Web 服务调用。但是,您可能应该避免阻塞 UI 线程,因为这会造成非常糟糕的用户体验。
A very simple way to implement a service call synchronously is to use a ManualResetEvent.
同步实现服务调用的一种非常简单的方法是使用 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();
}
回答by John Leitch
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
这是一个可以让您在 SL 中同步调用 WCF 服务的类:http: //johnleitch.blogspot.com/2010/03/easy-way-to-synchronously-call-wcf.html
回答by Raju
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.
使用 SL4 构建业务应用程序本身是一种错误的方法。我在 SL4 客户端中遇到异步调用问题。不仅是这个问题。一旦您在登录 SL 应用程序后进行 15 分钟的搜索,中断后您的 SL 应用程序根本无法保留所有数据。它有时会非常频繁地发生。