WCF Services and Web Parts

This post will cover how to use and call WCF services without complicated methods to update your web.config\app.config. When you add a WCF service reference to your code it generates all the endpoints in your project’s app.config file. For web parts this is a problem as each web part does not have it’s own app.config but shares the web.config of the web application. There are some methods (feature listeners\events) where you can update the web application’s web.config when your feature gets activated or installed. I found this to be overkill and too much management is required. My solution was to use the ChannelFactory class to call the web service. The code:

// Create Service based on Endpoint Uri serviceUri = new Uri(“[The Endpoint URL]”); BasicHttpBinding serviceBinding = new BasicHttpBinding(); // Set for larger messages serviceBinding.MaxReceivedMessageSize = 2147483647; // Create new channel to service EndpointAddress MyWCFService = new EndpointAddress(serviceUri); ChannelFactory<MyWCFService.MyWCFClass> channelFactory = new ChannelFactory<MyWCFService.MyWCFClass>(serviceBinding, MyWCFService); // Create client channel MyWCFService.MyWCFClass client = channelFactory.CreateChannel(); // Call method object var = client.MyMethod(); No web.config\app.config required!

Leave a Reply

Your email address will not be published. Required fields are marked *