In this section, thought of sharing one of the interesting stuff around WCF topic. Here, in this post, I will be explaining how to create Channel Factory first then in 2nd post, we will see how to create how to create channel factory using reflection and why it is actually needed. Hence, let’s get started with Channel Factory definition first of all.
A proxy is a simple class. It is a class, which you can instantiate like a regular class and access its members via object. However, internally proxy creates a channel as we have already seen the usage of channel by calling upon channel property. Proxy is nothing but an implementation of Service Contract. This implementation of service contract can be obtained without actual proxy class rather virtual proxy. This is where Channel Factory comes in. Channel Factory can create a proxy for you and return to you created implementer of service contract without you actually create proxy class explicitly. In addition, channel factory can be accessed both config and config-less fashion. Here, is the code for button click event via channel factory.Let me explain the code a bit. First, I have created the channel factory with the Client Service Contract. Now, Empty quote here means I am going to refer my only endpoint for the particular contract. If I will be using multiple for the same contract, then in that case I need to provide the name explicitly. However, here I cannot use default constructor without quotes. Therefore, quotes are mandatory here. Then, I have obtained channel from this factory. As I said, channel is going to be represented by the implementation of service contract. Therefore, here I have created one virtual proxy class and used the same to print the movie name. In the end, I have closed the factory as we used to close proxy. Therefore, with the above changes in place when I build and run the app, then it will produce me the below output. Above thing achieved via config. Now, the same thing can be achieved configs-less way. Below, I have just tweaked the code for the same.
In order to understand WCF End-To-End, you can refer my book @ http://bit.ly/pragmaticWCF-kindle. Entire example is from my book itself.
Code you can download @ https://github.com/rahulsahay19/MovieLib-WCF.
Thanks for Joining me. In the next section, we’ll look at advance way of creating factory out of contracts.
488 total views, 1 views today