CS 3870 Web Service 1 Web Service What
CS 3870 Web Service 1
Web Service • What is Web Service? Providing functionality online to other applications, Web or Windows applications. • The original Goal of MS ASP. NET To enable developers to build and consume Web services with easy 2
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 and SOAP • XML: e. Xtensible 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
. NET Framework 4. 6 • Right click the main Web site • Select Property Pages • Build • . NET Framework 4. 6 6
Creating Web Service • Add New Items • Web Service (ASMX) – Specify Name: UWPCSSEWeb. Service 2018 • File UWPCSSEWeb. Service 2018. asmx – Inside the main Web site • File UWPCSSEWeb. Service 2018. cs – Inside App_Code 7
Web. Service Page Directive File UWPCSSEWeb. Service 2018. asmx <%@ Web. Service Language=“C#" Code. Behind="~/App_Code/UWPCSSEWeb. Service 2018. cs" Class=“UWPCSSEWeb. Service 2018" %> 8
using using Web Service Class (C# File) System; System. Collections. Generic; System. Linq; System. Web. Services; /// <summary> /// Summary description for UWPCSSEWeb. Service 2018 /// </summary> [Web. Service(Namespace = "http: //tempuri. org/")] [Web. Service. Binding(Conforms. To = Wsi. Profiles. Basic. Profile 1_1)] // To allow this Web Service to be called from script, using ASP. NET AJAX, uncomment the // following line. // [System. Web. Script. Services. Script. Service] public class UWPCSSEWeb. Service 2018 : System. Web. Services. Web. Service { public UWPCSSEWeb. Service 2018() { //Uncomment the following line if using designed components //Initialize. Component(); } [Web. Method] public string Hello. World() { return "Hello World"; } 9 }
Web Service Class • Web Service Namespace – Default : http: //tempuri. org/ – Change to: https: //alpha. ion. uwplatt. edu/Your. Web. Site. Name/ 10
Web Service Class public class UWPCSSEWeb. Service 2018 : System. Web. Services. Web. Service { //Initialize Products in table. public UWPCSSEWeb. Service 2018() { SQLData. Class. get. All. Products(); } [Web. Method] public string Hello. World() { return "Hello World"; } } 11
Four More Methods [Web. Method] public System. Data. Table WS_Get. All. Products() [Web. Method] public void WS_Update. Product(string ID, string new. Name, double new. Price, string new. Description) [Web. Method] public void WS_Insert. Product(string ID, string name, double price, string description) [Web. Method] public void WS_Delete. Product(string ID) 12
Looking at Web Service https: //alpha. ion. uwplatt. edu/CS 3870/UWPCSSEWeb. Service 2018. asmx https: //alpha. ion. uwplatt. edu/Yang. Q/UWPCSSEWeb. Service 2018. asmx ? ? ? 13
SOAP • All Web Methods • SOAP Envelop • SOAP Body 14
Completing Web Methods public class UWPCSSEWeb. Service 2018 : System. Web. Services. Web. Service { [Web. Method] public System. Data. Table WS_Get. All. Products() [Web. Method] public void WS_Update. Product(string ID, string new. Name, double new. Price, string new. Description) [Web. Method] public void WS_Insert. Product(string ID, string name, double price, string description) [Web. Method] public void WS_Delete. Product(string ID) } Call methods of SQLData. Class Make sure method setup. Adapter is called 15
Consuming Web Service • Adding Web Reference • Invoking Web Service from client application 16
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/CS 3870/UWPCSSEWebservice 2018. asmx? WSDL • Go • Namespace Web. Service. CS 3870 • OK 17
Required Three Web References • Service Reference to the Web Service created on Web site CS 3870 • Service Reference to the Web Service created on Web site Yang. Q • Service Reference to the Web Service created on your Web site 18
Web Config The Web Config file will be updated automatically with the following. <system. service. Model> <bindings> <basic. Http. Binding> <binding name="UWPCSSEWeb. Service 2018 Soap"> <security mode="Transport" /> </binding> <binding name="UWPCSSEWeb. Service 2018 Soap 1" /> </basic. Http. Binding> </bindings> <client> <endpoint address="https: //xray. ion. uwplatt. edu/cs 3870/UWPCSSEWebservice 2018. asmx" binding="basic. Http. Binding" binding. Configuration="UWPCSSEWeb. Service 2018 Soap" contract="CS 3870. UWPCSSEWeb. Service 2018 Soap" name="UWPCSSEWeb. Service 2018 Soap" /> </client> </system. service. Model> 19
Selecting Web Services on Form Default. aspx //Sets the webservice to the selected in the dropdown list. protected void Drop. Down. List 1_Selected. Index. Changed(. . . ) { var ws = Drop. Down. List 1. Text; object obj; if (ws == "CS 3870") obj = new Web. Service. CS 3870. UWPCSSEWeb. Service 2018 Soap. Client(); else if (ws == "Yang. Q") obj = new Web. Service. Yang. Q. UWPCSSEWeb. Service 2018 Soap. Client(); else obj = new Web. Service. Web. Site. UWPCSSEWeb. Service 2018 Soap. Client(); . . . } 20
protected void Drop. Down. List 1_Selected. Index. Changed(. . . ) { var ws = Drop. Down. List 1. Text; object obj; if (ws == "CS 3870"). . . Session["Prog 9_WS"] = obj; Session["Prog 9_WSName"] = obj. To. String(); Session["Prog 9_Page. Index"] = 0; Session["Prog 9_Record. Index"] = 0; Session["Prog 9_Unit. Price"] = 0; Session["Prog 9_Index"] = 0; Session["Prog 9_ID"] = ""; Set. Label. Text(); } 21
Prog 9 Master. Page <asp: Content ID="Content 2" Content. Place. Holder. ID="Content. Place. Holder 1" Runat="Server"> <h 3 class="center. Align"> Prog 9: <asp: Label ID="Label 1" runat="server" Text=""></asp: Label> </h 3> <asp: Tree. View ID="Tree. View 1" runat="server". . . > </asp: Tree. View> <asp: Site. Map. Data. Source ID="Site. Map. Data. Source 1" runat="server" /> <asp: Script. Manager ID="Script. Manager 1" runat="server"></asp: Script. Manager> <asp: Content. Place. Holder ID="Content. Place. Holder 1" runat="server "> </asp: Content. Place. Holder> </asp: Content> 22
Display the selected Web Service name Session["Prog 9_WS"] = obj; Session["Prog 9_WSName"] = obj. To. String(); string ws = Session["Prog 9_WSName"]. To. String(); Control c = Master. Find. Control("form 1"); c = c. Find. Control("Content. Place. Holder 1"); c = c. Find. Control("Label 1"); ((Label)c). Text = ws; 23
Invoking Web Methods on Form All. Products. aspx protected void Page_Load(object sender, Event. Args e) { var obj = Session["Prog 9_WS"]; if (obj == null) Response. Redirect("~/Prog 9/Default. aspx"); System. Data. Table my. Table; if (obj. To. String(). Contains("Yang. Q")) my. Table = ((Web. Service. Yang. Q. UWPCSSEWeb. Service 2018 Soap. Client)obj). WS_Get. All. Products(); else if (obj. To. String(). Contains(“User. Name")) my. Table = ((Web. Service. User. Name. UWPCSSEWeb. Service 2018 Soap. Client)obj). WS_Get. All. Products(); else my. Table = ((Web. Service. CS 3870. UWPCSSEWeb. Service 2018 Soap. Client)obj). WS_Get. All. Products(); Grid. View 1. Data. Source = my. Table; if (!Is. Post. Back) Grid. View 1. Page. Index = (int)Session["Prog 9_Page. Index"]; Grid. View 1. Data. Bind(); } 24
Must Set to Tls 12 protected void Page_Load(object sender, Event. Args e) {. . . System. Net. Service. Point. Manager. Security. Protocol = System. Net. Security. Protocol. Type. Tls 12; ((Web. Service. CS 3870. UWPCSSEWeb. Service 2018 Soap. Client)obj). WS_Get. All. Products(); } 25
try { var obj = Session["Prog 9_WS"]; if (obj. To. String(). Contains("Yang. Q")) ((Web. Service. Yang. Q. UWPCSSEWeb. Service 2017 Soap. Client)obj). WS_Insert. Product (Product. ID, Product. Name, double. Parse(Unit. Price), Description); else if (obj. To. String(). Contains("Dubiel. T")) ((Web. Service. Dubiel. T. UWPCSSEWeb. Service 2017 Soap. Client)obj). WS_Insert. Product (Product. ID, Product. Name, double. Parse(Unit. Price), Description); else ((Web. Service. CS 3870. UWPCSSEWeb. Service 2017 Soap. Client)obj). WS_Insert. Product (Product. ID, Product. Name, double. Parse(Unit. Price), Description); txt. Msg. Text = "Record Inserted"; } catch(Exception ex) { txt. Msg. Text = ex. Message; } 26
Accessing Database Using Web Service No Credit if Accessing Database without Web Service 27
Session Variables • Need to be Created and Updated • Name starting using “Prog 9_” 28
Not Working • Delete and Create Service Reference? 29
AJAX All Web Forms except Default. aspx Should have multiple Update. Panels on one Web form. 30
- Slides: 30