Reputation: 5900
I currently have a number of ViewModels in my WPF application that requires data from a WCF service. Currently, each ViewModel stores a reference to a (different) serviceproxy. The problem is, the WCF is not closing gracefully (server is reporting unexpected closing of the connection) on application exit and if the user is idle for long periods the connection times out.
I was thinking about two different ways to use the WCF within my application:
Create the serviceproxy within the ViewModel and access the data within a Using() statement so that I am only keeping the connection open for short periods.
Use a singleton of the WCF that all ViewModels can access for each call.
The problem with (1) is that I made calls to the WCF for CanExecute methods to determine if buttons are enabled or disabled on my GUI. I am concerned that making so many proxies is going to be expensive and slow.
The problem I see with (2) is one I am already noticing: if a WCF connection is idle for long periods, it times out and the proxy is faulted. That means even more error handling.
If anyone has a good pattern for this sort of data access, I'd appreciate any insight.
EDIT: I get that using the WCF is a bad idea for the CanExcute. If it causes problems, I'll change it but right now this application has 3 users. I am merely looking for a good way to access the service from within my ViewModel that prevents disconnects and is also testable.
Upvotes: 0
Views: 310
Reputation: 1341
Upvotes: 0
Reputation: 833
It is bad practice to do blocking calls (like WCF calls) from CanExecute code. Your UI is blocked while those calls are running. And CanExecute gets called far too many times, so your server will get performance issues too...
What you need is a way for the server to signal the client(s) that the CanExecutes have changed. For example: when one client deletes a customer, the server can raise a 'customer deleted' event that other clients can subscribe to, disabling their 'Edit' button if they have that particular customer on their screen.
Search the web for publish/subscribe for help implementing that pattern. You can download a 'publish subscribe framework' using WCF from the IDesign.net website here: http://www.idesign.net/Downloads/GetDownload/2032
Upvotes: 1