meljun cortes vb.net handout understanding web service
TRANSCRIPT
Visual Basic .NET Programming
Understanding Web Service * Property of STI Page 1 of 8
TOPIC TITLE: Understanding Web Service Specific Objectives: At the end of the topic session, the students are expected to: Cognitive:
1. Explain what web services are. 2. Explain the Web service module. 3. Define Dynamic Discovery Document, HTML Description Page
and WSDL. 4. Explain how to create a Web service.
Affective:
1. Listen to others with respect. 2. Participate in class discussions actively.
MATERIALS/EQUIPMENT:
o topic slides o OHP
TOPIC PREPARATION:
o Have the students research about Web Service. o It is imperative for the instructor to incorporate various kinds of
teaching strategies while discussing the suggested topics. The instructor may use the suggested learning activities below to facilitate a thorough and creative discussion of the topic.
o Prepare the slides to be presented in the class.
TOPIC PRESENTATION: The topic will revolve around Web Services. This will be the suggested flow of discussion for the course topic:
1. Ask the students to tell something about web services. 2. Explain what web services are. 3. Explain the Web service module. 4. Discuss how to create a function that can be accessed through
a Web Service by providing sample codes. 5. Explain the Dynamic Discovery Document, HTML Description
Page and WSDL document. 6. Discuss how to invoke a Web Service from a browser and a
client. Give examples. 7. Explain how to create a Web service through a demonstration. 8. Conduct Quiz #7
Visual Basic .NET Programming
Understanding Web Service * Property of STI Page 2 of 8
What is Web Service Page 1 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STIPage 1 of 14
What is a What is a What is a What is a Web Service?Web Service?Web Service?Web Service?
� allows access to components by means of Internet and HTTP
� a class that is stored on one computer
that can be accessed on another computer
over the network
� supporting frameworks for the Web services:
� Open Internet Protocols
� XML Messages and SOAP
� Messages defined by Web Services
Description Language (WSDL)
What is a Web Service? A Web service is a program that allows access to components by means of Internet and HTTP. It is a class that is stored on one computer that can be accessed on another computer over the network. Prior to Web services, internal applications can only communicate internally through local are networks. Listed below are the supporting frameworks for the Web services.
• Open Internet Protocols
• XML Messages and SOAP
• Messages defined by Web Services Description Language (WSDL)
Open Internet protocols such as XML, XML Schema Definition (XSD), HTTP and Simple Mail Transfer Protocol (SMTP) are used to transmit messages over the network. SOAP stands for Simple Object Access Protocol and is the industry standard for using XML to represent data and commands in an extensible way. To accurately describe the Web service’s methods and their arguments, it must provide a contract that client applications can rely upon. Web service contract defines the purpose of the service and its operation. The contract also describes how the service can be accessed and located. WSDL is an XML-based way of providing such contract. It describes the contract information that can be read by all client applications that can work with XML. [What is a Web Service?, Page 1 of 14]
Web Service Module Page 2 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STIPage 2 of 14
Web Service Web Service Web Service Web Service ModuleModuleModuleModule
� used to group the files needed to create a Web service
� contains two files:
� .asmx
• includes the WebService directive
� .asmx.vb file
• contains the code that executes when a
Web Service method is invoked
<%@ WebService Language=”vb”
CodeBehind=”AdminUsr.asmx.vb”
Class=”WebApp.AdminUsr” %>
Web Service Module In Visual Studio 2005, Web Service module is used to group the files needed to create a Web service. It contains two files namely the .asmx
and the .asmx.vb file. The .asmx file includes the WebService
directive that specifies the language used, the name of the code file and the name of the class used in referencing the service. The following example declares a service named AdminUsr written in
VB.Net, using the Admin.asmx.vb module for the code and in the
WebApp namespace.
<%@ WebService Language=”vb”
CodeBehind=”AdminUsr.asmx.vb”
Class=”WebApp.AdminUsr” %>
On the other hand, the .asmx.vb file contains the code that executes
when a Web Service method is invoked. The code inherits from the System.Web.Services.WebServices class and provides direct
access to common ASP.Net objects such as Session, Server,
Application and User.
[Web Service Module, Page 2 of 14]
Visual Basic .NET Programming
Understanding Web Service * Property of STI Page 3 of 8
Adding Public Subroutines or Functions Page 3 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STIPage 3 of 14
Adding Public Adding Public Adding Public Adding Public Subroutines or FunctionsSubroutines or FunctionsSubroutines or FunctionsSubroutines or Functions
� Example:
<WebMethod()> Public Function
AddAdminUsr(ByVal strName As
String) As String
‘ Functionality to add a user and
return new ID
…
Return strNewId
End Function
<WebMethod()> Public Sub
DeleteAdminUsr(ByVal strId As
String)
‘ Functionality to delete a user based
on ID
…
End Sub
Adding Public Subroutines or Functions Web Service methods can be created by using standard method syntax for subroutines or functions. Then, the Web Service method must be
marked as public and use the WebMethod class attribute.
The following example shows how to create a function that can be accessed through a Web Service. <WebMethod()> Public Function AddAdminUsr(ByVal
strName As String) As String
‘ Functionality to add a user and return new ID
…
Return strNewId
End Function
<WebMethod()> Public Sub DeleteAdminUsr(ByVal strId
As String)
‘ Functionality to delete a user based on ID
…
End Sub
In addition, a description can be added to the method by specifying the Description argument of the WebMethod attribute. Example is
shown below. <WebMethod(Description:=”This method performs an
action”)> Public Sub PerformsAction()
…
End Function
[Adding Public Subroutines or Functions, Page 3 of 14]
Dynamic Discovery Document Page 4 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STIPage 4 of 14
Dynamic Dynamic Dynamic Dynamic Discovery DocumentDiscovery DocumentDiscovery DocumentDiscovery Document
� contains information about the Web service
� used to locate and discover Web services
� automatically created by ASP.NET
� return the details of all Web Services
� uses the .vsdisco file extension
� used to exclude particular folders from the dynamic discovery process
Dynamic Discovery Document Dynamic discovery document is a document that contains information about the Web service. It is used to locate and discover Web services. The dynamic discovery document is automatically created by ASP.NET and will return the details of all Web Services contained in the root directory and any sub-directories. It uses the .vsdisco file extension. It is also used to exclude particular folders from the dynamic discovery process. [Discovery Document, Page 4 of 14]
Visual Basic .NET Programming
Understanding Web Service * Property of STI Page 4 of 8
HTML Description Page and WSDL Document Page 5 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STIPage 5 of 14
HTML Description Page HTML Description Page HTML Description Page HTML Description Page and WSDL Documentand WSDL Documentand WSDL Documentand WSDL Document
� The HTML description page
� describes Web services methods and
arguments
� displayed when Web service URL is entered
without specifying a particular method
name
� contains simple test utility methods
� provides test facilities for each methods of
the Web service
� allows you to provide parameter through
an input field
� follows the format:
http://webservername:port/webservi
ce.asmx.
� Example:
http://localhost:3163/MyService.as
mx
HTML Description Page and WSDL Document Page 6 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STIPage 6 of 14
HTML Description Page HTML Description Page HTML Description Page HTML Description Page and WSDL Documentand WSDL Documentand WSDL Documentand WSDL Document
� Web Services Description Language (WSDL)
� describes methods, arguments and
responses of a Web service
� created automatically by adding the ?WSDL
switch to the Web service URL
� Example:
http://localhost:3163/MyService.as
mx?WSDL
HTML Description Page and WSDL Document The HTML description page describes Web services methods and arguments. It is displayed when Web service URL is entered without specifying a particular method name. The URL follows the format http://webservername:port/webservice.asmx. For example,
to display the HTML description page of a Web service named MyService, the URL is
http://localhost:3163/MyService.asmx
The HTML description page contains simple test utility methods. It provides test facilities for each methods of the Web service. It allows you to provide parameter through an input field. Upon entering the values and clicking the Invoke button, the Web service executes and displays the result in XML format. It also displays additional descriptions about each method if the Description parameter for the WebMethod
attribute is specified. In Visual Studio 2005, another way to display the HTML description page is by right-clicking the .asmx file in Solution
Explorer and then clicking View in Browser. The Web Services Description Language (WSDL) document describes methods, arguments and responses of a Web service. It is created automatically by adding the ?WSDL switch to the Web service URL. For
example, to display the WSDL of the Web service named MyService,
the URL will be:
http://localhost:3163/MyService.asmx?WSDL
[HTML Description Page and WSDL Document, Pages 5-6 of 14]
Invoking a Web Service from a Browser and a Client Page 7 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STIPage 7 of 14
Invoking a Web Service Invoking a Web Service Invoking a Web Service Invoking a Web Service from a Browser from a Browser from a Browser from a Browser
and a Clientand a Clientand a Clientand a Client
� To invoke a Web Service from a browser:
� enter the URL of the service, specify the method name to run as well as parameter
values
� Syntax:
http://webservername/vdir/webservi
cename.asmx/MethodName?parameter
=value
� Examples:
http://www.myserver.com/User/Admin
Usr.asmx/AddAdminUsr?strName=Man
ny
<?xml version=”1.0” ?>
<string
xmlns=http://tempuri.org/>43-
124-21</string>
Invoking a Web Service from a Browser and a Client To invoke a Web Service from a browser, enter the URL of the service, specify the method name to run as well as parameter values. The following syntax is used to invoke a Web Service: http://webservername/vdir/webservicename.asmx/MethodN
ame?parameter=value
The following example shows how to invoke a Web Service. This code
calls the AddAdminUsr method of the AdminUsr.asmx Web Service in
the Users virtual directory on the myserver.com Web server. It also
passes in a parameter value of the admin user name Manny.
http://www.myserver.com/User/AdminUsr.asmx/AddAdminUs
r?strName=Manny
The Web Service will return XML output containing the results of the
Visual Basic .NET Programming
Understanding Web Service * Property of STI Page 5 of 8
Invoking a Web Service from a Browser and a Client Page 8 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STI
Page 8 of 14
Invoking a Web Service Invoking a Web Service Invoking a Web Service Invoking a Web Service from a Browser from a Browser from a Browser from a Browser
and a Clientand a Clientand a Clientand a Client
� HTTP GET method
� used to send the form data to the Web
server
� most commonly used for testing purposes
� SOAP protocol
� used for real application-to-application
communication
� difference between the HTTP-GET and
SOAP
� the way of sending data to the Web server
Invoking a Web Service from a Browser and a Client Page 9 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STI
Page 9 of 14
Invoking a Web Service Invoking a Web Service Invoking a Web Service Invoking a Web Service from a Browser from a Browser from a Browser from a Browser
and a Clientand a Clientand a Clientand a Client
� To invoke a Web service from a client:
� add a Web reference to the Web Service
� enter the URL for the .asmx file
� select the required references
� create the client code for accessing a
component
� Example:
Private Sub btnSubmit_Click(ByVal
sender As System.Object, _
ByVal e As System.EventArgs)
Handles btnSubmit.Click
’Services is the given namespace
Dim usr As New Services.user( )
MessageBox.Show(usr.AddUser(txtNam
e.Text))
End Sub
execution as shown in the following example. The results show a String value of 43-124-21 being returned as a new identity of the added user.
<?xml version=”1.0” ?>
<string xmlns=http://tempuri.org/>43-124-21</string>
This approach uses the HTTP-GET protocol. The HTTP GET method is used to send the form data to the Web server. The form data is appended onto the URL in the Address field. This is most commonly used for testing purposes. For real application-to-application communication, SOAP protocol is used instead. SOAP is a specification for exchanging structured information across networks and computer platforms using XML and HTTP. The main obvious difference between the HTTP-GET and SOAP protocols is the way data is sent to the Web server. In HTTP-GET, data is part of the URL while in SOAP, it uses XML. For this reason, SOAP is better in terms of performance, security and scalability than HTTP-GET. Depending on the client, Web Service can be invoked from a client application either thru a Windows Form or a Web Forms application. If the client is written in Visual Basic .NET, you can use the following process:
1. Add a Web reference to the Web Service. 2. Enter the URL for the .asmx file. 3. Select the required references. 4. Create the client code for accessing a component. Use the
appropriate namespaces. When Web reference is added to the client project, Visual Basic .NET creates a proxy class that hides the complexity of calling a Web Service. This proxy allows the use of early binding when connecting to the service, as if the components were accessible within a local assembly. In the following example, the required Web Service has taken a web reference, and the Web reference has been renamed as the Services
namespace. A command button named btnSubmit and a text box
named txtName have been placed on a Windows Forms form with the
following code for the btnSubmit_Click event handler:
Private Sub btnSubmit_Click(ByVal sender As
System.Object, _
ByVal e As System.EventArgs) Handles btnSubmit.Click
’Services is the given namespace
Dim usr As New Services.user( )
MessageBox.Show(usr.AddUser(txtName.Text))
End Sub
When the preceding code is executed, a new user is created. The txtName text box specifies the name of the new user, and the
AddAdminUsr method creates a message box that displays the new
identity. [Invoking a Web Service from a Browser and a Client, Pages 7-9 of 14]
Visual Basic .NET Programming
Understanding Web Service * Property of STI Page 6 of 8
Demo: Creating a Web Service Page 10 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STI
Page 10 of 14
Demo: Creating a Demo: Creating a Demo: Creating a Demo: Creating a Web ServiceWeb ServiceWeb ServiceWeb Service
� Open Visual Studio 2005.
� File > New Web Site
� select ASP.NET Web Service
� set Location to File System
� set Language to Visual Basic then click OK
� delete the asmx file
� delete the vb file
� right-click on the project name and click
Add New Item
� select Web Service in the Templates list then set the Web service and click Add
� right-click on the App_Code and click Add New Item
� select Class from the Installed Templateslist
� set the class name then click Add
Demo: Creating a Web Service Page 11 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STI
Page 11 of 14
Demo: Creating a Demo: Creating a Demo: Creating a Demo: Creating a Web ServiceWeb ServiceWeb ServiceWeb Service
� open the Code Editor for the .asmx file,
locate the Public Class definition then modify the WebService attribute
<WebService(Namespace:=”http://tem
puri.org/”, _
Description:=” Web service demo
information.”
)> Public Class WebServiceDemo
� delete the code:
<WebMethod()> _
Public Function HelloWorld()
As String
Return "Hello World"
End Function
Demo: Creating a Web Service Page 12 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STI
Page 12 of 14
Demo: Creating a Demo: Creating a Demo: Creating a Demo: Creating a Web ServiceWeb ServiceWeb ServiceWeb Service
� To define the Web Service methods:
� Add WebMethod attribute and Web service
method
<WebMethod(Description:="Retrieves
welcome message.")> _
Public Function GetMessage()
As String
GetMessage = "Learning Web
services is fun!"
End Function
� To test the Web Service:
� right-click on the project name and select
Build Web Site
� right-click the .asmx file, and then click View in Browser
Demo: Creating a Web Service
1. Open Visual Studio 2005. 2. In the menu bar, click File and then click New Web Site. 3. In the New Web Site window, select ASP.NET Web Service. In
the Location information, make sure that the drop-down list is set to File System. Then, set the location of your Web application.
4. For the Language information, select Visual Basic. Click OK. 5. In the Solutions Explorer, delete the asmx file named
Service.asmx. 6. Delete the vb file named Service.vb. 7. Right-click on the project name and click Add New Item. The
Add New Item window is displayed. 8. In the Templates list, select Web Service. In the Name field,
set the Web service name to WebServiceDemo.asmx. Click Add.
9. In the Solutions Explorer, right-click on the App_Code and click Add New Item. The Add New Item window is displayed.
10. In the Installed Templates list, select Class. In the Name field, set the class name to WebServiceDemo.vb. Click Add.
11. Open the Code Editor for WebServiceDemo.asmx, locate the Public Class WebServiceDemo definition, and then modify the WebService attribute to include the following Description parameter value:
Web service demo information.
12. Verify that the WebService attribute now looks as follows: <WebService(Namespace:=”http://tempuri.org/”, _
Description:=” Web service demo information.”
)> Public Class WebServiceDemo
13. Locate and delete the following code. This code is a sample
Web service method that is created automatically.
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
To define the Web Service methods:
14. Add the following WebMethod attribute and Web service
method. This method returns a message.
<WebMethod(Description:="Retrieves welcome
message.")> _
Public Function GetMessage() As String
GetMessage = "Learning Web services is fun!"
End Function
To test the Web Service:
15. Build the project. In Solution Explorer, right-click on the project
name and select Build Web Site. 16. In Solution Explorer, right-click WebServiceDemo.asmx, and
then click View in Browser to display the HTML description page. The page should look similar to the one on the next page.
Visual Basic .NET Programming
Understanding Web Service * Property of STI Page 7 of 8
Demo: Creating a Web Service Page 13 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STI
Page 13 of 14
Demo: Creating a Demo: Creating a Demo: Creating a Demo: Creating a Web ServiceWeb ServiceWeb ServiceWeb Service
� click the Service Description hyperlink
Demo: Creating a Web Service Page 14 of 14
Understanding Web Service
Visual Basic .NET Programming
* Property of STI
Page 14 of 14
Demo: Creating a Demo: Creating a Demo: Creating a Demo: Creating a Web ServiceWeb ServiceWeb ServiceWeb Service
� click the Back button
� click the hyperlink of the method of the Web Service
� click the Invoke button
17. Click the Service Description hyperlink and examine the WSDL
document. The page should look similar to the one below.
18. Click the Back button on the Web toolbar to move back to the HTML description page.
19. Click the hyperlink of the method of the Web Service (GetMessage) to view the method details.
Visual Basic .NET Programming
Understanding Web Service * Property of STI Page 8 of 8
20. Click the Invoke button. The Web Service will return XML output containing the results of the execution as shown below.
NOTE: If you have already a Web Site project, you can add a Web service by right-clicking on the project name in the Solutions Explorer, then clicking Add New Item to display the Add New Item window. Select Web Service in the list of templates.
[Demo: Creating a Web Service, Pages 10-14 of 14]
EVALUATION/GENERALIZATION:
• A Web service is a class that is stored on a remote machine and accessed through a remote procedure call.
• Web services method calls are implemented through SOAP.
• A Web service has two parts namely the .asmx file and the code-behind file.
• The Description property of the WebService attribute uniquely
identifies a Web service.
• A developer specifies a method as a Web service by marking it with a WebMethod attribute.
REFERENCES:
� Microsoft Official Course, (2002), 2373B: Programming with
Microsoft Visual Basic .NET, Microsoft Corporation � Holzner, Steven, (2003), Sams teach yourself Microsoft Visual
Basic.Net 2003 in 21 days, USA, Sams Publishing � Liberty, Jesse, (2003), Learning Visual Basic .NET, USA,
O'Reilly & Associates, Inc