36
XML Web Services ASP.NET

XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

  • View
    231

  • Download
    0

Embed Size (px)

Citation preview

Page 1: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

XML Web ServicesASP.NET

Page 2: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Overview of Web Services (Page 1)

• Web Service– Part or all of a Web application that is publicly

exposed so that other applications can interact with it. • The code itself is not exposed, the interface is

– Accessible through standard Web protocols– Platform independent

• Remote Procedure Call (RPC) – Used to call another application on the Web server

Page 3: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Overview of Web Services (Page 2)

• Browser– Is a client of the Web application. – Does not know the Web server communicates with a

Web service

• Web Server = Web Service Client– Delivers content to a browser – Does not know that Web Service is communicating

with any back-end applications or data sources

Page 4: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Overview of Web Services

Page 5: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Using Visual Studio .NET to Create and Consume Web Services• Web Services

– The core of the Microsoft .NET vision– Microsoft and IBM worked together to create the XML

Web Services standards

• Web Services can be created using a variety of developer tools and programming languages

• A sample "Business-to-Business" Web Service– Tara Store

• Creates a Web Service which provides product data– MaryKate’s Housewares

• Integrates product data into its own Web site

Page 6: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Building and Consuming a Web Service

Page 7: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Building a Web Service• Web Service file extentions

– .asmx and .asmx.vb for the code behind the page

• Import System.Web.Services namespace• Three major components

1. Web Service on the server

2. Web Service client application that calls the Web Service via a Web reference

3. A WSDL Web Service description document that describes the functionality of the Web Service

Page 8: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Web Services Standards and Protocols• HTTP protocol – can be used to deliver documents

through proxy servers and firewalls– Pass parameters with a QueryString using HTTPGet– HTTPPost passes parameters as a URL-encoded

string, which contains form field names and values attached to the body of the message

– SOAP (Simple Object Access Protocol) is default method by which ASP.NET internally calls a Web Service

Page 9: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Creating a Web Service (Page 1)

• To create the WebService document:1. Select Project from the menu bar

2. Select WebService… from the Project menu

3. Verify that "WebService" is the selected Template, give it a name (i.e. "ServiceTSCategories") and click the <Open> button

• Keyword WebService comes before class header– Statement is part of the class header– Must be qualified if the System.Web.Services

namespace is not imported

Page 10: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Creating a Web Service (Page 2)

• Properties for the keyword WebService:– Namespace property

• Unique URL which distinguishes your Web Service from others (default domain is "tempuri.org")

• Replace with your organization’s domain name

– Description property • Used to provide information about your Web Service• Can include HTML commands

– Name property • Name displayed in the Web Service home page.

Page 11: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Creating a Web Service (Page 3)

• Example:<System.Web.Services.WebService _

(Namespace:="http://tarastore.com/", _

Description:="<H2>Displaying Data from the Tara Store Database</H2>", _

Name:="Chapter 11 Web Service")> _

Public Class ServerVariables

...

End Class

Page 12: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Creating a Web Service (Page 4)

• In Visual Studio .NET compiling the Web Service generates the WSDL document, DISCO document and test Web page

Page 13: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

The Web Method (Page 1)

• Create new function in "Code Behind the Page"– Exposes the method to applications in which the

WebService is instantiated

• Keyword WebMethod comes before class header– Statement is part of the function header– Must be qualified if the System.Web.Services

namespace is not imported (like WebService)

Page 14: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

The Web Method (Page 2)

• Properties in statement prior to method header:– BufferResponse property

• Store the response on the server until the entire function is processed

• If set to True, improves server performance by minimizing communication

– CacheDuration property • Number of seconds to cache Web service results

• Automatically cache the results for each unique parameter

Page 15: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

The Web Method (Page 3)

• Properties before method header (con):– MessageName property

• Name for the method displayed in the Web page

• Uses the name of the function or method if this property is not included

– Description property• Can format the description with HTML

Page 16: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

The Web Method (Page 4)

• Example:<WebMethod(CacheDuration:=10, _

Description:="HelloWorld() says hello.")> _

Public Function HelloWorld() As String

Return "Hello World"

End Function

Page 17: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

The Web Method

Page 18: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Previewing the Web Service Home Page (Page 1)

• View the Web Service home page in a browser – http://localhost/Ch11WebService/Ch11WS_ProductsDS.asmx

• Click the hyperlink GetCatMethod() takes you to URL:– http://localhost/Chapter11WebService/Ch11WS_ProductsDS.a

smx?op=GetCat()+Method

• Click the <Invoke> button to test GetCatMethod()– XML document returned (XML data within a DataSet

element)– Top section describes data; bottom section contains data

Page 19: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Previewing the Web Service Home Page (Page 2)

• Go <Back>, enter hyperlink for Service Description – View the service contract– Service Description URL – "?WSDL" is appended to

Web Service URL– http://localhost/Ch11WebService/Service1.asmx?WSDL

Page 20: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Creating a Web Service

Page 21: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Creating a Web Service

Page 22: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Creating a Web Service

Page 23: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Creating a Web Service

Page 24: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Create a New Web Method

Page 25: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Using a Web Service (Page 1)

• Call Web Services via protocol: – HTTP Get or HTTP Post

• Limit to primitive data types (integers, strings, arrays of primitive data types)

– SOAP (Simple Object Access Protocol)• Send any structure, class or enumerator, i.e. a DataSet

Page 26: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Using a Web Service (Page 2)

• Send information over Internet, packaged in a format that can travel over a TCP/IP network– Serialization - changing an object into a form that can

be readily transported over the network– Deserialization - change the string back into the

original structure

• If the document is in XML format– Use LoadXML method of the XML Document

Object Model (DOM) to retrieve the XML data

Page 27: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Consuming a Web Service from an ASP.NET Page (Page 1)

• In Web Form application, add a Web Reference in Solution Explorer1. Select Project from the menu bar

2. Select Add Web Reference… from the Project menu (or right-click "Web References" in Solution Explorer)

3. Find or enter the Web Service URL, i.e.• http://localhost/Chapter11WebService/Ch11WS_ProductsDS.

asmx

4. Give the reference a Web reference name:

5. Click the <Add Reference> button

Page 28: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Consuming a Web Service from an ASP.NET Page

Page 29: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Consuming a Web Service from an ASP.NET Page

Page 30: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Consuming a Web Service from an ASP.NET Page

Page 31: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Consuming a Web Service from an ASP.NET Page

Page 32: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

• Create an object from the Web Service reference• Call a WebMethod of the Web Service• Example:

Dim dsProducts As New DataSet

Dim objWebService As localhost.Chapter11WebService = New localhost.Chapter11WebService

dsProducts = objWebService.WS_GetProducts()

dgProdList.DataSource = dsProducts.Tables(0)

dgProdList.DataBind()

Consuming a Web Service from an ASP.NET Page (Page 2)

Page 33: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Consuming a Web Service from an ASP.NET Page

Page 34: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Consuming a Web Service from an ASP.NET Page• Create a new WebForm—WebForm2.aspx• Add a DataGrid—

– ID--dgCategories

Page 35: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Consuming a Web Service from an ASP.NET Page

Page 36: XML Web Services ASP.NET. Overview of Web Services (Page 1) Web Service – Part or all of a Web application that is publicly exposed so that other applications

Locating Web Services• UDDI - directory service for registering and

searching for Web Services • Third-party Web Services

– www.xmethods.net – www.coldrooster.com – www.asp.net – www.gotdotnet.com

• Local Web Services– Discovery document points to Web Services