Click here to Skip to main content
15,867,890 members
Articles / Web Development / IIS

Getting Away with Client Config in WCF

Rate me:
Please Sign up or sign in to vote.
3.69/5 (9 votes)
18 Aug 2007CPOL2 min read 62.5K   1.1K   24   8
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):

XML
<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):

XML
<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.

C#
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.

C#
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:

XML
<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)


Written By
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 Pin
vignesh chennai20-Jun-13 2:51
vignesh chennai20-Jun-13 2:51 
GeneralOut of Box Thinking Pin
ChrisWaldron12-May-11 17:23
ChrisWaldron12-May-11 17:23 
GeneralRe: Out of Box Thinking Pin
Sidhartha Gundavarapu13-May-11 4:31
Sidhartha Gundavarapu13-May-11 4:31 
GeneralMy vote of 5 Pin
ChrisWaldron12-May-11 17:15
ChrisWaldron12-May-11 17:15 
GeneralYeah It works Pin
Member 43440208-Dec-10 17:23
Member 43440208-Dec-10 17:23 
GeneralVery nice Pin
limbique25-Jun-09 7:22
limbique25-Jun-09 7:22 
GeneralIt works Pin
Anton Krouglov3-Oct-08 0:56
Anton Krouglov3-Oct-08 0:56 
GeneralThis is the way to go... sadly Pin
keozcigisoft12-May-08 10:41
keozcigisoft12-May-08 10:41 

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

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