230 likes | 239 Views
Web Services. 6.916 Lecture Tuesday, October 17, 2000 Jim Miller (Microsoft). Videos: Visions of the Future. The Web Today. Independent Web sites Web farms and data bases Reliability Round-the-clock operation Graceful upgrade Great for browsing OK for indexing and searching.
E N D
Web Services 6.916 Lecture Tuesday, October 17, 2000 Jim Miller (Microsoft)
The Web Today • Independent Web sites • Web farms and data bases • Reliability • Round-the-clock operation • Graceful upgrade • Great for browsing • OK for indexing and searching
A Hard Problem Today:A Shopping Service • Clients describe themselves • Credentials indicate purchasing groups they participate in (AAA, MIT, United Airlines) • Pay for access to additional groups run by us • Service contracts with suppliers • Suppliers trust us to test credentials • Suppliers offer discounts for our groups • Suppliers compete with each other • Price lists with discounts aren’t public • Product offerings change often
? What’s the Structure? Browser Shopping Service Supplier 1 HTTP / HTML Supplier 2 Customer Data and Credentials Supplier 3 Merchandise, Prices, and Discounts
Distributed Systems • 1970s: FTP, RFC780 (email) • Manually move data • No embedded links • No graphics (to speak of) • 1980s: RPC / DCE • Describe a method in a special language (IDL) • Generate a client-side proxy and server-side stub • Marshalls data, etc. • Link client and server code against the proxy/stub • “Feels like” a procedure call • Problem: IDL maintenance • 1990s: CORBA / DCOM • IDL-based (extended) • Location-independent • Object-based access • Problem: no inter-op
? What’s the Structure? Browser Shopping Service Supplier 1 HTTP / HTML Supplier 2 Idea:A Web Service is a Web site that provides a programmatic interface. Customer Data and Credentials Supplier 3 Merchandise, Prices, and Discounts
Web Services A Web site that provides a programmatic interface • Operations are called using HTTP and XML (SOAP) • Results are returned using HTTP and XML (SOAP) • The operations are described using XML (WSDL) What’s changed? • Timing: TCP/HTTP/XML is ubiquitous • Common client: browsers everywhere • Rich content standard: HTML, XML, style sheets, and client-side objects
Sample • The E-Commerce example is too big… • There are three Web services to play with • TerraService has images (photos and maps) • CensusService has population and demographic data • LandmarkService has latitude and longitude of “interesting” places (rivers, train stations, …) • Let’s build a Web page that uses the three services to provide a rich user experience
? What’s the Structure? Browser Geography Site Terra HTTP / HTML Census Landmark
Web Service Implementation (I) namespace TerraServer { using System; ... public class TerraService : WebService { [WebMethod] public PlaceFacts[] GetPlaceList(string placeName, int MaxItems, Boolean imagePresence) { PlaceFacts[] placeFacts = new PlaceFacts[MaxItems]; int i = 0, iRowType = 0; SQLDataReader reader = null; // Create a new Connection and DataSetCOmmand TerraServerConnection tsCon = new TerraServerConnection(); SQLCommand tsCmd = new SQLCommand("spTSNFindPlaceFirst", tsCon); tsCmd.CommandType = CommandType.StoredProcedure; // . . . Continued on next slide
Service Implementation (II) SQLParameter workParam = null; workParam = tsCmd.Parameters.Add (new SQLParameter("@PlaceName", SQLDataType.VarChar, 100)); workParam.Direction = ParameterDirection.Input; // . . . tsCmd.Parameters["@PlaceName"].Value = placeName;// . . . try { tsCon.Open(0); tsCmd.Execute(out reader); while (reader.Read()) { iRowType = reader.GetInt32(1); placeFacts[i].Place.City = reader.GetString(5); // . . . i++; if ((i == MaxItems)||(!reader.NextResult())) break; } } finally { if (reader != null) reader.Close(); tsCon.Close(); } return(placeFacts); } } }
How Is a Service Described? • WSDL (“Web Service Description Language”) • XML-based • Submitted to W3C for standardization • Look at http://terranet.research.microsoft.com/ • terraservice.asmx?sdl • censusservice.asmx?sdl • landmarkservice.asmx?sdl • Take a look at GetTheme in terraservice
How Is a Service Used? • Run WebServiceUtil to generate client proxy (for each service) • Compile together to form one program that has all three proxies available • Write code that calls the proxies as though they were the actual objects • Proxies • Generate SOAP • Send to the target service • Receive the SOAP response • Parse the SOAP • Generate an object • Return the object as a result
Service Description (WSDL) <?xml version="1.0"?> <serviceDescription xmlns:s0="http://tempuri.org/" name="TerraService" targetNamespace="http://tempuri.org/" xmlns="urn:schemas-xmlsoap-org:sdl.2000-01-25"> <httpget xmlns="urn:schemas-xmlsoap-org:get-sdl-2000-01-25"> <service> <requestResponse name="GetPlaceList" href="http://207.46.235.37/terranet/terraservice.asmx/GetPlaceList"> <request> <param name="placeName"/> <param name="MaxItems"/> <param name="imagePresence"/> </request> <response> <mimeXml ref="s0:ArrayOfPlaceFacts"/> </response> </requestResponse> </service> </httpget> </serviceDescription>
SOAP Request <?xml version="1.0"?> <soap:Envelope xmlns:soap= "http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc= "http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi= "http://www.w3.org/1999/XMLSchema-instance" xmlns:xsd= "http://www.w3.org/1999/XMLSchema"> <soap:Body> <GetPlaceList xmlns="http://tempuri.org/"> <placeName>redmond</placeName> <MaxItems xmlns="">10</MaxItems> <imagePresence>false</imagePresence> </GetPlaceList> </soap:Body> </soap:Envelope>
Proxy Code namespace TerraServer { using System.Xml.Serialization; . . . public class TerraService: System.Web.Services.Protocols.SoapClientProtocol { public TerraService() { this.Url = "http://terraweb/TerraNet/TerraService.asmx"; } [System.Web.Services.Protocols.SoapMethodAttribute ("http://tempuri.org/GetPlaceList"), System.Xml.Serialization.XmlArrayItemAttribute ("PlaceFacts", Form=System.Xml.Serialization.XmlForm.Unqualified, IsNullable=false)] public PlaceFacts[] GetPlaceList (string placeName, [System.Xml.Serialization.XmlElementAttribute ("MaxItems", Form=System.Xml.Serialization.XmlForm.Unqualified, IsNullable=false)] int maxItems, bool imagePresence) { object[] results = this.Invoke("GetPlaceList", new object[] {placeName, maxItems, imagePresence}); return (PlaceFacts[])(results[0]); } } }
Web Page Source Code <html> <script language="C#" runat=server> void SubmitBtn_Click(Object sender, EventArgs e) { TerraServer.TerraService ts = new TerraServer.TerraService(); CensusServer.CensusService cs = new CensusServer.CensusService(); LandmarkServer.LandmarkService ls = new LandmarkServer.LandmarkService(); TerraServer.PlaceFacts[] pf = ts.GetPlaceList(Place.Text, 10, false); ChosenPlace.Text = String.Format("Chosen place:<b> {0}, {1}, {2}</b>", pf[0].Place.City, pf[0].Place.State, pf[0].Place.Country); ChosenLonLat.Text = String.Format("(<b>Longitude = {0}, Latitude = {1}</b>)", pf[0].Center.Lon, pf[0].Center.Lat); // Continued next slide ...
Source Code (II) CensusServer.CensusFacts cf = cs.GetPoliticalUnitFactsByName (CensusServer.PoliticalUnit.City, pf[0].Place.City, pf[0].Place.State, 1990); CensusInfo.Text = String.Format("Population = {0} ({1} males & {2} females)", cf.Population1990, cf.Males, cf.Females); // . . . Landmark code here . . . } </script>
Source Code (III) <body> <center> <form method="post" runat="server"> Enter a place: <asp:textbox id="Place" runat="server"/> <asp:button type=submit text="Lookup" OnClick="SubmitBtn_Click" runat="server"/> <BR> <asp:label id="ChosenPlace" runat="server"/> <BR> <asp:label id="ChosenLonLat" runat="server"/> <p> <asp:label id="CensusInfo" runat="server"/> <BR><BR> <asp:label id="LandmarkInfo" runat="server"/> </form> </center> </body> </html>
Discussion of Videos • Discussion: What services are here • Discussion: What’s needed beyond just Web Services?
How Do I Find WebServices? • Take a look at http://uddi.org