Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web Services Essentials

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for sivaraj sivaraj
July 02, 2012
89

Web Services Essentials

Prepared for Developers

Avatar for sivaraj

sivaraj

July 02, 2012
Tweet

Transcript

  1. 2 © Mahindra Satyam 2009 Agenda  Introduction – Service

    Oriented Architecture  .NET Web Service basics – Creating a Web Service – Discovering Web Services – Determining Web Service Functionality – Testing a Web Service Method – Building a client to Use a Web Service  Simple Object Access Protocol (SOAP)  Publishing and consuming Web Services – Defining a web service – Building a web service in Visual Studio – Deploying the Web Service – Creating a client to consume the web service – Consuming the Web service  User-defined types in Web Services
  2. 3 © Mahindra Satyam 2009 Introduction A web service is

    a class that allows its methods to be called by methods on other machines via common data formats and protocols, such as XML and HTTP. A web service promotes software reusability in distributed systems where applications execute across multiple computers on a network. In .NET the over-the-network methods calls are generally implemented through the SOAP (Simple Object Access Protocol) SOAP is an XML-based protocol describing how to mark up requests and responses so that they can be transferred via protocols such as HTTP.
  3. 4 © Mahindra Satyam 2009 Service Oriented Architecture  Web

    Services refers to the technologies that allow for making connections.  Services are what you connect together using Web Services.  A service is the endpoint of a connection.  A combination of services—internal and external to an organization—make up a service-oriented architecture.  Also known as a composite application.  A composite application is created by combining services.  Composite applications are built using a service-oriented architecture.
  4. 6 © Mahindra Satyam 2009 .NET Web Service Basics A

    web service is a software component stored on one machine that can be accessed by an application or other software component on another machine over a network. The machine on which the Web Service resides is the remote machine The application that accesses the web service is the client
  5. 7 © Mahindra Satyam 2009 Creating a web service 

    Create a project of type ASP.NET Web Service  Solution contains – Files to contain the Web Service code (which implements the web service) – ASMX file (which provides access to the web service) – DISCO file (which clients use to discover the Web service)  Methods in a web service are invoked through a Remote Procedure Call (RPC)  The methods are marked with the WebMethod attribute – which makes them accessible to other classes through RPCs. This is called as exposing a method
  6. 8 © Mahindra Satyam 2009 Discovering Web Services  After

    a web service is implemented, compiled and deployed on a Web Server, client application can consume the Web Service.  The client uses the .disco file to discover the web service and know its capabilities  Discovery of Web Services (DISCO) is a Microsoft-specific technology used to located web services on a server.  DISCO files consist of XML markup that describes the location of the Web services to the client.  A .disco file is accessed via a Web Service’s ASMX page and contains the markup specifying references to the documents that define various Web Services  Resulting data returned from accessing a .disco file is placed in .discomap file.
  7. 9 © Mahindra Satyam 2009 Determining a Web Service’s functionality

     For the client to determine the web service’s functionality, Web services contain a service description  This is an XML document that conforms to the Web Service Description Language (WSDL)  WSDL document defines the methods exposed by the web service and how client must use them  Also specifies required formats for requests and responses.  WSDL is generated dynamically when a client requests the web service’s WSDL description – (i) By appending ?WSDL to the ASMX file’s URL – (ii) By clicking the Service Description link in the ASMX file
  8. 10 © Mahindra Satyam 2009 Testing a Web Service’s methods

     In the ASMX page, clicking on the method name requests a test page that describes the method  Test page allows users to test the method by clicking on the Invoke button  Page displays the sample request and response messages using SOAP and HTTP POST  The protocol that transmits request and response messages in Web Services is called wire format or wire protocol  SOAP is commonly used wire format as it can be sent using several transport protocols. HTTP POST requires HTTP protocol
  9. 11 © Mahindra Satyam 2009 Building a client to use

    the Web Service  Client can be Console App, WinForm App or Web App  Enable client to consume Web Service by adding a reference to the client  Visual Studio accesses the WSDL information and copies into a WSDL file stored in the client’s Web References folder.  WSDL information is used to create a proxy class which handles the method call (i.e. networking details and formation of the SOAP messages)  When client app calls the Web method, the application actually calls a corresponding method in the proxy class. This method has the same name and parameters as the Web method but formats the call to be sent as a request in a SOAP message  Web service receives the request as a SOAP message, executes the method call and returns the result as another SOAP message.  When the client application receives the SOAP message, the proxy class deserializes it and returns the results as the return value of the web method that was called.
  10. 12 © Mahindra Satyam 2009 Simple Object Access Protocol (SOAP)

     SOAP is a platform-independent protocol that uses XML to make remote procedure calls over HTTP  Each request and response is packaged in a SOAP message  SOAP messages are platform independent and human-readable  SOAP types include primitive types (e.g. Integer) as well as DateTime, XmlNode, etc  SOAP can also transmit arrays, DataSets and user defined types  The client invoking the Web method sends the request and related information packaged in a SOAP message.  Web Service processes the contents contained inside a SOAP envelope and interprets the SOAP message’s contents (parsing)  After the web service sends the response to the client in another SOAP message, the client parses the response to retrieve the result of the method call.
  11. 13 © Mahindra Satyam 2009 SOAP Request POST /InStock HTTP/1.1

    Host: www.example.org Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.example.org/stock"> <m:GetStockPrice> <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope>
  12. 14 © Mahindra Satyam 2009 SOAP Response HTTP/1.1 200 OK

    Content-Type: application/soap+xml; charset=utf-8 Content-Length: nnn <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.example.org/stock"> <m:GetStockPriceResponse> <m:Price>34.5</m:Price> </m:GetStockPriceResponse> </soap:Body> </soap:Envelope>
  13. 15 © Mahindra Satyam 2009 Defining the Web Service 

    The WebService attribute attached to a Web Service class declaration allows to specify a namespace(preferably company URL for uniqueness) and description (appears in the ASMX file)  Web Service must conform to Basic Profile 1.1 (BP 1.1) developed by the Web Services Interoperability Organisation (WS-I) dedicated to promoting interoperability among web services developed on different platforms with different programming languages.  Setting the WebServiceBinding attribute’s ConformsTo property to WsiProfiles.BasicProfile1_1 generates WSDL and ASMX files in conformance with the guidelines specified in BP 1.1  Web Service class inherits from System.Web.Services.WebService  The methods that are to be exposed must have the WebMethod atribute.
  14. 16 © Mahindra Satyam 2009 Web Method Attributes  BufferResponse:

    When set to True, the response from the XML Web Service is held in memory and sent as a complete package. If False (default) the response is sent to client as it is constructed on the server  CacheDuration: Specifies the number of seconds that the response should be held in the system’s cache. Default=0  Description: Allows ASMX page to display information about the method.  EnableSession: True enables session state for the method. Default=False  MessageName: Applies a unique name to the WebMethod; required if methods are overloaded  TransactionOption: Specifies transactional support for the WebMethod. Default=Disabled
  15. 17 © Mahindra Satyam 2009 The ASMX file The .asmx

    file contains a @WebService Page directive which has four possible attributes:  Class: Required! Specifies the class used to define the methods and data types visible to the XML Web Service Clients  CodeBehind: Required only when you are working with an XML web service using the code-behind model. Takes a string value that represents the physical location of the class file containing the web service logic. It is normally stored in the App_Code folder  Debug: Optional! Either True (compiled with debug symbols in place) or False.  Language: Required! Specifies the language used for the web service.
  16. 18 © Mahindra Satyam 2009 Creating the Web Service Steps:

     Create ASP.NET Web Service Project  Modify the code-behind file. Rename the class, method and file name if necessary  Modify the ASMX file and rename file name (Demo)
  17. 19 © Mahindra Satyam 2009 Deploying the Web Service 

    Build Web Site  Test  Deploy (Demo)
  18. 20 © Mahindra Satyam 2009 Creating a client to consume

    the Web Service  In client application, add web reference of the Web Service by entering the URL  Proxy class is created automatically  In code, create an instance of the proxy class and call its method  New web services can be located using these technologies: – UDDI (Universal Description, Discovery and Integration) and – DISCO (Discovery of Web Services)
  19. 21 © Mahindra Satyam 2009 Consuming the Web Service 

    In the client, instantiate a proxy object to access the web service methods. (Demo)
  20. 22 © Mahindra Satyam 2009 Transport Protocols for Web Services

    Three wire formats are available to web services:  HTTP-GET  HTTP-POST  SOAP (default format)
  21. 23 © Mahindra Satyam 2009 Using HTTP-GET Enables to send

    the request along with any required parameters as a Query String. E.g., http://www.mysite.com?methodArgs=value To enable HTTP-GET, create <protocols> section in the web.config file to add the protocol. <system.web> <webServices> <protocols> <add name = “HttpGet” />
  22. 24 © Mahindra Satyam 2009 Using HTTP-POST  Similar to

    HTTP-GET in that a name/value pair is sent to the server for processing.  HTTP-POST places the name/value pair within a request header which is not visible unlike HTTP-GET <html> <head> </head> <body> <form method = “post” action = http://www.mysite.com/method > <p><input type=“submit” value = “call service” > </p>
  23. 25 © Mahindra Satyam 2009 Session Tracking  Setting WebMethod

    property EnableSession to True indicates that session information should be maintained and should be accessible to this method.
  24. 26 © Mahindra Satyam 2009 User defined types Serialization: 

    Custom types that are sent to or from a Web Service are serialized, enabling them to be passed in XML format. This process is called XML serialization Requirements: Classes that are used to specify return types and parameter types for Web Methods must meet following requirements:  Public default constructor must be present  Properties and instance variables that should be serialized in XML format must be declared public  Properties that should be serialized must provide both get and set accessors even if empty bodied. Read-only properties are not serialized.