开发者

WPF application freezes on first WCF call

开发者 https://www.devze.com 2023-03-20 19:11 出处:网络
I have WPF application that needs to access WCF service at start ( login window ). Ea开发者_StackOverflow社区ch time application runs on Windows 7 it freezes on login until gets a responce from WCF. I

I have WPF application that needs to access WCF service at start ( login window ). Ea开发者_StackOverflow社区ch time application runs on Windows 7 it freezes on login until gets a responce from WCF. Is there any way to design this process differently?


It sounds like you need to make your calls asynchronously. Either start the call on a new thread (preferably using a Task), or call the WCF service using an asynchronous design pattern.


Put the WCF call in a background thread


There are two causes for something perceived as "freezing" when carrying out WCF Service Calls:

  1. Calling the service in a synchronous fashion will block your UI thread until the call has completed. This bad and the reason why Silverlight forbids synchronous calls and forces you to follow the Begin/End Async pattern for any kind of RPC - be it WebRequest or WCF Layer. By default the async methods are not generated when adding a service reference to your WPF project but you can turn it on using Configure Service Reference.
  2. The second cause is less obvious. The initial service client instantiation can take almost 3 seconds - even on fast machines. That's why you are well advised to QueueWorkUserItem the proxy instantiation and the BeginXXX call even when using the async pattern.


If you need the response from this service call to start the application, you can use a background thread to call this service and handle the return value. While this thread is consuming the service you can display your window or a splash screen.

If you don't need the retrun value from this service method you can use [OperationContract(IsOneWay=true)] on your service. So that you don't need to worry about threading and stuff.


You're calling a WCF call on the main thread, Therefore it will appear to crash.

  • You can either, put it in a thread and call it at the start of your app. Put it in a background process ( if you're in visual studio you can drag this off the tool bar)

You can do threads quite easily by defining a Thread, then defining a threadstart, passing in your login WCF call, and the call thread.start(); and pass in your threadstart you defined.

A background worker is pretty similar, you can put your code in the backgroundWorker1_DoWork() method

  • Or make your WCF call Async, so it will send off the login response, and your login code will call on the "OnTaskCompleted" method( you could also put it on a new thread as well, but don't really have to)

Try this thread for Async WCF calls

How to make a call to my WCF service asynchronous?

0

精彩评论

暂无评论...
验证码 换一张
取 消