How to create the proxy class with Visual Studio in ASP.NET

You need to create the Proxy class when you want to consume a web service as described in the article How to consume a web service in ASP.NET. The first option is described in the article How to create the proxy class with wsdl command-line tool in ASP.NET. As a second option, you can create the Proxy class by adding a web reference in the client project in Visual Studio 2010. Web references point to a web service with a WSDL contract. In this case you should follow the next steps:

1. Right-click the client project in the Solution Explorer, and select Add Service Reference. Then press buttons Advanced and Add Web Reference.

 

Add Service Refence dialog box

Add Service Refence dialog box

Service Reference Settings dialog box

Service Reference Settings dialog box

 

2. The Add Web Reference dialog box opens, as shown next. This window provides options for searching web registries or entering a URL directly. It also has a link that allows you to browse all the web services on the local computer or search a UDDI registry.

 

The Add Web Reference dialog box

The Add Web Reference dialog box

3. You can browse directly to your web service by entering a URL which points to the .asmx file. The test page appears in the window as shown next, and the Add Reference button is enabled.

 

Adding a web reference

Adding a web reference

4. In the Web Reference Name text box you can change the namespace in which the proxy class is generated.

5. To add the reference to this web service, click Add Reference at the bottom of the window.

6. The web reference appears in the Web References group for your project in the Solution Explorer window.

Important notes

1. You need to update your proxy class by right clicking the web reference and choosing Update Web reference if web service us changed. Web references aren’t updated automatically when you recompile the application.

2. When you are developing and testing a web service in Visual Studio, it’s often easiest to add both the web service and the client application to the same solution. This allows you to test and change both pieces at the same time. To choose which application you want Visual Studio to launch when you click Start, right-click the appropriate project name in the Solution Explorer and select Set As StartUp Project.

 

When you add a web reference, Visual Studio saves a copy of the WSDL document in your project:

1. In a web application, Visual Studio creates the App_WebReferences folder (if it doesn’t already exist) and then creates a folder inside it with the web reference name (which you chose in the Add Web Reference dialog box). Finally, Visual Studio places the web service files in that folder. However, Visual Studio doesn’t generate the proxy class. Instead, it’s built and cached as part of the ASP.NET compilation process. This is a change in the behavior from ASP.NET 1.x.

2. In any other type of application, Visual Studio creates a WebReferences folder and then creates a folder inside it with the web reference name. Inside that folder it places all the support files you see in a web application (most important is a copy of the WSDL document). It also creates a file named Reference.cs (assuming it’s a C# application) with the proxy class source code. By default, this class is hidden from view. To see it, select Project ➤Show All Files.