Click here to Skip to main content
15,215,147 members

How to make your Web Reference proxy URL dynamic

Rate this:
4.91 (40 votes)
Please Sign up or sign in to vote.
4.91 (40 votes)
30 Nov 2005
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

Comments and Discussions

QuestionDynamic uses Settings.Settings, not web.config Pin
Member 119716339-Sep-15 5:11
MemberMember 119716339-Sep-15 5:11 
AnswerRe: Dynamic uses Settings.Settings, not web.config Pin
Mujtaba Faizan9-Dec-15 2:12
MemberMujtaba Faizan9-Dec-15 2:12 
GeneralRe: Dynamic uses Settings.Settings, not web.config Pin
lokeswararao22-Dec-15 10:10
Memberlokeswararao22-Dec-15 10:10 
QuestionAnother Option: Pin
LLloody1-Jun-15 6:52
MemberLLloody1-Jun-15 6:52 
GeneralMy vote of 5 Pin
Amol_B26-Jun-13 20:05
professionalAmol_B26-Jun-13 20:05 
GeneralMy vote of 5 Pin
pen56015-Jan-13 19:44
Memberpen56015-Jan-13 19:44 
GeneralMy vote of 1 Pin
Rakesh Hegishte4-Jun-12 2:14
professionalRakesh Hegishte4-Jun-12 2:14 
QuestionVS 2010 Pin
hidekiwing20-Mar-12 5:53
Memberhidekiwing20-Mar-12 5:53 
QuestionI am not getting the code Pin
Kumar_Gaurav8-Feb-12 21:04
MemberKumar_Gaurav8-Feb-12 21:04 
GeneralThanks Pin
p_karthik_p24-May-11 1:39
Memberp_karthik_p24-May-11 1:39 
GeneralMy vote of 5 Pin
coreprojectthang14-Nov-10 21:44
Membercoreprojectthang14-Nov-10 21:44 
GeneralDoesn't work with VS 2008 Pin
freephone13-Oct-10 14:59
Memberfreephone13-Oct-10 14:59 
GeneralThank you Thank you Thank you Pin
adg123419-Sep-10 20:43
Memberadg123419-Sep-10 20:43 
GeneralSeems slightly different for a Forms App ... (there is no web.config file) Pin
Martin Koster11-Jan-09 23:23
MemberMartin Koster11-Jan-09 23:23 
GeneralIt only works if you don't use the WSE types Pin
stephg12317-May-08 6:54
Memberstephg12317-May-08 6:54 
GeneralThanks Pin
lcsistemas1-May-08 6:24
Memberlcsistemas1-May-08 6:24 
GeneralThat is pretty staightforward. Pin
Member 45177303-Dec-07 10:00
MemberMember 45177303-Dec-07 10:00 
GeneralIn VS05 WinForm and ClassLibrary projects applicationSettings section is used Pin
Michael Freidgeim7-May-06 17:09
MemberMichael Freidgeim7-May-06 17:09 
GeneralA better way Pin
Ben Powell6-Dec-05 4:54
MemberBen Powell6-Dec-05 4:54 
GeneralRe: A better way Pin
ruionwriting23-Jan-06 0:23
Memberruionwriting23-Jan-06 0:23 
GeneralRe: A better way Pin
mikker_1232-Apr-06 23:51
Membermikker_1232-Apr-06 23:51 
AnswerRe: A better way Pin
Peter_Bridger19-Aug-10 23:24
MemberPeter_Bridger19-Aug-10 23:24 
GeneralRe: A better way Pin
david markal30-Apr-12 2:40
Memberdavid markal30-Apr-12 2:40 
GeneralRe: A better way Pin
Stuart Dobson7-Oct-12 17:48
MemberStuart Dobson7-Oct-12 17:48 
GeneralIt works, sort of.... Pin
Zod The Mighty1-Dec-05 4:20
MemberZod The Mighty1-Dec-05 4:20 

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.

Posted 30 Nov 2005


76 bookmarked