How can we host a service on two different protocols on a single server?

 Posted by articlesMaint on 9/14/2009 | Category: WCF Interview questions | Views: 30334


Let us first understand what this question actually means. Let us say we have made a service and we want to host this service using HTTP as well as TCP. You must be wondering why to ever host services on two different types of protocol. When we host a service, multiple types of client consume it and it’s very much possible that they have there own protocol of communication. A good service has the capability to downgrade or upgrade its protocol according the client who is consuming him.
Let us do a small sample in which we will host the ServiceGetCost on TCP and HTTP protocol.
 


Note: - You can the below sample in “WCFMultipleProtocolGetHost” folder in the CD provided.



Below is the code snippet pasted from the same sample? As usual, we have numbered them and here is the explanation of the same:-
1 and 2 - As we are hosting the service on two protocols we need to create two objects if the URI. You can also give the URI through config file. Pass these two objects of the URI in the constructor parameter when creating the service host object.





 


Figure 20:- Server side code for Multi Protocol hosting



3 – In the config file we need to define two bindings and end as we are hosting the service in multiple protocols.
Once we are done the server side coding its time to see make a client by which we can switch between the protocols and see the results. Below is the code snippet of the client side for multi-protocol hosting.
 



Figure 21:- Multi Protocol Client code



Let us understand the code:-
1 - In the generated config file we have added two ends. When we generate the config file, it generates only for one protocol. The other end has to be manually added.
2 - To test the same we have a list box, which has the name value given in the end point.
3 - In the list box select event we have then loaded the selected protocol. The selected protocol string needs to be given in the proxy class and finally call the proxy class GetTotalCost.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response