Tuesday, 12 March 2013

Calling WCF/Web Service Without Adding Service Reference , by using ChannelFactory


Hi friends,

In this article i will tell you how to call WCF service without creating service reference


Sometimes we need to calling service without adding service reference.
The ChannelFactory object is used to create a communication channel with the service. Note that the BasicHttpBinding is used (because it was used in configuration of the service's host) and the same URL address

The used ChannelFactory object is created for the service's interface  and is able to create an object implementing this interface which can be easily used in the code 
Following steps are required for creating channelFactory
Step 1 : Initialize Channel factory   

Step  2 : create channel factory

Step  3 :  Access Proxy

 
Example :
 
Let Assume that your service name is ConfigurationServices and it has method getCustomerDetails
we retrieve User Details based on UserId using getCustomerDetails method.   

Below is the code snippet.

var factory = new ChannelFactory<ConfigurationServices>(new BasicHttpBinding(), 
           new EndpointAddress("http://localhost/HirenWebServices/ConfigurationServices"));

 
 
var proxy = factory.CreateChannel();

var response = proxy.getCustomerDetails(
           new getCustomerDetails() { arg0 = AppConfiguration.Current.Userid });

var v = response.@return;





Happy Programming!!
Don’t forget to leave your feedback and comments below!
If you have any query mail me to Sujeet.bhujbal@gmail.com     
Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
-----------------------------------------------------------------------------------

2 comments :

Julia David said...

Your nice work is very attractive and impressive.
1337x UK proxy

Julia David said...

This is so essential post. This information helps them who are new bloggers. Thanks for helpful post for us. 
Mp3Juices UK proxy

Post a Comment