Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, WCF, When to reuse a client side proxy

Tags:

c#

wcf

Im writing an application which transfers files using WCF. The transfers are done in segments so that they can be resumed after any unforeseen interruption.

My question concerns the use of the client side proxy, is it better to keep it open and reuse it to transfer each file segment or should I be reopening it each time I want to send something?

like image 472
Andrew Avatar asked Dec 08 '22 05:12

Andrew


2 Answers

The reason to close a proxy as quickly as possible is the fact that you might be having a session in place which ties up system resources (netTcpBinding uses a transport-level session, wsHttpBinding can use security or reliability-based sessions).

But you're right - as long as a client proxy isn't in a faulted state, you can totally reuse it.

If you want to go one step further, and if you can share a common assembly with the service and data contracts between server and client, you could split up the client proxy creation into two steps:

  • create a ChannelFactory<IYourServiceContract> once and cache that - this is a very expensive and resource-intensive operation; since you need to make this a generic using your service contract (interface), you need to be able to share contracts between server and client

  • given that factory, you can create your channels using factory.CreateChannel() as needed - this operation is much less "heavy" and can be done quickly and over and over again

This is one possible optimization you could look into - given the scenario that you control both ends of the communication, and you can share the contract assembly between server and client.

like image 189
marc_s Avatar answered Dec 10 '22 02:12

marc_s


You can reuse your WCF client proxy and that will make your client application faster, as proxy just will initialize once.

like image 28
Rubens Farias Avatar answered Dec 10 '22 02:12

Rubens Farias