How I can implement sync calls to WCF services in SIlverlight?

纵然是瞬间 提交于 2019-11-28 12:58:59

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

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();
}

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!