Does generated WCF service client in Silverlight re-use the ChannelFactory?

656 views Asked by At

Should one use the generated WCF service client (e.g. MyServiceClient) when invoking a WCF service from silverlight, or instead use the ChannelFactory (e.g. ChannelFactory.Create())?

Other questions have asked this question: WCF/Silverlight: Why use a ChannelFactory instead of a Client?

However, the responses just say it's best to re-use the ChannelFactory. But if this is done directly, you lose all of the other functionality of the generated WCF service client (the async events, etc.)

Is there no way to get the generated WCF service client to re-use the ChannelFactory itself?

1

There are 1 answers

0
Aliostad On BEST ANSWER

WCF Fiasco

START_RANT

Even Microsoft and WCF team know that the way ChannelFactory and Channel and ClientBase was designed and the way it was changed because of various performance issues was a real fiasco. Just the fact that the disposing of proxy is a real mess is enough to know underneath extensibility of WCF, we are dealing with an inefficent, bloated and convoluted design. Just bad bad bad.

END_RANT

OK, you probably have looked at documentation but I assume you have not been able to find your answers - as I did not - since there is no easy answer. But here my findings which hopefully summarises these points (you cannot find below in any MS documentation):

1) We have ChannelFactory and IChannel. ChannelFactory knows how to communicate with the transport and channel knows how to interprete parameters to WCF method calls and Vice Versa. IChannel is the one that "implements" (or looks like implementing) your service interface while ChannelFactory has all necessary abstractions for communicating (including endpoint, security, ...).

2) ClientBase is nothing but both ChannelFactory and IChannel cobbled together. So it is obvious that creating ChannelFactory once and then creating channels over and over means it is more efficient, howevere as you guessed it is not that easy because ...

3) ChannelFactory once opened, cannot be changed. This means that if you use username password to connect to WCF service and that username password changes, then you need a new one. This might not seems like a big problem but it becomes a big problem in an ASP NET scenario.

4) ChannelFactory and IChannel in fact reuse the same underlying communication channels so closing ChannelFactory will make IChannel invalid. This causes confusion in clean up code and decising whose responsiblity is it to close the connection.

5) As you see there is no easy answer. If you do not have to use username/password or change configuration at runtime, create and cache ChannelFactory and then just create the channels. In any case ChannelFactory and IChannel is more efficient than ClientBase.