Enterprise Software

Create a proxy to consume .NET Web services

Learn how to create a proxy in .NET for consuming the .NET Web service. Phillip Perkins builds and registers the proxy component for use in VB6, as well as presents sample code for using the proxy within VB6.

In my previous article, I introduced the plan to use Visual Basic 6.0 (VB6) to consume a .NET Web service. I also presented sample code for the Web service and discussed how to get this service operational. Now I'll explain how to create a proxy in .NET for consuming the .NET Web service. I will also build and register the proxy component for use in VB6. Finally, I'll present sample code for using the proxy within VB6.

Creation of the Web service

Since .NET has the internal workings for hosting and consuming Web services, I used .NET to create the Web service. I relied on ASP.NET Web Matrix's Web Service Proxy Generator to create the code (which was created to consume the service in .NET). From the Tools menu option, choose the Web Service Proxy Generator. The information necessary to complete this step consists of the WSDL URL, the namespace of the proxy, the output directory, and the source file name. The WSDL URL is nothing more than the URL of your .asmx Web service file. You can choose any namespace you wish, but it's a good idea to be consistent. For instance, my namespace is set to MyWebServiceProxy; I used UserServiceProxy.cs as the source file name. Considering that I was going to create a .NET project to add this file to, I entered the same output directory where I was going to make my project. (Note: Do not generate the assembly.)

Let the project begin

Now it's time to create a new project in Visual Studio .NET. This project Output Type should be a Class Library project. I named my project MyWebServiceProxy. Inside this project, I added the UserServiceProxy.cs source file. I also changed a few of the visibility statements in this class. The resulting code should look similar to Listing A.

Now, follow these steps:

  1. Right-click the project name in the Project Explorer and choose Properties.
  2. In Configuration Properties | Build, set the Register For COM Interop option to True.
  3. To build the project, create a new Standard EXE project in VB6.
  4. Add a reference to the MyWebServiceProxy type library.
  5. On the default form, add a CommandButton control.
  6. In the CommandButton control's Click event, add the following code:
    Dim o As MyWebServiceProxy.UserService
    Dim usr As MyWebServiceProxy.User
    Set o = New MyWebServiceProxy.UserService
    Set usr = o.GetUser(1)
    MsgBoxusr.FirstName & " " & usr.LastName

IntelliSense gives you the public classes of the MyWebServiceProxy library; however, you may not get the members (i.e., GetUser() method, etc.) available to the classes. This doesn't mean that you can't use them—it just means that you'll either have to rely on reference or memory. You can also fix this by implementing an interface. (This may be a smarter approach, but it isn't necessary for the purposes of this article.)

Run the code and see what you get. If everything is set up correctly, you should get a message box with the user's first and last name where the user id is 1.

Overcoming security hurdles

The biggest hurdle you may run into is security. If you get an "access denied" error, then you have a couple of options. The first is to impersonate the anonymous account from IIS. In your web.config file for your Web service, make sure to add the Identity node and set the Impersonate attribute to True. In Internet Services Manager, change the File Security on your .asmx file. Set the anonymous account name to a system or domain user account that has sufficient privileges to access the resources needed by the Web service. If all else fails, then use an administrator account for this exercise. Try the code again and see what you get.

Another option is to set the userName and password attributes in the Identity node in the web.config file. Once again, set this to an appropriate account. Also, don't forget to include the impersonate attribute.

If you'd like the source code for these examples, you can download them from this zip file.

Keep your developer skills sharp by automatically signing up for TechRepublic's free Web Development Zone newsletter, delivered each Tuesday.

Editor's Picks

Free Newsletters, In your Inbox