This weblog provides a solution to one of the most common problem 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 weblog.
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 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.
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 generate 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) : base(endpointConfigurationName) { } public HelloWorldServiceClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public HelloWorldServiceClient(string endpointConfigurationName, EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public HelloWorldServiceClient(Binding binding, EndpointAddress remoteAddress) : base(binding, 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>
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 ofcourse, 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.
Introduction This weblog provides a solution to one of the most common problem faced by developers while