CS 3870/CS 5870 Web Service
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
SOAP and XML XML: eXtensible Makeup Language SOAP Simpler version of SGML (Standard Generalized Makeup Language) Best choice for disparate systems for now SOAP Simple Object Access Protocol
Previous Attempts DCOM Distributed Component Object Model RMI Remote Method Invocation CORBA: . . . IIOP: . . . Driven by a single vendor or Very vendor specific
Creating Web Service Add New Items Web Service Specify Name: UWPCSSEWebService2016 File UWPCSSEWebService2016.asmx File UWPCSSEWebService2016.vb under App_Code
WebService Page Directive File UWPCSSEWebService2016.asmx <%@ WebService Language="VB" CodeBehind="~/App_Code/ UWPCSSEWebService2016.vb" Class="WebService" %>
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
Web Service Class ScriptService: AJAX Service.Protocols Web Service Namespace Default : http://tempuri.org/ Change to: https://alpha.ion.uwplatt.edu/YourWebSiteName/ ScriptService: AJAX Service.Protocols SOAP can be used in VB.NET
Web Service Class Public Class UWPCSSEWebService2016 Inherits System.Web.Services.WebService <WebMethod()> _ Public Function HelloWorld() As String Return "Hello, World" End Function Public Function WS_GetAllProducts() As Data.DataTable . . . ‘ Return a datatable End Class
Web Service Class Public Class UWPCSSEWebService2016 Inherits System.Web.Services.WebService <WebMethod()> _ Public Function WS_GetAllProducts() As Data.DataTable Public Sub WS_UpdateProduct(. . . Public Sub WS_InsertProduct(. . . Public Sub WS_DeleteProduct(. . . End Class Must have <WebMethod()> Can call methods of SQLDataClass Make sure method setupAdapter is called Prog3
Looking at Web Service https://alpha.ion.uwplatt.edu/CS3870/UWPCSSEWebService2016.asmx https://alpha.ion.uwplatt.edu/YangQ/UWPCSSEWebService2016.asmx
SOAP SOAP Envelop SOAP Body
Consuming Web Service Adding Web Reference Invoking Web Service from client application
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://alpha.ion.uwplatt.edu/CS3870/UWPCSSEWebservice2016.asmx?WSDL Namespace Your choice for your program
Required Three Web References Service Reference to the Web Service created on Web site CS3870 Service Reference to the Web Service created on Web site YangQ Service Reference to the Web Service created on your Web site
The Web Config file will be updated automatically with the following. <system.serviceModel> <bindings> <basicHttpBinding> <binding name="UWPCSSEWebService2016Soap"> <security mode="Transport" /> </binding> <binding name="UWPCSSEWebService2016Soap1" /> </basicHttpBinding> </bindings> <client> <endpoint address="https://xray.ion.uwplatt.edu/cs3870/UWPCSSEWebservice2016.asmx" binding="basicHttpBinding" bindingConfiguration="UWPCSSEWebService2016Soap" contract="CS3870.UWPCSSEWebServiceSoap" name="UWPCSSEWebService2016Soap" /> </client> </system.serviceModel>
Selecting Web Services on Form Default.aspx Dim ws As String = DropDownList1.Text Dim obj As Object If ws = "CS3870" Then obj = New CS3870.UWPCSSEWebService2016SoapClient ElseIf ws = “YangQ" Then obj = New MyWebService.UWPCSSEWebService2016SoapClient Else obj = New YourWebService.UWPCSSEWebService2016SoapClient End If Session(“Prog9_WS") = obj
Display the selected Web Service name If ws = "CS3870" Then . . . obj = New YourWebService.UWPCSSEWebService2016SoapClient End If Session(“Prog9_WS") = obj ws = obj.ToString Dim c As Control = Master.Master.FindControl("form1") c = c.FindControl("ContentPlaceHolder1") c = c.FindControl("Label1") CType(c, Label).Text = ws Session("Prog9_WSName") = ws
Prog9MasterPage <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <h3>Prog 9: <asp:Label ID="Label1" runat="server" Text=""></asp:Label> </h3> </asp:Content> ws = obj.ToString Dim c As Control = Master.Master.FindControl("form1") c = c.FindControl("ContentPlaceHolder1") c = c.FindControl("Label1") CType(c, Label).Text = ws Session("Prog9_WSName") = ws
Invoking Web Methods on Form AllProducts.aspx Dim obj As Object Dim myTable As Data.DataTable Protected Sub Page_Load(. . .) Handles Me.Load obj = Session(“Prog9_WS") myTable = obj.WS_GetAllProducts() GridView1.DataSource = myTable GridView1.DataBind() End Sub
Invoking Web Methods on Form Product.aspx Protected Sub Page_Load(. . . ) Handles Me.Load If Session("Prog9_WS") Is Nothing Then Response.Redirect("~/Prog9/Default.aspx") End If obj = Session(“Prog9_WS") myTable = obj.WS_GetAllProducts() GridView1.DataSource = myTable GridView1.DataBind() End Sub
Try and Catch Protected Sub Button1_Click(. . .) Handles btnUpdate.Click Try . . . obj.WS_UpdateProduct(oldID, txtName.Text, txtPrice.Text, txtDescription.Text) Catch ex As Exception txtMsh.Text = ex.Message End Try End Sub
Shopping and Updating Look into Your Prog3
Accessing Database Using Web Service No Credit if Accessing Database without Web Service
Session Variables Need to be Created and Updated Name starting using “Prog9_”
Delete and Create Service Reference? Not Working Delete and Create Service Reference?
AJAX All Web Forms except Default.aspx Could have multiple UpdatePanels on one Web form.
Bonus Points