28
1 CS 3870/CS 5870: Note 23 Web Service

1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

Embed Size (px)

Citation preview

Page 1: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

1

CS 3870/CS 5870: Note 23

Web Service

Page 2: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

2

Web Service

• What is Web Service?

Providing functionality online to other applications, Web and Windows applications.

• The original Goal of MS ASP.NET

To enable developers to build and consume Web services with easy

Page 3: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

Communication Between Disparate Systems

• Systems within an organization– Unix– Windows– Mac– Others

• Various systems need to talk with one another– Not an easy job

3

Page 4: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

XML

• XML: eXtensible Makeup Language– Simpler version of SGML

(Standard Generalized Makeup Language)– Best choice for disparate systems for now

• SOAP

Simple Object Access Protocols

4

Page 5: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

Previous Attempts• DCOM

Distributed Component Object Model• RMI

Remote Method Invocation• CORBA: . . .• IIOP: . . .

• Driven by a single vendor or

Very vendor specific

5

Page 6: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

6

Creating Web Service

• Add New Items

• Web Service

– Specify Name

• File WebService.asmx

• File WebService.vb

– under App_Code

Page 7: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

7

WebService Page Directive

File WebService.asmx

<%@ WebService Language="VB"

CodeBehind="~/App_Code/WebService.vb"

Class="WebService" %>

Page 8: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

8

Web Service Class

Imports System.Web

Imports System.Web.Services

Imports System.Web.Services.Protocols

‘ To allow this Web Service to be called from script,

‘ using ASP.NET AJAX, uncomment the following line.

‘ <System.Web.Script.Services.ScriptService()> _

<WebService(Namespace:="http://tempuri.org/")> _

<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _

Public Class WebService

Inherits System.Web.Services.WebService

. . .

End Class

Page 9: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

9

Web Service Class

• Web Service Namespace

– Default : http://tempuri.org/– Change to: https://xray.ion.uwplatt.edu/YourUserName/

• ScriptService: AJAX

• Service.Protocols– SOAP can be used in VB.NET

Page 10: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

10

Web Service Class

Public Class WebService

Inherits System.Web.Services.WebService

<WebMethod()> _

Public Function HelloWorld() As String

Return "Hello"

End Function

<WebMethod()> _

Public Function WS_GetAllProducts() As Data.DataTable

. . .

‘ Return a datatable

End Function

End Class

Page 11: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

11

Looking at Web Service

Go to the Web Service URL:

https://xray.ion.uwplatt.edu/yangq

/WebService.asmx

Page 12: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

SOAP

• SOAP Envelop

• SOAP Body

12

Page 13: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

13

Web Service Class

<WebService(Namespace:="https://xray.ion.uwplatt.edu/YangQ/")> _

Public Class WebService

Inherits System.Web.Services.WebService

‘ Create new web methods

<WebMethod()> _

Public Sub WS_UpdateProduct(. . .)

. . .

End Sub

End Class

Page 14: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

14

Web Service Class<WebService(Namespace:="https://xray.ion.uwplatt.edu/YangQ/")> _

Public Class WebService

Inherits System.Web.Services.WebService

‘ Insert a new record into the table

Public Sub WS_InsertProduct(. . .)

. . .

End Sub

End Class

Page 15: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

15

Web Service Class<WebService(Namespace:="https://xray.ion.uwplatt.edu/YangQ/")> _

Public Class WebService

Inherits System.Web.Services.WebService

‘ Insert a new record into the table

<WebMethod()> _

Public Sub WS_InsertProduct(. . .)

. . .

End Sub

End Class

Must have

<WebMethod()> _

Page 16: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

16

Consuming Web Service

• Adding Web Reference

• Invoking Web Service from client application

Page 17: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

17

Adding Web Reference

• Right click the root of the Web Site

• Add Service Reference

• Enter the URL of the wanted web service followed by “?WSDL”

https://xray.ion.uwplatt.edu/CS3870/UWPCSSEWebservice.asmx?WSDL

• Namespace

UserName (Web Site Name, e.g. CS3870)

Page 18: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

18

Adding Web Reference

• Add Service Reference to the Web Service created on your Web site

• Namespace

YourUserName (Web Site Name, e.g. CS3870)

Page 19: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

19

Web Config

The Web Config file will be updated automatically with the following.

<system.serviceModel>

<bindings>

<basicHttpBinding>

<binding name="UWPCSSEWebServiceSoap">

<security mode="Transport" />

</binding>

<binding name="UWPCSSEWebServiceSoap1" />

</basicHttpBinding>

</bindings>

<client>

<endpoint address="https://xray.ion.uwplatt.edu/cs3870/UWPCSSEWebservice.asmx"

binding="basicHttpBinding" bindingConfiguration="UWPCSSEWebServiceSoap"

contract="CS3870.UWPCSSEWebServiceSoap" name="UWPCSSEWebServiceSoap" />

</client>

</system.serviceModel>

Page 20: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

Page Product.aspx

Protected Sub Page_Load(. . . ) Handles Me.Load

Dim obj As CS387.UWPCSSEWebServiceSoapClient

Dim myTable As Data.DataTable = obj.WS_GetAllProducts()

GridView1.DataSource = myTable

GridView1.DataBind()

End Sub

20

Page 21: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

21

Selecting Web Services

Dim ws As String

Dim obj As Object

If ws = "CS3870" Then

‘obj = New CS3870.UWPCSSEWebService

obj = New CS3870.UWPCSSEWebServiceSoapClient

ElseIf ws = "yangq" Then

obj = New yangq.UWPCSSEWebServiceSoapClient

End If

Session("WS") = obj

Page 22: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

22

Invoking Web Methods

Dim obj As Object

Dim myTable As Data.DataTable

Protected Sub Page_Load(. . .) Handles Me.Load

obj = Session("WS")

myTable = obj.WS_GetAllProducts()

Session("WS_Table") = myTable

GridView1.DataSource = myTable

GridView1.DataBind()

End Sub

Page 23: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

Must Select WS first

• On each of other pages

Protected Sub Page_Load(. . .) Handles Me.Load

If Session("WS") Is Nothing Then

'Server.Transfer("~/Lab9/Default.aspx")

Response.Redirect("~/Lab9/Default.aspx")

Exit Sub

End If

. . .

End Sub

23

Page 24: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

Accessing Table From Web Service

No Credit if Accessing Database without Web Service

24

Page 25: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

Session Variables

• Need to be Created and Updated

25

Page 26: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

Not Working

• Delete and Create Service Reference?

26

Page 27: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

AJAX

• All Web Pages

27

Page 28: 1 CS 3870/CS 5870: Note 23 Web Service. 2 What is Web Service? Providing functionality online to other applications, Web and Windows applications. The

AJAX: Test 3• Page Default (displaying courses)

– Two UpdatePanels– Triggers– UpdateMode: Conditional

• Page Credit– One UpdatePanel– Put a Panel or another container inside UpdatePanel– Add user controls inside the Panel– No Triggers

28