1 / 41

Web Services

Tutorial : Introduction to. Web Services. With ASP.NET. Lesson 1: Creating a Web Service. Lesson 1 – Creating a Web Service. Exercise 1.1 – Create and understand a web service Web Service is objects and methods that can be invoked from any client over HTTP.

taite
Télécharger la présentation

Web Services

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tutorial: Introduction to Web Services With ASP.NET

  2. Lesson 1: Creating a Web Service

  3. Lesson 1 – Creating a Web Service • Exercise 1.1 – Create and understand a web service • Web Service is objects and methods that can be invoked from any client over HTTP. • In this exercise, you will create a simple Web service with “Hello World” method using Visual Studio.NET. • You will test the Web service through its associated documentation web page.

  4. Lesson 1 – Creating a Web Service 1. Launch Microsoft Visual Studio.NET 2008. 2. Create new website, choose Visual C#, select location as HTTP and enter site location as http://localhost/itcs426/myService , then click OK.

  5. Lesson 1 – Creating a Web Service 3. Create new web service, click WebSite ->Add New Item… 4. Select Web Service icon and enter file name as WebService.asmx, then click Add button. 5. The default page of web service class will be created.

  6. Lesson 1 – Creating a Web Service 1 2 3 1) ASP.NET web service class - kept in App_Code Folder only, used to create web methods 2) “WebService.asmx” file – let the client call web methods 3) “HelloWorld” method – default method in web service class

  7. Lesson 1 – Creating a Web Service 6. Test the web service, right click on WebService.asmx in solution explorer, then click Set As Start Page, press F5 to run the solution (start debugging). 7. Internet Explorer will start and display a documentation page for the web service as seen in the following illustration.

  8. Lesson 1 – Creating a Web Service 8. Click “HelloWorld” method to test the operation using the HTTP POST protocol, then click the Invoke button. 9. You will see a resulting XML page that contains the result of the “HelloWorld” method as seen in the following illustration.

  9. Lesson 2: Passing Parameters and Returning Values in Web Service

  10. Lesson 2 – Passing Parameters and Returning Values in Web Service • Exercise 2.1 – Create a web method with parameter passing and returning values • In this exercise, you will create a new web method “HelloUser” which will get the parameter passed from web page and return back the string value. • You will test the Web service through its associated documentation web page.

  11. Lesson 2 – Passing Parameters and Returning Values in Web Service 1. Create new web method “HelloUser”. [WebMethod] public string HelloUser(string yourName) { return "Hello,"+ yourName; } 2. Press F5 to run the solution. 3. Test the Web service method in browser. To test the method, click on the link for the method name “HelloUser”, type your name in the text box and then click Invoke button to view the result.

  12. Lesson 2 – Passing Parameters and Returning Values in Web Service

  13. Lesson 3: Adding a Web Reference

  14. Lesson 3 – Adding a Web Reference • Exercise 3.1 – Add a web reference to the web service • Web reference is proxy class created on the client to connect to the web service running on the server. • In this exercise, you will learn how to add a web reference to the web service.

  15. Lesson 3 – Adding a Web Reference 1. Click WebSite->Add web Reference.. 2. On Add Web Reference window • Enter URL Web Service: http://HostName/itcs426/myService/WebService.asmx • Host Name: IP Address or domain Web Service • Enter your Web Reference name: WebReference • Click Add Reference button

  16. Lesson 3 – Adding a Web Reference

  17. Lesson 3 – Adding a Web Reference 3. Result in solution explorer.

  18. Lesson 4: Calling a Web Service via Web Application

  19. Lesson 4 – Calling a Web Service via Web Application • Exercise 4.1 – Call a web service via web application • In this exercise, you will create an ASP.NET web page that uses the web service you created in lesson 2. • You will test the web service client that will pass the parameter to the web service.

  20. Lesson 4 – Calling a Web Service via Web Application 1. Open “Default.aspx” page, then add textbox, button, and label in the design view. • Textbox ID: yourName • Button ID: callService • Label ID: valueReturn

  21. Lesson 4 – Calling a Web Service via Web Application 2. Create method “callService_Click” for “callService” button. protected void callService_Click(object sender, EventArgs e) { //Create new object of web reference for call web service WebReference.WebService callResult = new WebReference.WebService(); //Call method HelloUser and pass parameter to method //Method HelloUser returns result to label control valueReturn.Text = callResult.HelloUser(yourName.Text); }

  22. Lesson 4 – Calling a Web Service via Web Application 3. Press F5 to run the solution. 4. Test the application in browser. Enter your name in textbox and click Result button.

  23. Lesson 5: Modifying a Web Service

  24. Lesson 5 – Modifying a Web Service • Exercise 5.1 – Modify web service by adding more web methods • In this exercise, you will add 2 web methods to your existing web service class. • “getQuestionList” method will return the arrayList for binding to dropDownList. • “checkAnswer” method will get the selected index from dropDownList as a parameter and return the answer in string format.

  25. Lesson 5 – Modifying a Web Service 1. Create new web methods “getQuestionList” and “checkAnswer” in WebService class. [WebMethod] public ArrayList getQuestionList() { ArrayList list = new ArrayList(); list.Add("Select Question"); list.Add("What is your name?"); list.Add("What is your student ID?"); return list; }

  26. Lesson 5 – Modifying a Web Service [WebMethod] public string checkAnswer(int questionIndex) { string answer; switch (questionIndex) { case 0: answer = "Select Question"; break; case 1: answer = "My name is Enter Name"; break;

  27. Lesson 5 – Modifying a Web Service case 2: answer = "My Student ID : Student ID Number"; break; default: answer = "Select Question"; break; } return answer;  }

  28. Lesson 5 – Modifying a Web Service 2. In solution explorer, update web reference by right clicking at App_WebReferences folder and click Update Web\Service. 3. Add dropDownList and Label to web page (Default.aspx) in design view. • dropDownList ID: List Question • dropDownList AutoPostBack: True • Label ID: AnswerValue

  29. Lesson 5 – Modifying a Web Service 3. Add code in code-behind (default.aspx.cs) protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { WebReference.WebService getDataSource; getDataSource = new WebReference.WebService(); ListQuestion.DataSource = getDataSource.getQuestionList(); ListQuestion.DataBind(); } }

  30. Lesson 5 – Modifying a Web Service protected void ListQuestion_SelectedIndexChanged(object sender, EventArgs e) { WebReference.WebService getAnswer; getAnswer = new WebReference.WebService(); AnswerValue.Text = getAnswer.checkAnswer(ListQuestion.SelectedIndex); }

  31. Lesson 5 – Modifying a Web Service 4. Run and test the result in browser.

  32. Lesson 6: Web Services in Real Application

  33. Lesson 6 – Web Service in Real Application • Exercise 6.1 – Call a web service from another machine and display in gridView. • In this exercise, you will learn to call web service from server and display the result in gridView in the form of table.

  34. Lesson 6 – Web Service in Real Application 1. Add web reference • URL: http://10.22.6.132/CustomerService/Customer.asmx • Web reference name: CustomerUpdate 2. Create new ASP.NET web form “Customer.aspx” in design page. Add textbox, button, and gridView. • Textbox ID: customerName • Button ID: saveCustomer • GridView ID: viewCustomer • GridView Set AutoFormat: Classic

  35. Lesson 6 – Web Service in Real Application

  36. Lesson 6 – Web Service in Real Application 2. Create new ASP.NET web form “Customer.aspx” in design page. Add textbox, button, and gridView. • Textbox ID: customerName • Button ID: saveCustomer • GridView ID: viewCustomer • GridView Set AutoFormat: Classic

  37. Lesson 6 – Web Service in Real Application

  38. Lesson 6 – Web Service in Real Application 3. Add code in code-behind (default.aspx.cs) protected void loadCustomer() { CustomerUpdate.Service cus = new CustomerUpdate.Service(); viewCustomer.DataSource = cus.viewCustomer(); viewCustomer.DataBind(); } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack){ loadCustomer(); }   }

  39. Lesson 6 – Web Service in Real Application protected void saveCustomer_Click(object sender, EventArgs e) { CustomerUpdate.Service cus = new CustomerUpdate.Service(); int result = cus.updateCustomer(CustomerName.Text, "Your Student ID"); if (result == 1) { loadCustomer(); } else { Response.Write("Insert faile !"); } }

  40. Lesson 6 – Web Service in Real Application 3. Run and test in browser

  41. Q & A

More Related