I am consuming a WCF service and created its proxy using the VS 2008 service reference.
I am looking for the best pattern to call WCF service method
I am using the .Net framework 3.5 SP1, basicHttp
binding with little customization.
The WCF client proxy can be generated manually by using the Service Model Metadata Utility Tool (SvcUtil.exe) for more information see, ServiceModel Metadata Utility Tool (Svcutil.exe). The WCF client proxy can also be generated within Visual Studio using the Add Service Reference feature.
Actually Proxy is a class in WCF that is used to communicate with client application. We can simply get the entire configuration through the proxy class. There is no need to do extra effort to generate the configuration setting for the client. Proxy class used when you think that your service must be loosely coupled.
A metadata exchange endpoint is required to support the dynamic generation of proxy and configuration for client applications. You must explicitly enable metadata exchange by adding the endpoint and enabling the metadata exchange behavior.
Select Open. From the View menu, select Solution Explorer. In the Solution Explorer window, select the GettingStarted solution (top node), and then select Add > New Project from the shortcut menu. In the Add New Project window, on the left side, select the Windows Desktop category under Visual C# or Visual Basic.
It depends ;-)
If you have a sequence in your app that requires several calls after one another, you could hang on to the proxy client and keep using it to make further calls. Be warned though to check for the "faulted" state - if an error happens on the server, the channel between the client proxy and the server might "fault" and thus your client proxy becomes unusable.
Also - the expensive part is the creation of the ChannelFactory<T>
- you could try to separate these two steps out when you create your client proyx in code:
ChannelFactory<IYourService> factory = new ChannelFactory<IYourService>();
Hang on to that channel factory, e.g. cache it somewhere
The second step should be much less intensive in terms of time and horsepower:
IYourService client = factory.CreateChannel();
You could do this step before every call (or call sequence) and shouldn't get bad performance out of that, really.
I would strongly recommend to avoid singletons whenever possible - it's like opening a can of worms, don't do it unless you absolutely, positively have to (e.g. to manage access to a single resource that's only available for one caller at a time).
Marc
Sorry for kicking up an old question, but I wanted to add this for easy reference.
I fully agree with marc_s and rally25rs. So start there, but also consider using a proxy or wrapper that handles faulted states. Here is a question on SO that discusses some solutions, and here is another good solution I came across on the internet by Corneliu, "Building a reusable ClientBase proxy". His solution generates wrappers that expose your service methods for maximum convenience and performance. I still have to test if it works though :).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With