Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / IIS

Getting Away with Client Config in WCF

3.69/5 (9 votes)
18 Aug 2007CPOL2 min read 1   1.1K  
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)