Summary: How to access Web services from Visual Basic 6.0 and Visual Basic .NET, and how to convert existing SOAP applications to the .NET platform.
the goal
Learn about SOAP Tools in Microsoft® Visual Studio® .NET Reviews Use SOAP and Visual Basic® 6.0 Access COM / COM Components to access Web services from Visual Basic .NET to WEB services
Precondition
Understand what is web services and access to Visual Basic 6.0 and SOAP, and know how to create an ActiveX® DLL project is familiar with the basics of Visual Studio .NET.
Web services, SOAP and your tools
The Web service provides features that can be accessed through the Internet, that is, call the web service and get the result of returning. The new feature of the web service is that you can now connect to the world's largest network Internet Internet. However, as a Visual Basic programmer, the Web service has handled a cumbersome Internet transaction.
In this article, you'll learn how to use Visual Studio .NET to access Web services, review the process of creating a web service using Visual Basic 6.0 and SOAP Toolkit 2.0, and will also learn how to convert existing SOAP Toolkit applications for the .NET framework Use web services.
The web service used in this article has a very simple function CreateCustomer. It accepts the name of the customer and returns the client identifier. However, we are not here to discuss the functionality of the web service so that you can concentrate on how to create and access Web services using Visual Basic 6.0 or Visual Basic .NET.
Visual Basic and SOAP
The user communicates with the Web service by sending and receiving the SOAP document. In Visual Basic 6.0, there are two ways to create a SOAP document:
Use the SOAPCLIENT in SOAP Toolkit 2.0 by connecting string combined documents
SOAPCLIENT not only creates a SOAP document with Web service, but also sends this document to the web service for processing.
In the transmitted web server, a SOAP listener is required to capture and process the document sent by the client. SOAP Toolkit 2.0 includes SOAPSERVER that can be used to use it in the listening application to process SOAP documents. The listener is an application created separately, which is independent of components that implements a web service.
Using Visual Studio .NET, you can simplify the creation and access to the Web service. In Visual Studio .NET, creating a Web service does not require a separate listening application. When accessing a web service, it automatically creates a proxy so that you are using a web service as accessible to any other object.
Web Service Description Tool
Web Services Description Language (WSDL) is an XML language that is designed to describe Web services. As part of the process of making web services into an open standard, WSDL has been submitted to W3C along with SOAP. The WSDL description of the service can contain WSDLSPEC TMODEL, which provides all the information required to create and send SOAP documents for the service. The following is an example of a WSDLSPEC TMODEL file that supports the CreateCustomer function:
XML Version = '1.0' encoding = 'UTF-8'?>
File Generator, Version 1.00.623.1 ->
Targetnamespace = 'http://tempuri.org/wsdl/'
XMLns: wsdlns = 'http: //tempuri.org/wsdl/'
XMLns: typens = 'http://tempuri.org/type'
XMLns: soap = 'http://schemas.xmlsoap.org/wsdl/soap/'
XMLns: xsd = 'http://www.w3.org/2001/xmlschema'
XMLns: STK =
'http://schemas.microsoft.com/soap-toolkit/wsdl-extension'
XMLns = 'http://schemas.xmlsoap.org/wsdl/'>
XMLns = 'http://www.w3.org/2001/xmlschema'
XMLns: SOAP-ENC =
'http://schemas.xmlsoap.org/soap/ENCoding/'
XMLns: wsdl = 'http: //schemas.xmlsoap.org/wsdl/'
ElementFormDefault = 'Qualified'>
schema>
type>
message>
message>
ParameterOrder = 'First Last'>
'WSDLNS: CustServices.createCustomer' />
Message = 'WSDLNS: CustServices.createCustomerResponse' />
Operation>
porttype>
Transport = 'http://schemas.xmlsoap.org/soap/http' />
'http://tempuri.org/Action/custservices.add' />
Namespace = 'http://tempuri.org/message/'
eNCodingStyle =
'http://schemas.xmlsoap.org/soap/encoding/' />
input>
Namespace = 'http://tempuri.org/message/'
eNCodingStyle =
'http://schemas.xmlsoap.org/soap/encoding/' />
OUTPUT>
Operation>
binding>
Binding = 'WSDLNS: CustServiceSsoapBinding'>
Location = 'http://localhost/custom/cust.asp' />
port>
service>
definitions>
The tag at the end of WSDLSpec TMODEL specifies which SOAP listener to send requests, in this case, will be sent to http: //localhost/cust/cust.asp page. The previous section of this file describes the message that the service can use.
Note: Namespaces used in SOAP documents
Tempuri.org is a temporary namespace that is used only when developing (
Temporary
URI). Before this service is put into the product, you should modify the WSDL file so that it has a unique URI (for example, the URL of your company website).
Both the SOAP Toolkit object and Visual Studio .NET IDE read the WSDL file. SOAPCLIENT uses information in the WSDL file to make it a proxy for web services. Visual Studio .NET uses the same information to generate a proxy class. So you can do the following:
The way to call the web service seems to be the method of these agents. Before sending messages, verify calls according to the WSDL description of the web service. Interact with proxy without having to be familiar with the content of the WSDL file or the SOAP document to be generated.
Figure 1 shows the relationship between each component, including a Web Service Molin Language (WSML) file. This file is only required for Visual Basic 6.0 and describes this file in its exercise. Figure 1: Web service and client schematic
In SOAP, SOAP Toolkit and Visual Studio .NET IDE perform similar features. Different, the SOAP Toolkit object cannot provide all support required for Visual Basic developers. In Figure 2, you can see IntelliSense® support (Visual Basic programmer thinks such support is necessary). The IntelliSense drop-down list automatically displays the CreateCustomer method for the CustomerServices COM object.
Figure 2: IntelliSense support for COM / DCOM / COM object
In Figure 3, you can see the IntelliSense support provided by SOAPCLIENT for Web services. All SOAPCLIENT methods are listed, but no way to use SOAPCLIT as a proxy service (ie CreateCustomer).
Figure 3: IntelliSense support using SOAPCLIENT
If you use Visual Studio .NET, the proxy class generated by the IDE can better represent web services than SOAPCLIENT. In Figure 4, you can see IntelliSense support for accessing the web service. The CreateCustomer method is one of the methods owned by the object interaction with the web service.
Figure 4: IntelliSense support for web services in Visual Studio .NET
SOAP Toolkit 2.0 Overview
This section briefly reviews the process of creating a web service in the previous section of Web services, SOAP, and your tools.
Create and access Web services using SOAP Toolkit
In Visual Basic 6.0, create a new Visual Basic ActiveX DLL project called CustCP for Web Services and class modules called CustomerServices and their code: Public Function CreateCustom (Byval First As String, _
BYVAL LAST AS STRING AS STRING
CreateCustomer = "a" & cstr (len (first)) & _
CSTR (Len (Last))
END FUNCTION
Encap the WSDL and WSML files using the SOAP Toolkit WSDL (WSML file containing SOAPSERVER for information to map from the web service to the ActiveX COM object). Create a SOAP listener, as an ASP page using SOAPSERVER: <% @ language = VBScript%>
<% OPTION Explicit
DIM SS
DIM WSDL
DIM WSML
Response.contentType = "text / xml"
WSDL = Server.mappath ("Cust.wsdl")
WSML = Server.mAppath ("Cust.wsml")
Set ss = server.createObject ("mssoap.soapserver)
Ss.init WSDL, WSML
ss.soapinvoke request, response, ""%> Create a client to call us using SOAPCLIENT Web Services: Private Sub Cmdcreate_Click ()
DIM SC As Mssoaplib.soapClient
SET SC = New SOAPCLIENT
Sc.MSSOAPINIT "http://vogel2/cust/cust.wsdl"
Me.txtCustId.text = _
Sc.createCustomer (Me.txTfirstname.text, _
Me.txtlastname.text)
End Sub
Visual Basic .NET drill
In this section, you will learn how to do the following:
Create a web service in Visual Studio .NET. Access the Web service from the Visual Basic .NET application.
When generating a web service in Visual Studio .NET, a "discovery file" will be automatically generated for the web service. Discover files are used to add web services to other Visual Studio .NET applications, including WSDL description.
Note: For Visual Basic and C # projects, the file type of the file is found is
.vsdisco; for the C project,
.
Create a web service in Visual Studio .NET
On the FILE (File) menu, select New to display the New Project dialog as shown in Figure 5. Figure 5: Visual Studio .NET New Project (New Project) dialog box in the Visual Basic Projects section, select the ASP.NET Web service. In the Name box, give the project name (MigrateToDotNetService). Enter the Web site URL (LocalHost / Customer) for the web service. Click OK to create a project. By default, the project contains modules called Service1.asmx. Right-click Service1.asmx module and select View Code from the menu. Add this code to the public class service1 statement at the top of the module, as shown below:
Description: = "Customer Management")
Public Class Service1
Copy the CreateCustomer function from the Visual Basic 6.0 sample, but add the property to the function declaration, as follows:
"Pass your name, return id value")> _
Public Function CreateCustomer (_
BYVAL First As String, _
BYVAL LAST AS STRING AS STRING
Only by changing one, you can convert the Visual Basic 6.0 code to a Visual Basic .NET syntax. Use CreateCustomer = in the RETURN replacement function as follows: Return "a" & cstr (len (limited) & _CSTR (Len (Last))
END FUNCTION
On the Build (Generated) menu, select Build to create a web service. Check the errors during the generated process in the Output window. As part of the generation, the discovery file of the application is generated.
Create a client
You can access the .NET web service without creating a .NET client. Visual Basic 6.0 applications created with SOAP Toolkit. The following steps will show you how to create a .NET client for the web service (with the web service is not related to Visual Basic 6.0 or by Visual Basic .NET):
On the FILE (File) menu, select New, then select Project to display the Add Project dialog. In the Visual Basic Projects section, select Windows Application (Windows Applications). Give the project name (MigrateToWebClient) in the Name box, then click OK to create a project. In the Solution Explorer (Solution Explorer), right-click this item and select Add Web Reference. The Add Web Reference dialog is displayed, as shown in Figure 6. Figure 6: Add Web Reference dialog box Enter the URL of the .vsdisco file of the web service item in the Address box. For example, http://localserver/customers/migratetonetService.vsdisco. Click the arrow at the address box to select the code.
Note:
.vsdisco file Drag from the Solution Explorer to the code module, you can also get the web service item
The URL of the .vsdisco file. This will add the URL of the file to the code module. Then, cut the URL from the module and paste it into
Add Web Reference dialog
ADRESS box.
Click the Add Reference button to return the project. The reference to the web service will be displayed in the Solution Explorer, as shown in Figure 7. Figure 7: Visual Basic Windows application containing a web service adds a text box named txtfirstname and txtlastName to the form, and adds a button to the customer's name and add a button to the code that calls the Web service. Add code to declare the variable of the reference agent class, this agent class handles the web service (the default name of the proxy class consists of server name and service name): private subdcreate_click () DIM CC as localhost.service1
Add code, instantiate the proxy class and call the web service: SET CC = New localhost.service1
Me.txtCustId.text = _
cc.createcustomer (Me.txtFirstname.Text, _
Me.txtlastname.text)
End Sub
Press F5 to run the client and access the Web service.
Switch from SOAP Toolkit to Visual Studio .NET
The application generated using the SOAP Toolkit or .NET Web service complies with the SOAP specification. When using SOAP-based applications, the connections between clients and web services are defined by the WSDL file. Web services generated using .NET can use the web services generated by SOAP Toolkit, and vice versa. This allows you to move from SOAP Toolkit to .NET by storing WSDL definitions of service constants. For example, while the web service is converted to .NET, the client accessing the Web service can continue to use the SOAP Toolkit's SOAPClient.
The easiest way to store WSDL files for web service constants is to copy the Visual Basic 6.0 code to the ASMX module of the web service. After the copy is complete, you must add the and properties to the class module to create .NET Web services. Then, you need to convert the Visual Basic 6.0 code to .NET syntax, such as MSDN and other articles of this series. If you want to create a new .NET version of Web service, you can use the SOAP Toolkit version of the WSDL file to generate the basic code of the web service.
At the client, different client applications accessing the web service can be converted from SOAP Toolkit to .NET, respectively. Due to the interoperability provided by the SOAP specification, even if a client generates multiple .NET versions, there is no problem. To convert from SOAP Toolkit to Visual Studio .NET within the client, you must replace reference to SOAPCLIENT with reference to the web service agent.
summary
You have already understood how to access Web services from Visual Basic 6.0 and Visual Basic .NET. It also understands the operation of converting existing SOAP applications to the .NET platform.
The following three points should be remembered: