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
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.
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.
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
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
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.
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
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
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.
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.
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.
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
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.
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)
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)
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” />
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>
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.