1 / 56

IFABMA Web Service

IFABMA Web Service. Objectives. You will be able to Create a web method that returns a collection of objects constructed from information retrieved from a database. Create a client for a web method that returns a collection of objects.

vachel
Download Presentation

IFABMA Web Service

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. IFABMA Web Service

  2. Objectives You will be able to • Create a web method that returns a collection of objects constructed from information retrieved from a database. • Create a client for a web method that returns a collection of objects. • Display data from a collection returned by a web method using a databound control.

  3. The Association • All of Northwind Traders’ suppliers are members of the (fictitious) International Association of Food and Beverage Manufacturers (IFABMA). • IFABMA provides an e-commerce web site that accepts orders for all of its members using a web service. • http://ifabma.com • These are manufacturers and distributors. Northwind Traders and other wholesalers would use this web service to replace inventory.

  4. IFABMA Web Services We will reproduce some of the functionality of this web service as a nontrivial example.

  5. The Example • Let's implement the Get_Members web method. • Query the Northwind database Suppliers table. • Remember that IFABMA members are Northwind suppliers. • Return • Company Name • Contact Name • Phone Number • for all IFABMA members

  6. New Web Site • In Visual Studio 2008, create a new web site.

  7. Create New Web Site • Template: ASP.NET Web Service (NOT Web Site) • Name: IFABMA_Web_Service

  8. Create New Web Site • Change file names from Service to IFABMA_Web_Service • IFABMA_Web_Service.asmx • App_Code\IFABMA_Web_Service.cs • In IFABMA_Web_Service.cs • Change class name from Service to IFABMA_Web_Service • Also constructor • Change Namespace in [WebService... ] to your scorpius URL: http://cis4930wp.eng.usf.edu/yourusername • Remember that this is just a unique name for the namespace. • It is not used as a URL.

  9. IFABMA_Web_Service.cs

  10. IFABMA_Web_Service.asmx • In IFABMA_Web_Service.asmx, change Service to IFABMA_Web_Service

  11. Set Port Number • In Solution Explorer, select the project IFABMA_Web_Service • View > Properties Window • Change “Use dynamic ports” to false. • Change Port number to 4500 • as shown on next slide • 4500 is an arbitrary choice. • Won't change if the website is rebuild.

  12. Set Port Number

  13. Web Method Get_Members • We will implement the Get_Members web method. • Returns information for all IFABMA members: • Company Name • Contact Name • Phone Number • We need class Member. • Object will hold the above information for one IFABMA member. • Get_Members will return an array of Member objects.

  14. Web Services Classes • In order for a web method to be able to return an object, the class must be serializable. • Visual Studio will provide serialization code automatically and transparently if the class consists only of public fields and properties. • Must have a get and a set for each property. • Must have a default constructor. • No parameters. • Classes used in SOAP typically are just containers. • Data but no methods.

  15. Class Member • Add Class Member to web site. • Represents an IFABMA Member • “Supplier” in the Northwind Traders Database. • Website > Add New Item

  16. Adding Class Member

  17. Adding Class Member Click Yes

  18. Class Member using System; using System.Data.SqlClient; [Serializable] public class Member { private string companyName; private string contactName; private string phone; private bool valid; private string errorMessage; // Properties ... // Default constructor public Member() { valid = false; errorMessage = "Object not initialized"; }

  19. Adding the Web Method • In IFABMA_Web_Service.cs • Delete the HelloWorld method. • Replace it with a Get_Members stub • as shown on the next slide.

  20. Get_Members Stub using System; using System.Web; using System.Web.Services; using System.Collections.Generic; ... [WebMethod] public List<Member> Get_Members() { List<Member> members = new List<Member>(); Member m = new Member(); members.Add(m); return members; }

  21. Testing • Let’s test the service with the Get_Members() stub before going further. • In Solution Explorer • Select IFABMA_Web_Service.asmx • Right click and select View in Browser

  22. View in Browser

  23. View in Browser

  24. View in Browser

  25. Function Return Type • Note the root node in the XML message. • <ArrayOfMember ... > • Our Get_Members method returns a List<Member> • But SOAP doesn't know about the .NET generic List. • The List<Member> is converted into an array of Member objects for transmission. • A client program have array of Member as the return type for Get_Members.

  26. Implementing the Real Get_Members Web Method • What we need is a collection of all IFABMA members. • Requires query against a database table holding the IFABMA members. • Actually the Suppliers table in the Northwind database. • Will need a connection string in web.config

  27. web.config <connectionStrings> <add name="Northwind" connectionString="Data Source=scorpius.eng.usf.edu; Initial Catalog=Northwind; User ID=wpusr40; Password=xxxxx"/> </connectionStrings>

  28. New Constructor for Class member • We need a new constructor for class Member that takes a SqlDataReader as its input parameter. • Caller does the query producing a SqlDataReader. • Repeatedly calls the SqlDataReader's Read method, making the next line of the query result available, and passes the SqlDataReader to the constructor for class Member.

  29. Table Suppliers In Server Explorer, look at the definition of table Suppliers Note that most columns allow nulls.

  30. Potentially Null Items • Most of the columns in the Northwind Suppliers table allow nulls. • We have to check for an item being null and avoid trying to retrieve it from the reader if it is null. • An attempt to retrieve a null item will throw an exception. (Bad!) • This is not the same as a null reference. • Can't use if (xxx == null)

  31. New Constructor for Class member public Member(SqlDataReader rdr) { try { companyName = (string)rdr["CompanyName"]; if (rdr["CompanyName"] is DBNull) { contactName = ""; } else { contactName = (string)rdr["ContactName"]; } ...

  32. New Constructor for Class Member (continued) if (rdr["Phone"] is DBNull) { phone = ""; } else { phone = (string)rdr["Phone"]; } valid = true; errorMessage = ""; } catch (Exception ex) { valid = false; errorMessage = ex.Message; } }

  33. Class Query • Let's create a static class to hold all of the ADO.NET code. • As we have done in previous projects. • Class Query will have exclusive responsibility for knowledge of how to retrieve information from the database. • Website > Add New Item

  34. Adding Class Query

  35. Class Query using System; using System.Data.SqlClient; using System.Web.Configuration; using System.Collections.Generic; public static class Query {

  36. Query.Get_Members public static List<Member> Get_Members() { List<Member> members = new List<Member>(); SqlDataReader rdr = null; SqlConnection cn = null; try { cn = Setup_Connection(); rdr = Get_Reader(cn); // Process Query Result while (rdr.Read()) { Member m = new Member(rdr); members.Add(m); } } ...

  37. Query.Get_Members (continued) catch (Exception ex) { Member m = new Member(); m.ErrorMessage = ex.Message; members.Add(m); } finally { if (rdr != null) rdr.Close(); if (cn != null) cn.Close(); } return members; }

  38. Query.Setup_Connection private static SqlConnection Setup_Connection() { String connection_string = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString; SqlConnection cn = new SqlConnection(connection_string); cn.Open(); return cn; }

  39. Query.Get_Reader private static SqlDataReader Get_Reader(SqlConnection cn) { SqlCommand cmd = new SqlCommand(); cmd.CommandText = "SELECT CompanyName, ContactName, Phone " + " FROM Suppliers"; cmd.Connection = cn; return cmd.ExecuteReader(); }

  40. IFABMA_Web_Service.cs • Replace stub with the real method. [WebMethod] public List<Member> Get_Members() { List<Member> members = Query.Get_Members(); return members; }

  41. Try it! • Build web site. • In Solution Explorer • Right click on IFABMA_Web_Service.asmx • Select “View in Browser”

  42. IFABMA Web Service

  43. Get_Members

  44. Results from Get_Members() End of Section

  45. A Client for IFABMA_Web_Service • Now let's produce a web app that uses the web method that we just created. • Keep Visual Studio and the browser window open and running the IFABMA Web Service web site. • Open a new instance of Visual Studio 2008 for the client app.

  46. A Client for IFABMA_Web_Service • The Repeater_Demo web app from last class displays information about Northwind Customers that it gets from the database. • CompanyName • ContactName • Phone • The Northwind Suppliers table has columns with the same names. • The IFABMA Web Service web method Get_Members returns these columns of the Northwind Suppliers table to its caller. • Class Member has properties with the same names. • We can reuse the .aspx file from Repeater_Demo to display IFABMA Member information.

  47. A Client for IFABMA_Web_Service • Download the Repeater Demo from last class: • http://www.cse.usf.edu/~turnerr/Web_Application_Design/Downloads/2012_06_26_In_Class/ File Repeater_Demo.zip • Extract all • Drill down to the website folder. • Rename website folder as IFABMA_Client. • Open the website in the new Visual Studio instance. • Delete DataSourceID from the Repeater in Default.aspx. • As shown on next slide. • Change title to IFABMA Client.

  48. Delete DataSourceID

  49. The Repeater

  50. Add Web Reference

More Related