WCF Services and Web Parts

Leave a comment

August 21, 2012 by Ruaan Harmzen

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

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: