Consuming .NET Web services with COM and VB6

See how Phillip Perkins creates a simple .NET Web service component, as well as a client proxy for the service, and uses that proxy within VB6 without having to utilize the entire SOAP toolkit.

Microsoft's .NET strategy does not leave Visual Basic 6.0 programmers completely in the dark. One of the benefits of .NET is the ease at which you can create Web services. Another benefit is its interoperability with Component Object Model (COM), the basis for VB6 component creation and ActiveX.

VB6 programmers can find comfort in knowing that .NET components can be registered as COM components, allowing VB6 programmers to create a reference to the registered component and use that component within their own projects. This can be a very powerful way to utilize .NET Web service components within VB6 projects.

In this article, I will create a simple .NET Web service component, as well as a client proxy for the service, and use that proxy within VB6 without having to utilize the entire SOAP toolkit.

Creating Web services in .NET is simple, because the foundation for HTTP transport through SOAP is built into the .NET Framework. This is, for the most part, transparent to the .NET programmer. The .NET programmer creates classes with public methods just like he/she would with any other development environment. However, when he/she compiles the project, the WSDL and supporting Web service functionality are created also. This is a major benefit to creating applications in .NET.

Most likely, consumption of .NET Web services is done through a consumer proxy. If you have the ASP.NET Web Matrix IDE on your PC, it contains a tool for automatically creating these proxies. Otherwise, you can create the proxy using command line tools available with the .NET Framework. Once you've created these proxies, which include the source code, you can tweak the code so that you can consume the proxy's functionality as a registered COM component in VB6.

Creating a solution

The first thing to do is create a .NET Web service that provides simple functionality but is representative of a useful business function that you would want to create as a Web service. Let's create a service for grabbing information from a database and supplying that data to the client. This is what the great majority of all business functions do. The proxy will consume the Web service and will be registered as a COM component. Finally, a standard EXE will consume the proxy COM and display the results in textboxes on a form.

The most common data you can find in most solutions is user information. So in our .NET Web service, we'll create a method that accepts an integer value, the user ID as the parameter and returns a User type. The User type contains members such as FirstName, LastName, etc.

First, create a .NET Web service application in the Visual Studio .NET IDE, or you can create this solution in the ASP.NET Web Matrix IDE mentioned above. Add the code in Listing A to the newly created project.

From the preceding code, you can see where we create the connection to the database (in this case, SQL Server), grab the user information based on the user ID passed in to our method, and create and return the User type.

Compile this code, and test the functionality by navigating your browser to the URL where your Web service is located, such as http://localhost/MyWebService/UserService.asmx. You should get a page that lists the method of your service. Click on this method and you should get a textbox to enter a user ID. Type in a valid user ID, click on the Invoke button, and check the return information in the next page. You should get XML back that looks something like Listing B.

If you get this, then we're on the right track. If not, normal errors include security errors such as access to the database. In your web.config file, make sure the authentication type is set to "Windows", and add identity impersonation with the following code:

<authentication type="Windows" />
<identity impersonate="true" />

At this point, you may have to add IUSR_[computer name] access to the database. You can add this user to the public role and grant SELECT rights on your user information table to public. However, you should turn off anonymous access to this page through Internet Services Manager.

In the next article, I'll create the Web service proxy using both the command line tools and the Web Matrix IDE. I'll tweak the code so that it can be used as a COM component and rebuild the DLL. Then I'll create a standard VB6 EXE that will reference the proxy COM.

If you're new to Web services in .NET, the ASP.NET Web site provides simple, yet effective, information for creating Web services in .NET.

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