Click here to Skip to main content
6,822,123 members and growing! (17,281 online)
Email Password   helpLost your password?
Languages » XML » Web Services     Intermediate

How to make your Web Reference proxy URL dynamic

By Christopher G. Lasater

This is a quick helper to allow you to make the web reference URLs for your WSDL proxies easier to configure when switching environments.
XML, Windows, .NET, ASP.NET, Visual-Studio, Dev
Posted:30 Nov 2005
Views:114,188
Bookmarked:49 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 5.84 Rating: 4.29 out of 5
1 vote, 4.3%
1
1 vote, 4.3%
2

3
3 votes, 13.0%
4
18 votes, 78.3%
5

Introduction

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 Reference.map. 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.

License

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


Member
Christopher G. Lasater
See my Website for Updates to books, articles and more...

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


Wordware is offering a 35% discount and free freight (continental U.S. only) to all Codeproject members/visitors when the book is purchased from www.wordware.com. The discount is good not only for my book, but for all books purchased from the site. Just enter the coupon code dp0314 when ordering.


Please see his blog for article, product and discussion information.
Occupation: Web Developer
Location: United States United States

Other popular XML articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralSeems slightly different for a Forms App ... (there is no web.config file) PinmemberMartin Koster0:23 12 Jan '09  
GeneralIt only works if you don't use the WSE types Pinmemberstephg1237:54 17 May '08  
GeneralThanks Pinmemberlcsistemas7:24 1 May '08  
GeneralThat is pretty staightforward. PinmemberMember 451773011:00 3 Dec '07  
GeneralIn VS05 WinForm and ClassLibrary projects applicationSettings section is used PinmemberMichael Freidgeim18:09 7 May '06  
GeneralA better way PinmemberBen Powell5:54 6 Dec '05  
GeneralRe: A better way Pinmemberbass4g0d1:23 23 Jan '06  
GeneralRe: A better way Pinmembermikker_1230:51 3 Apr '06  
GeneralIt works, sort of.... PinmemberZod The Mighty5:20 1 Dec '05  
GeneralRe: It works, sort of.... Pinmemberchris lasater6:27 1 Dec '05  
GeneralRe: It works, sort of.... PinmemberZod The Mighty7:54 1 Dec '05  
GeneralIt works but ... PinmemberDoubin23:13 30 Nov '05  
GeneralRe: It works but ... Pinmemberlunky2:33 1 Dec '05  
GeneralRe: It works but ... Pinmemberceddie16:56 8 Dec '05  
GeneralRe: It works but ... Pinmemberlunky4:29 9 Dec '05  
GeneralRe: It works but ... Pinmemberceddie5:53 9 Dec '05  
GeneralRe: It works but ... Pinmemberfloverso12:18 1 Dec '06  
GeneralRe: It works but ... Pinmemberfloverso12:19 1 Dec '06  
GeneralRe: It works but ... Pinmembercyberpunk_np19:42 7 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 30 Nov 2005
Editor: Smitha Vijayan
Copyright 2005 by Christopher G. Lasater
Everything else Copyright © CodeProject, 1999-2010
Web22 | Advertise on the Code Project