Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF client proxy initialization

Tags:

c#

wcf

channel

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

  • Should I create the client proxy instance every time I call the service method and close the client as soon as I am done with that? When I profiled my client application, I could see that it is taking a lot of time to get the Channel while initializing the proxy client
  • Should I use a Singleton pattern for the client proxy so that I can use the only once instance and get rid of the re-initializing overhead? Is there any hidden problem with this approach?

I am using the .Net framework 3.5 SP1, basicHttp binding with little customization.

like image 454
123Developer Avatar asked Nov 05 '09 16:11

123Developer


People also ask

How to generate proxy for WCF service?

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.

What is WCF client proxy?

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.

What is required by the client to generate proxy?

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.

How to create a WCF client in c#?

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.


2 Answers

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

like image 170
marc_s Avatar answered Sep 19 '22 03:09

marc_s


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 :).

like image 29
Andre Luus Avatar answered Sep 20 '22 03:09

Andre Luus