Click here to Skip to main content
Click here to Skip to main content

Getting Away with Client Config in WCF

, 18 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Sidhartha Gundavarapu
Microsoft
India India
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.

Comments and Discussions

 
Questionneed help PinprofessionalMember 1004153520-Jun-13 3:51 
GeneralOut of Box Thinking PinmemberChrisWaldron12-May-11 18:23 
GeneralRe: Out of Box Thinking PinmemberSidhartha Gundavarapu13-May-11 5:31 
GeneralMy vote of 5 PinmemberChrisWaldron12-May-11 18:15 
GeneralYeah It works PinmemberMember 43440208-Dec-10 18:23 
GeneralVery nice Pinmemberlimbique25-Jun-09 8:22 
GeneralIt works PinmemberAnton Krouglov3-Oct-08 1:56 
GeneralThis is the way to go... sadly Pinmemberchaosgeorge12-May-08 11:41 

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.

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