Click here to Skip to main content
13,288,520 members (78,801 online)
Click here to Skip to main content
Add your own
alternative version


74 bookmarked
Posted 30 Nov 2005

How to make your Web Reference proxy URL dynamic

, 30 Nov 2005
Rate this:
Please Sign up or sign in to vote.
This is a quick helper to allow you to make the web reference URLs for your WSDL proxies easier to configure when switching environments.


I have been asked before, how to make the URL property for a web reference to a web service in a project, configurable in a config file instead of compiled in the web reference proxy. This is most useful when you want to deploy a project with a web reference between different business environments (like between Test/QA and Production) without recompiling the project. There is a simple way to do this that requires no coding at all on the developer's part (provided you are using Visual Studio for .NET).

Getting Started

To follow this article, first you need to have a compiled web service to reference, and a project in which you wish to add a web reference. After you have added your web reference (which creates the proxy class with references to the web service for you automatically using VS .NET), you need to set your solution view to 'Show All':

Solution Explorer Show All Files

This shows the Reference.cs file for the This is the proxy class file that VS.NET generates automatically for you when you add a web reference.

Open up this file and notice under the constructor for the proxy class that the URL is hard coded for you inside the constructor:

Sample screenshot

Changing the URL from Static to Dynamic

We are going to change the hard coded URL in the proxy class Reference.cs to a key in the web.config of the web service client project. An appSettings section will be added automatically with the current URL, and code will be placed in the Reference.cs proxy class constructor to look for the URL there. All this will be done by changing one property setting on the proxy reference.

If you look at the web reference properties (below):

Sample screenshot

you can see that there is a configuration setting called 'URL Behavior'. This setting is by default set to Static.

Sample screenshot

To make the URL in the Reference.cs map class code behind look for the web service URL in your web.config file, we need to change this setting to Dynamic:

Sample screenshot

Doing this in VS.NET does two things for you. It changes the Reference.cs file to have the code to look for the WSDL URL in the project's web.config file:

Sample screenshot

And it adds the URL as a key value to the projects web.config file under appSettings:

Sample screenshot

Now you can set the URL to different servers for deployment in different environments, without having to change the code. You just change the URL in your config file for your project.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Christopher G. Lasater
Web Developer
United States United States
Christopher G. Lasater

I am also a published author, please check out my book:
ISBN: 1-59822-031-4
Title: Design Patterns
Author:Christopher G. Lasater
More from my web site

You may also be interested in...


Comments and Discussions

GeneralRe: It works, sort of.... Pin
chris lasater1-Dec-05 6:27
memberchris lasater1-Dec-05 6:27 
GeneralRe: It works, sort of.... Pin
Zod The Mighty1-Dec-05 7:54
memberZod The Mighty1-Dec-05 7:54 
GeneralIt works but ... Pin
Doubin30-Nov-05 23:13
memberDoubin30-Nov-05 23:13 
GeneralRe: It works but ... Pin
lunky1-Dec-05 2:33
memberlunky1-Dec-05 2:33 
GeneralRe: It works but ... Pin
ceddie8-Dec-05 16:56
memberceddie8-Dec-05 16:56 
GeneralRe: It works but ... Pin
lunky9-Dec-05 4:29
memberlunky9-Dec-05 4:29 
GeneralRe: It works but ... Pin
ceddie9-Dec-05 5:53
memberceddie9-Dec-05 5:53 
GeneralRe: It works but ... Pin
floverso1-Dec-06 12:18
memberfloverso1-Dec-06 12:18 
1- En ocasiones debe modificarse la clase proxy para incluir algunos aspectos en el llamado (como ejemplo las credenciales al WS, que también se pueden pasar en cada llamado, pero considero que no, pues es un tema para encapsularse e independizar al cliente de esto)... en ocasiones debe re-crearse la referencia (por esas situaciones "que ocurren" cuando se trabaja en proyectos en VS) y la clase proxy es generada nuevamente pudiendo sobreescribir código.

2- El key siempre es generado en el config del cliente que referencia el WS. ¿qué pasa cuando el cliente es una dll?... que el parámetro no será usado y manualmente debe copiarse en el config de la aplicación que inicia el AppDomain (ejemplo: la app web que llama a la dll que consume el ws... el key debe crearse en el config de la app web)... y deberá parametrizarse por cada cliente que lo consuma

3- Por último y "ya por sólo gusto", el nombre del key que se emplea en el código y que es autogenerado en el config, no siempre es intuitivo o no se adapta a un estándar del proyecto... Un valor como:
<add key="WSETest.localhost.BussinessService.Security" value="La URL" />
podría remplazarse con: <add key="wsSeguridad" value="La URL"/> y ser más claro para quien use el parámetro "wsSeguridad" en los componentes


GeneralRe: It works but ... Pin
floverso1-Dec-06 12:19
memberfloverso1-Dec-06 12:19 
GeneralRe: It works but ... Pin
cyberpunk_np7-May-07 19:42
membercyberpunk_np7-May-07 19:42 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171207.1 | Last Updated 30 Nov 2005
Article Copyright 2005 by Christopher G. Lasater
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid