Modifying the Web References for Visual Studio .NET






3.42/5 (12 votes)
Jun 26, 2003
3 min read

74463

559
Modifying the Web References for Visual Studio .NET.
Introduction
In this article, we will discuss a common problem with Visual Studio .NET Web Service development. Suppose you are building a project using Visual Studio .NET, and you decide that you want to start consuming Web Services. The first step that you will generally take is, you will add a Web reference to that particular Web Service which is residing on a certain server, say the localhost server for this example. Visual Studio .NET will then add a new item under Solution Explorer called Web References, and it will create a folder node underneath it called localhost. The localhost node will contain the WSDL and the proxy class files for the Web Service that was referenced. Later on, you find that you need to add another Web reference to a Web Service that is residing on the same server as the first Web Service. Visual Studio .NET will then create another folder node, and this time it will name this node localhost1. The localhost1 node is identical to the localhost node, except that it references the second Web Service that was added to the Visual Studio .NET project. Now, if you were to add a few more Web Services that were all located on that localhost server, you would end up with a couple of folder nodes with the name "localhost" appended with numeral values, like "localhost1", "localhost2", "localhost3", and so on.
When Add Web reference adds a new Web Service, it places the proxy in a separate namespace. So, when you create instances of your proxy classes, your code may look something similar to this...
Dim srv1 As New localhost.ClassNameA()
Dim srv2 As New localhost1.ClassNameB()
Dim srv3 As New localhost2.ClassNameC()
As you can see, the above code doesn't look very neat with the different namespaces. Instead, you may want your proxy classes to be all under one namespace like the following...
Dim srv1 As New localhost.ClassNameA()
Dim srv2 As New localhost.ClassNameB()
Dim srv3 As New localhost.ClassNameC()
To achieve this, you need to modify a few things. Each folder node under the Solution Explorer/Web References section has a file node called Reference.map. This file contains an XML pointer to the WSDL file. When you expand the Reference.map file node, you will see a file called Reference.vb sitting right underneath it (if you are using C#, you will see a file called Reference.cs). The Reference.vb file is the proxy class file for the Web Service that the node is referencing to. You can manually edit the name of the namespace of the proxy class to a desired name. For example, if the proxy class is under a namespace called localhost2
, you can change the name of the namespace to localhost
. Alternatively, the Reference.map file node has a property called "Custom Tool Namespace". Setting a value to this property will automatically update the namespace of the proxy class. Another thing that you may want to do is group all the proxy class files under one common folder node. To do this, you will need to rename each of the Reference.map files to a unique name with .map extension. This will automatically rename the name of the proxy class file. Then drag the renamed .map file node to one particular folder node under the Solution Explorer/Web References section. Finally, delete the folder node where the renamed .map file previously existed in, because it is no more required. I have created a video demonstrating how this can be done, which can be downloaded from download area.
Related Article
- Sharing Types by Scott Seely (Microsoft Corporation)