- Introduction
If you are familiar with the use of COM objects, writing Web services using Microsoft SOAP Toolkit can be a very easy task.
Microsoft recently announced the Beta 2 release of their SOAP Toolkit version 2.0. Good news is that this release is a fully Microsoft supported product.
Click here to download Microsoft SOAP Toolkit 2.0 Beta 2 and in this article I'll show you how easy it is to work with Web services!
This article assumes that you are familier with SOAP, concept of Web services and you have Microsoft SOAP toolkit installed to build and test following "Hello World" Web service. Check perfectxml.com SOAP Focus section to learn more about SOAP.
- Writing the Server Component
Step 1.1 Create an ActiveX DLL Project named "HelloServer" in Visual Basic 6.0 and then add a class with the name "Hello"
Step 1.2 Add following code to the class:
Option Explicit
Public Function getHello() As String
getHello = "Hello World from Microsoft SOAP Toolkit"
End Function
|
Step 1.3 Build the DLL
Step 1.4 Let's quickly test our DLL with the help of following ASP code: (an optional step)
<%
set hello = server.createobject("HelloServer.Hello")
response.write hello.getHello()
%>
|
Create an ASP page, add above lines and try to load the ASP page from the browser. If you see the response as "Hello World from Microsoft SOAP Toolkit", then you can be sure that DLL is working as expected.
Step 1.5 Now we need an ASP file that handles the requests send to our web service. To make things simple, we will not write this ASP file from scratch, but use the one that is already available in the samples shipped with the toolkit. Make a copy of file CalcVB.asp (Samples/Web subdirectory) to Hello.asp.
The best thing is that we need to change just four lines in this file to make it a part of our Hello World service.
The only changes made are highlighted.
<%
Dim SoapServer
Dim WSDLFilePath
Dim WSMLFilePath
On Error Resume Next
Response.ContentType = "text/xml"
Set SoapServer = Application("HelloServer")
If SoapServer Is Nothing Then
Set SoapServer = Server.CreateObject("MSSOAP.SoapServer")
If Err Then SendFault "Cannot create,, SoapServer object. " & Err.Description
WSDLFilePath = Server.MapPath("Hello.wsdl")
WSMLFilePath = Server.MapPath("Hello.wsml")
SoapServer.Init WSDLFilePath, WSMLFilePath
If Err Then SendFault "SoapServer.Init failed. " & Err.Description
Set Application("HelloServer") = SoapServer
End If
SoapServer.SoapInvoke Request, Response
If Err Then SendFault "SoapServer.SoapInvoke failed. " & Err.Description
Sub SendFault(ByVal LogMessage)
Dim Serializer
On Error Resume Next
Response.AppendToLog " SOAP ERROR: " & LogMessage
Set Serializer = Server.CreateObject("MSSOAP.SoapSerializer")
If Err Then
Response.AppendToLog "Could not create SoapSerializer object. " & Err.Description
Response.Status = "500 Internal Server Error"
Else
Serializer.Init Response
If Err Then
Response.AppendToLog "SoapSerializer.Init failed. " & Err.Description
Response.Status = "500 Internal Server Error"
Else
Serializer.startEnvelope
Serializer.startBody
Serializer.startFault "Server", "The request could not be processed due to a problem in the server. Please contact the system admistrator."
Serializer.endFault
Serializer.endBody
Serializer.endEnvelope
If Err Then
Response.AppendToLog "SoapSerializer failed. " & Err.Description
Response.Status = "500 Internal Server Error"
End If
End If
End If
Response.End
End Sub
%>
|
Step 1.6 Next, we'll have to make one line change in the global.asa file (Samples\Web subdirectory):
<SCRIPT LANGUAGE="VBScript" RUNAT="SERVER">
Option Explicit
Sub Application_onStart()
Set Application("CalcVBServer") = Nothing
Set Application("CmxDomVBServer") = Nothing
Set Application("HelloServer") = Nothing
End Sub
</SCRIPT>
|
Step 1.7 The last step involves creating WSDL and WSML files using WSDLGEN utility (MSSoapSDK\Binaries) provided with the toolkit.
When you run WSDLGEN.exe, in the first text box (labeled DLL or TLB file to read ) enter the name of your DLL
with full path. In the second textbox (labeled URL of the SOAP server) enter "http://localhost/MSSoapSamples/Hello.asp" (without quotes).
Here MSSoapSamples is the virtual directory for Samples/Web directory inside the toolkit.
In the third text box (labeled Output WSDL file) enter "C:\Program Files\MSSoapSDK\Samples\Web\Hello.wsdl".
Now click OK. This will generate the WSDL and WSML files for our Hello World SOAP service. The serevr is ready now, let's now move forward to writing the client for this.
- Writing the Client for above SOAP Web service
Step 2.1 Create a "Standard EXE" Project in Visual Basic 6.0
Step 2.2 Click on Project | References and add reference (check the box) for Microsoft SOAP Type Library (ver 1.0)
Step 2.3 Click on View | Code and add following lines, which essentially declare a global variable and a sub-routine.
Private Client As SoapClient
Private Sub Connect()
Set Client = New SoapClient
Client.mssoapinit "http://localhost/MSSoapSamples/Hello.wsdl", "Hello", "HelloPortType"
End Sub
|
Step 2.4 Open/view the form and add a text box (name it cMsg) and a button (name it doHello). Double click on the button to add the following event handler code:
Private Sub doHello_Click()
On Error GoTo ErrorHandler
Connect
cMsg.Text = CStr(Client.getHello())
Exit Sub
ErrorHandler:
MsgBox Client.faultstring, vbExclamation
End Sub
|
Step 2.5 Run this application, click on the button and see how it calls the Web service method. I'll not go into the code details, to learn more about SOAP Toolkit, visit: http://www.perfectxml.com/soapmstoolkit.asp.
Tip: If SOAP toolkit is not loading your Web service DLL properly, test the DLL first with simple ASP quickly and be sure that IIS is able to load the DLL and after that use the DLL with SOAP.
- Summary
In this short step-by-step tutorial, we learned how to use Microsoft SOAP toolkit and easily build Web services and clients.
If you have any questions or comments, feel free to contact author of this article, Abhishek Srivastava at abhisheks@india.hp.com.
Back to Articles Page
All information on this site is for training only.
We do not warrant its correctness or its fitness to be used.
The risk of using it remains entirely with the user.
|