1 CS 3870/CS 5870: Note 20 Web Service. 2 What is Web Service? Providing functionality online to...

Post on 17-Jan-2016

222 views 0 download

Tags:

Transcript of 1 CS 3870/CS 5870: Note 20 Web Service. 2 What is Web Service? Providing functionality online to...

1

CS 3870/CS 5870: Note 20

Web Service

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

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

XML

• XML: eXtensible Makeup Language– Simpler version of SGML

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

• SOAP

Simple Object Access Protocol

4

Previous Attempts• DCOM

Distributed Component Object Model• RMI

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

• Driven by a single vendor or

Very vendor specific

5

6

Creating Web Service

• Add New Items

• Web Service

– Specify Name

• File WebService.asmx

• File WebService.vb

– under App_Code

7

WebService Page Directive

File WebService.asmx

<%@ WebService Language="VB"

CodeBehind="~/App_Code/WebService.vb"

Class="WebService" %>

8

Web Service Class (VB File)

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

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

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

11

Looking at Web Service

Go to the Web Service URL:

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

https://xray.ion.uwplatt.edu/CS3870/UWPCSSEWebService.asmx

SOAP

• SOAP Envelop

• SOAP Body

12

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

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

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()> _

16

Consuming Web Service

• Adding Web Reference

• Invoking Web Service from client application

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 (the grader assumes this!)

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

18

Adding Web Reference

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

• Namespace

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

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 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

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

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

Must Select WS first

• On each of other pages

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

If Session("WS") Is Nothing Then

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

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

Exit Sub

End If

. . .

End Sub

23

Accessing Table From Web Service

No Credit if Accessing Database without Web Service

24

Session Variables

• Need to be Created and Updated

25

Not Working

• Delete and Create Service Reference?

26

AJAX

• All Web Pages

27

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