Click here to Skip to main content
Licence CPOL
First Posted 18 Aug 2007
Views 31,544
Downloads 573
Bookmarked 21 times

Getting Away with Client Config in WCF

By | 18 Aug 2007 | Article
This article provides a solution to the configuration file overwriting problem while developing WCF Services and using Visual Studio for development

Introduction

This article provides a solution to one of the most common problems faced by developers while developing their projects involving WCF Services using Visual Studio.

Background

To give you a little background on what this is all about… when you are working with WCF services and using Visual Studio for development, it becomes annoying as to how Visual Studio screws up the configuration settings. This is a small problem with an even smaller solution. Hence, it might be the smallest article.

Using the Code

There are two attachments with this article.

  1. NoConfigWS.zip is the web service. You've to unzip the contents and create a web application using IIS. I'll not go into the details on doing that, you can figure out on your own if you are not familiar by searching the internet.
  2. NoConfigClient.zip is the client that consumes the NoConfigWS web service. This is a console application that communicates with the service and displays the results back. All you need to do is to unzip the contents and change the app.config file to point your service.

Problem

This sample will help you in understanding the situation better.

Before updating (add service reference and changing some values):

<binding 
  name="BasicHttpBinding_IHelloWorldService" 
  closeTimeout="00:01:00" 
  openTimeout="00:01:00" 
  receiveTimeout="00:10:00" 
  sendTimeout="00:01:00" 
  allowCookies="false" 
  bypassProxyOnLocal="false" 
  hostNameComparisonMode="StrongWildcard" 
  maxBufferSize="131072" 
  maxBufferPoolSize="524288" 
  maxReceivedMessageSize="131072" 
  messageEncoding="Text" 
  textEncoding="utf-8" 
  transferMode="Buffered" 
  useDefaultWebProxy="true">
  <readerQuotas 
    maxDepth="32" 
    maxStringContentLength="8192" 
    maxArrayLength="16384" 
    maxBytesPerRead="4096" 
    maxNameTableCharCount="16384" />
    <security mode="None">
    <transport 
      clientCredentialType="None" 
      proxyCredentialType="None" 
      realm="" />
    <message 
      clientCredentialType="UserName" 
      algorithmSuite="Default" />
  </security>
</binding>

After changes (and updating the service reference):

<binding 
  name="BasicHttpBinding_IHelloWorldService" 
  closeTimeout="00:01:00" 
  openTimeout="00:01:00" 
  receiveTimeout="00:10:00" 
  sendTimeout="00:01:00" 
  allowCookies="false" 
  bypassProxyOnLocal="false" 
  hostNameComparisonMode="StrongWildcard" 
  maxBufferSize="65536" 
  maxBufferPoolSize="524288" 
  maxReceivedMessageSize="65536" 
  messageEncoding="Text" 
  textEncoding="utf-8" 
  transferMode="Buffered" 
  useDefaultWebProxy="true">
  <readerQuotas 
    maxDepth="32" 
    maxStringContentLength="8192" 
    maxArrayLength="16384" 
    maxBytesPerRead="4096" 
    maxNameTableCharCount="16384" />
  <security mode="None">
    <transport 
      clientCredentialType="None" 
      proxyCredentialType="None" 
      realm="" />
    <message 
      clientCredentialType="UserName" 
      algorithmSuite="Default" />
  </security>
</binding>

This becomes really painful if you had to change some settings (for example, maxReceivedMessageSize or maxStringContentLength) to a different value than the default for some of the services. Every time you update the service reference, Visual Studio will replace the updated settings with the defaults. For situations where multiple developers are developing the services, this problem only grows exponentially.

Solution

There are several solutions for this problem, and the one presented here is just one of them.

Whatever is your development environment to create your services, whether using Visual Studio or directly generating it, SVCUtil will generate multiple constructors for the proxy class (five to be exact). Guess what, one of them takes a Binding and EndpointAddress as parameters.

public HelloWorldServiceClient()
public HelloWorldServiceClient(string endpointConfigurationName)
public HelloWorldServiceClient(string endpointConfigurationName, string remoteAddress)
public HelloWorldServiceClient
	(string endpointConfigurationName, EndpointAddress remoteAddress)
public HelloWorldServiceClient(Binding binding, EndpointAddress remoteAddress)

Now, our job is to create these objects and create the proxy by passing these objects.

HelloWorldServiceClient helloWorldClient = new HelloWorldServiceClient(
      ProxyHelper.GetBinding(),
      ProxyHelper.GetEndpoint() );

GetBinding() and GetEndpoint() are helper methods I've used to create those objects. The complete source code for this article is available as a download.

With this approach, your final Config file would be as clean as follows:

<configuration>
  <appSettings>
    <add 
      key="webserviceURL"
      value="http://localhost/NoConfigWS/Service.svc"/>
  </appSettings>
</configuration>

Summary

This, IMO, is much cleaner for your end user to maintain and by the way, who better knows the advanced Config settings than the developer (and of course, the advanced network administrators).

Once done, you can rest in peace… I mean work on your services without ever worrying about Visual Studio playing with your config file anymore.

History

  • 18th August, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sidhartha Gundavarapu

Other
Microsoft
India India

Member

I'm working as a Consultant at Microsoft Global Services and like to spend my spare time investigating new stuff and write articles about the ones that are less discussed about. Please feel free to send your comments/suggestions/corrections on any of my work.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralOut of Box Thinking PinmemberChrisWaldron17:23 12 May '11  
GeneralRe: Out of Box Thinking PinmemberSidhartha Gundavarapu4:31 13 May '11  
GeneralMy vote of 5 PinmemberChrisWaldron17:15 12 May '11  
GeneralYeah It works PinmemberMember 434402017:23 8 Dec '10  
GeneralVery nice Pinmemberlimbique7:22 25 Jun '09  
GeneralIt works PinmemberAnton Krouglov0:56 3 Oct '08  
GeneralThis is the way to go... sadly Pinmemberchaosgeorge10:41 12 May '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 18 Aug 2007
Article Copyright 2007 by Sidhartha Gundavarapu
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid