Web Services

  • In this article you will learn:

    • What a Web service is.
    • How to create Web services.
    • The important part that XML and the XML-based Simple Object Access Protocol play in enabling Web services.
    • The elements that comprise Web services, such as service descriptions and discovery files.
    • How to create a client that uses a Web service.
    • How to use Web services with Windows applications and Web applications.
    • How to use session tracking in Web services to maintain state information for the client.
    • How to pass user-defined types to a Web service.
  • What a Web service is.

    • 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
    • In .NET, the over-the-network method calls are commonly implemented through the Simple Object Access Protocol (SOAP), an XML-based protocol describing how to mark up requests and responses so that they can be transferred via protocols such as HTTP
  • .NET Web Services 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 referred to as a remote machine. The application (i.e., the client) that accesses the Web service sends a method call over a network to the remote machine, which processes the call and returns a response over the network to the application
    • Web services and SOAP are platform and language independent, so companies can collaborate via Web services without worrying about the compatibility of their hardware, software and communications technologies
    • To create a Web service in Visual Web Developer, you first create a project of type ASP.NET Web Service. Visual Web Developer then generates the following:

      • files to contain the Web service code (which implements the Web service)
      • an ASMX file (which provides access to the Web service)
      • a DISCO file (which potential clients use to discover the Web service): Discovery of Web services
    • Declaring a method with attribute WebMethod makes the method accessible to other classes through RPCs and is known as exposing a Web method
    • Four types of DISCO files facilitate the discovery process: .disco files, .vsdisco files, .discomap files and .map files
    • DISCO files consist of XML markup that describes for clients the location of Web services. A .disco file is accessed via a Web service’s ASMX page and contains markup specifying references to the documents that define various Web services. The resulting data that is returned from accessing a .disco file is placed in the .discomap file.
    • When a potential client requests a .vsdisco file, XML markup describing the locations of Web services is generated dynamically, then returned to the client. First, the .NET Framework searches for Web services in the directory in which the .vsdisco file is located, as well as that directory’s subdirectories. The .NET Framework then generates XML (using the same syntax as that of a .disco file) that contains references to all the Web services found in this search. Note that a .vsdisco file does not store the markup generated in response to a request. Instead, the .vsdisco file on disk contains configuration settings that specify the .vsdisco file’s behavior. developers can specify in the .vsdisco file certain directories that should not be searched when a client requests a .vsdisco file
    • Web services created using ASP.NET contain the functionality to generate a .disco file when it is requested. This .disco file contains references only to files in the current Web service. Thus, a developer typically places a .vsdisco file at the root of a server; when accessed, this file locates the .disco files for Web services anywhere on the system and uses the markup found in these .disco files to return information about the entire system
    • After locating a Web service, the client must determine the Web service’s functionality and how to use it. For this purpose, Web services normally contain a service description. This is an XML document that conforms to the Web Service Description Language (WSDL) an XML vocabulary that defines the methods a Web service makes available and how clients interact with them. The WSDL document also specifies lower-level information that clients might need, such as the required formats for requests and responses
    • WSDL documents are not meant to be read by developers; rather, WSDL documents are meant to be read by applications, so they know how to interact with the Web services described in the documents
    • When viewed in a Web browser, an ASMX file presents Web method descriptions and links to test pages that allow users to execute sample calls to these methods
    • ASP.NET generates WSDL information dynamically rather than creating an actual WSDL file
    • The protocol that transmits request-and-response messages is also known as the Web service’s wire format or wire protocol, because it defines how information is sent "along the wire." SOAP is the more commonly used wire format, because SOAP messages can be sent using several transport protocols, whereas HTTP POST must use HTTP
    • Using the ASMX page of a Web service to test and debug methods can help you make the Web service more reliable and robust
    • You can enable a client application to consume a Web service by adding a Web reference to the client. This process adds files to the client application that allow the client to access the Web service
    • The WSDL information is used to create a proxy class, which handles all the "plumbing" required for Web method calls i.e., the networking details and the formation of SOAP messages)
    • Whenever the client application calls a 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 that is being called, but formats the call to be sent as a request in a SOAP message. The Web service receives this request as a SOAP message, executes the method call and sends back the result as another SOAP message. When the client application receives the SOAP message containing the response, the proxy class deserializes it and returns the results as the return value of the Web method that was called
  • Simple Object Access Protocol (SOAP):

    • The Simple Object Access Protocol (SOAP) is a platform-independent protocol that uses XML to make remote procedure calls, typically over HTTP. Each request and response is packaged in a SOAP message an XML message containing the information that a Web service requires to process the message. SOAP messages are written in XML so that they are human readable and platform independent. Most firewalls security barriers that restrict communication among networks do not restrict HTTP traffic. Thus, XML and HTTP enable computers on different platforms to send and receive SOAP messages with few limitations
    • When a program invokes a Web method, the request and all relevant information are packaged in a SOAP message and sent to the server on which the Web service resides. When the Web service receives this SOAP message, it begins to process the contents (contained in a SOAP envelope), which specify the method that the client wishes to execute and any arguments the client is passing to that method. This process of interpreting a SOAP message’s contents is known as parsing a SOAP message. After the Web service receives and parses a request, the proper method is called with the specified arguments (if there are any), and the response is sent back to the client in another SOAP message. The client parses the response to retrieve the result of the method call
  • Publishing and Consuming Web Services:

    • class WebService, provides members that are useful in determining information about the client and the Web service itself
    • methods that are tagged with the WebMethod attribute exposes a method so that it can be called remotely
    • Specify a namespace for each Web service so that it can be uniquely identified by clients. In general, you should use your company’s domain name as the Web service’s namespace, since company domain names are guaranteed to be unique
    • No method with the WebMethod attribute can be declared static for a client to access a Web method, an instance of that Web service must exist
    • To access the Web service from another computer in your company’s or school’s local area network, you can replace host with the actual name of the computer on which IIS is running
    • Web servers typically receive requests on port 80
    • Adding web services to the consumer computer and make an object from it then using it
  • Web Services Session Tracking:

    • Setting property EnableSession to TRue indicates that session information should be maintained and should be accessible to this method. This is required only for methods that must access the session information. Doing so allows the Web service to use an HttpSessionState object (named Session by ASP.NET) to maintain the deck of cards for each client application that uses this Web service
  • Using Web Services with Web Applications
  • Using user-defined types

    • The Web methods we have demonstrated so far all received and returned simple type values. It is also possible to process user-defined typesknown as custom typesin a Web service. These types can be passed to or returned from Web methods. Web service clients also can use these user-defined types, because the proxy class created for the client contains the type definitions
    • We mentioned earlier that all types passed to and from Web services must be supported by SOAP. How, then, can SOAP support a type that is not even created yet? Custom types that are sent to or from a Web service are serialized, enabling them to be passed in XML format. This process is referred to as XML serialization
    • Classes that are used to specify return types and parameter types for Web methods must meet several requirements:

      • They must provide a public default or parameterless constructor. When a Web service or Web service consumer receives an XML serialized object, the .NET Framework must be able to call this constructor as part of the process of deserializing the object (i.e., converting it back to a C# object)
      • Properties and instance variables that should be serialized in XML format must be declared public. (Note that the public properties can be used to provide access to private instance variables.)
      • Properties that should be serialized must provide both get and set accessors (even if they have empty bodies). Read-only properties are not serialized
  • Wrap Up:

This chapter introduced ASP.NET Web services a technology that enables users to request and receive data via the Internet and promotes software reusability in distributed systems. You learned that a Web service is a class that allows client machines to call the Web service’s methods remotely via common data formats and protocols, such as XML, HTTP and SOAP. We discussed several benefits of this kind of distributed computinge.g., clients can access certain data on remote machines, and clients lacking the processing power necessary to perform specific computations can leverage remote machines’ resources

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s