tags:

views:

319

answers:

2

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?

A: 

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

Rubens Farias
But you need to be careful not to attempt to reuse a proxy that is in the faulted state when employing this strategy.
Cocowalla
MY first thought was to reuse the client proxy but just looking at usage in some of the other applications we have here there seems to be a tendency to use and close them as soon as possible. No one has been able to explain to me why though.Will do Cocowalla!
Andrew
+3  A: 

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.

marc_s
Thanks for this idea, Ill look into it.
Andrew