200 likes | 379 Views
ASP.NET. Web Services. Web Services. A unit of managed code installed under IIS that can be remotely invoked using HTTP. Similar Technologies. DCOM CORBA EJB (Enterprise Java Beans) All are tied to a specific language or a proprietary protocol. Introducing Web Services.
E N D
ASP.NET Web Services
Web Services • A unit of managed code installed under IIS that can be remotely invoked using HTTP.
Similar Technologies • DCOM • CORBA • EJB (Enterprise Java Beans) • All are tied to a specific language or a proprietary protocol.
Introducing Web Services • Language Agnostic. • Can be called by any program that can parse XML and use HTTP. • Can be called from Web Forms as well as Windows Forms.
Supporting Technologies • Web Service Description Language (WSDL) • A wire protocol (HTTP GET, HTTP POST, or SOAP) • A discovery Service (*.disco files) • Proxies • IIS
Building a Simple Web Service • Select C# Projects->ASP.NET Web Service. • Automatically creates a virtual directory under IIS. • Can test from within the program without using a client.
WebService Base Class • Not required to derive from this class, can inherit directly from object. • Adds functionality such that a web service can interact with the same types used by the ASP.NET object model: • Application Object • Context Object • Server Object • Session Object • User Object
WSDL • Web Service Description Language • Contract between the client and the web service, that defines the interaction • Function names. • Parameters and their types. • Syntax of various wire protocols. • System.Web.Services.Description contains types that allow you to programmatically manipulate the WSDL.
SOAP • Simple Object Access Protocol • XML Based • Types • ADO.NET DataSets • Complex Arrays • Custom Types • Binding: </binding> <service name="HPSIClaimServices"> <port name="HPSIClaimServicesSoap" binding="s0:HPSIClaimServicesSoap"> <soap:address location="http://localhost/HPSIClaimServices/HPSIClaimServices.asmx" /> </port> </service>
Proxies • Creates a class that contains the interface for your web service • Using wsdl.exe C:\wsdl.exe out:c:\testproxy.cs http://localhost/testwebservice/text.asmx?wsdl • Using Visual Studio .NET • Add Web Reference
Proxies • Adds Asynchronous and Synchronous versions of each exposed Web Method • Looks and feels like the Web Service is a class in the client application
Advanced • Serializing Custom Types • Using Session to maintain information from previous calls • Connecting to a Database
Serializing Custom Types using System; using System.Xml.Serialization; namespace CarsWebService { //Tells the serializer to generate the proper XML tags for a cartype [XmlInclude(typeof(Car))] public class Car { public string name; public int maxSpeed; public Car(){} public Car(string n, int s) { name = n; maxSpeed = s; } } } //The XML Serializer will only serialize public members or properties that expose those members
Session State namespace TestService { public class TestService1 : System.Web.WebService { public TestService1() { InitializeComponent(); Session.Add(“TestDataSet”, null); } [WebMethod] public void TestMethod(DataSet ds) { Session[“TestDataSet”] = ds; } } }
Database Connections • Allows for Database client software to reside on one machine. • Allows for one consistent interface to the database. • Allows the business rules to be separate from the client
Discovery Service Protocol • Describes each Web Service in a given virtual directory. • .disco file is part of the client project • C:\disco <URL for .disco file> <?xml version="1.0" encoding="utf-8"?> <discovery xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.xmlsoap.org/disco/"> <contractRef ref="http://161.28.113.202/HPSIBuildRunServices/BuildRunService.asmx?wsdl" docRef="http://161.28.113.202/HPSIBuildRunServices/BuildRunService.asmx" xmlns="http://schemas.xmlsoap.org/disco/scl/" /> <soap address="http://161.28.113.202/HPSIBuildRunServices/BuildRunService.asmx" xmlns:q1="hpsiserver.hpsionline.com" binding="q1:BuildRunServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" /> </discovery>
Distributing over a network Web Form SQL Database Web Service SOAP Windows Form
Creating a Web Service Project • Choose ASP.NET Web Service • The project is created in C:\Inetpub\wwwroot\projectname • The web service file is .asmx • Code behind is in .asmx.cs file • IIS must be installed
Using a Web Service • Add a web reference • The proxy is hidden in the Web Reference • Create an instance of the proxy class • Use the proxy instance as you would a normal class instance
Conclusion • Web Services are cool! • Can be used to allow dissimilar systems to communicate • Easy to implement • Easy to learn how to use • Fairly Slow