How to make your Web Reference proxy URL dynamic

This is a quick helper to allow you to make the web reference URLs for your WSDL proxies easier to configure when switching environments.

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':

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:

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):

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

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:

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:

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

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.

Comments and Discussions

Yeah? This is a fact though. You do have to reconfigure the config file url based on the url the web service was originally compiled though when you change code. However you can beat this by writing some ant scripts to handle your changes and adding the script call as a VS add in (via an exe to call the script). This article is mainly to solve the problem that has the URL embedded in compiled code and you can't change environments (Test/QA/Staging) or servers without a recompile. If you change something, like in any other code base, you are going to have to fix stuff, unless you have it set up differently.

I agree, I was just hoping that VS would be smart enough to realize if it were updating the web service from the same source _and_ I had overriden the URL it would keep the changes... As it is, I have to remember to fix them before I give the app out for testing (Not a big deal, and I will eventually make a NAnt build script to deal with it, but it would have been nice...)

If it fine until someone refreshes the web reference and reference file is recreated.

You could set the URL of the service when you declare the webservice using a method. That way refreshing the reference won't make a difference. (There is a reason why you can't see the reference file by default)

Why not create a proxy from the web service proxy . That way you dont have a hard reference in your project to the service and it put the url property in the class. Thus making your assembly truely dynamic.

When you add a web service in Visual Studio it uses the proxy tool to create the proxy and place it in your project. Sometimes you cannot add a webservice directly into Visual Studio. Using the wsdl tool directly gives you more control of the proxy class. I wasnt saying in addition too. I was saying instead of.

=========================================================================================================================="UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity." -Dennis Ritchie