Friday, October 26, 2012

REST vs. SOAP

REST vs. SOAP

Web Services are the key point of Integration for different applications belonging to different Platforms, Languages, systems. It wouldn’t be wrong if you call Web-services as the “Rendezvous point of the Business”.

I’ve been using HTTP and SOAP since several years new. REST is rather new. SOAP revolutionized RPC and loose coupling beyond the restrictions posed by earlier protocols. However off late I have been giving APIs and interfaces considerable thought and am leaning a lot more towards simple HTTP based APIs with an XML or JSON response format as opposed to SOAP. Let’s try to discuss all the aspects one by one.

Before we start, Let’s do a basic terminology headsup - SOAP refers to Simple Object Access Protocol HTTP based APIs refer to APIs that are exposed as one or more HTTP URIs and typical responses are in XML / JSON. Response schemas are custom per object REST on the other hand adds an element of using standrdized URIs, and also giving importance to the HTTP verb used (ie GET / POST / PUT etc)
Although, in alst ffew years we saw growth of large no. of Web Services, despite that the hype surrounding the SOAP has barely reduced. Internet architects have come up with a surprisingly good argument for pushing SOAP aside: there’s a better method for building Web services in the form of Representational State Transfer (REST).
REST is more of an old philosophy than a new technology. But a realization that came later in technology. Whereas SOAP looks to jump-start the next phase of Internet development with a host of new specifications, the REST philosophy espouses that the existing principles and protocols of the Web are enough to create robust Web services. This means that developers who understand HTTP and XML can start building Web services right away, without needing any toolkits beyond what they normally use for Internet application development.
In a RESTful architecture, the key resources are identified — Can be entities, collections, or anything else the designer seems worthy of having its own URI. The standard methods — in this case, the HTTP verbs — are mapped to resource-specific semantics. All resources implement the same uniform interface. The dimension of content-types, which allows for different representations of resources (e.g. in both XML, HTML, and plain text), as well as the possibility of links to resources in resource representations. Use your imagination — e.g. the GET on /customer/4711 would return a document that contains a link to a specific /order/xyz.
I am seeing a lot of new web services are implemented using a REST style architecture these days rather than a SOAP one. Lets step back a second and put some light on what REST is.

What is a REST Web Service
Representational State Transfer or REST basically means that each unique URL is a representation of some object. You can get the contents of that object using an HTTP GET, to delete it, you then might use a POST, PUT, or DELETE to modify the object (in practice most of the services use a POST for this).

How Popular is REST?
All of the major webservices on the Internet now use REST: Twitter, Yahoo’s web services use REST, others include Flickr, del.icio.us, pubsub, bloglines, technorati, and several others. Both eBay and Amazon have web services for both REST and SOAP.

and SOAP?
SOAP is mostly used for Enterprise applications to integrate wide types and no. of applications and another trend is to integrate with legacy systems, etc. On the Internet side of things — Google is consistent in implementing their web services using SOAP, with the exception of Blogger, which uses XML-RPC.

REST vs SOAP
The companies that use REST APIs haven’t been around for very long, and their APIs came out this year or last year mostly. So REST is definitely In-Vogue for creating a web service. But, lets face it — Use SOAP to wash, and you REST when your tired). The main advantages of REST web services are:

  • Lightweight – not a lot of extra xml markup

  • Human Readable Results

  • Easy to build – no toolkits required

  • SOAP also has some advantages:

  • Easy to consume – sometimes

  • Rigid – type checking, adheres to a contract

  • Development tools

  • Is SOAP Simple Object access really that simple ? I guess a misnomer! Let’s discuss all the point of comparisons –

    API Flexibility & Simplicity

    The key to the REST methodology is to write Web services using an interface that is already well known and widely used: the URI. For example, exposing a currency converter service, in which a user enters a currency quote symbol to return a real-time target currency price, could be as simple as making a script accessible on a Web server via the following URI:
    http://www.ExampleCurrencyBrokerage.com/convert?=us-dollar&value=100&target=pound

    Any client or server application with HTTP support could easily call that service with an HTTP GET command. Depending on how the service provider wrote the script, the resulting HTTP response might be as simple as some standard headers and a text string containing the current price for the given ticker symbol. Or, it might be an XML document.

    This interface method has significant benefits over SOAP-based services. Any developer can figure out how to create and modify a URI to access different Web resources. SOAP, on the other hand, requires specific knowledge of a new XML specification, and most developers will need a SOAP toolkit to form requests and parse the results.

    Bandwidth Usage – REST is Lighter

    Another benefit of the RESTful interface is that requests and responses can be short. SOAP requires an XML wrapper around every request and response. Once namespaces and typing are declared, a four- or five-digit stock quote in a SOAP response could require more than 10 times as many bytes as would the same response in REST.

    SOAP proponents argue that strong typing is a necessary feature for distributed applications. In practice, though, both the requesting application and the service know the data types ahead of time; thus, transferring that information in the requests and responses is gratuitous.

    How does one know the data types—and their locations in the response—ahead of time? Like SOAP, REST still needs a corresponding document that outlines input parameters and output data. The good part is that REST is flexible enough that developers could write WSDL files for their services if such a formal declaration was necessary. Otherwise, the declaration could be as simple as a human-readable Web page that says, “Give this service an input of some stock ticker symbol, in the format q=symbol, and it will return the current price of one share of stock as a text string.”

    Security
    Probably the most interesting aspect of the REST vs. SOAP debate is the security perspective. Although the SOAP camp insists that sending remote procedure calls (RPC) through standard HTTP ports is a good way to ensure Web services support across organizational boundaries. However, REST followers argue that the practice is a major design flaw that compromises network safety. REST calls also go over HTTP or HTTPS, but with REST the administrator (or firewall) can discern the intent of each message by analyzing the HTTP command used in the request. For example, a GET request can always be considered safe because it can’t, by definition, modify any data. It can only query data.

    A typical SOAP request, on the other hand, will use POST to communicate with a given service. And without looking into the SOAP envelope—a task that is both resource-consuming and not built into most firewalls—there’s no way to know whether that request simply wants to query data or delete entire tables from the database.

    As for authentication and authorization, SOAP places the burden in the hands of the application developer. The REST methodology instead takes into account the fact that Web servers already have support for these tasks. Through the use of industry-standard certificates and a common identity management system, such as an LDAP server, developers can make the network layer do all the heavy lifting.

    This is not only helpful to developers, but it eases the burden on administrators, who can use something as simple as ACL files to manage their Web services the same way they would any other URI.

    REST ain’t Perfect
    To be wise, REST ain’t perfect. It isn’t the best solution for every Web service. Data that needs to be secure should never be sent as parameters in URIs. And large amounts of data, like that in detailed purchase orders (POs), can quickly become cumbersome or even out of bounds within a URI.

    And when It comes to attachments, SOAP is a solid winner. SOAP can transport your all text adn BINaries without a glitch. In such cases, SOAP is indeed a solid solution. But it’s important to try REST first and resort to SOAP only when necessary. This helps keep application development simple and accessible.

    Fortunately, the REST philosophy is catching on with developers of Web services. The latest version of the SOAP specification now allows certain types services to be exposed through URIs (although the response is still a SOAP message). Similarly, users of Microsoft .NET platform can publish services so that they use GET requests. All this signifies a shift in thinking about how best to interface Web services.

    Developers need to understand that sending and receiving a SOAP message isn’t always the best way for applications to communicate. Sometimes a simple REST interface and a plain text response does the trick—and saves time and resources in the process.

    Type Handling
    SOAP provides relatively stronger typing since it has a fixed set of supported data types. It therefore guarantees that a return value will be available directly in the corresponding native type in a particular platform. Incase of HTTP based APIs the return value needs to be de-serialized from XML, and then type-casted. This may not represent much effort, especially for dynamic languages. Infact, even incase of copmlex objects, traversing an object is very similar to traversing an XML tree, so there is no definitive advantage in terms of ease of client-side coding.

    Client-side Complexity (Thinner Clients)
    Making calls to an HTTP API is significantly easier than making calls to a SOAP API. The latter requires a client library, a stub and a learning curve. The former is native to all programming languages and simply involves constructing an HTTP request with appropriate parameters appended to it. Even psychologically the former seems like much less effort.

    Testing and Troubleshooting
    It is also easy to test and troubleshoot an HTTP API since one can construct a call with nothing more than a browser and check the response inside the browser window itself. No troubleshooting tools are required to generate a request / response cycle. In this lies the primary power of HTTP based APIs

    Server-side Complexity
    Most Programming languages make it extremely easy to expose a method using SOAP. The serialization and deserialization is handled by the SOAP Server library. To expose an object’s methods as an HTTP API can be relatively more challenging since it may require serialization of output to XML. Making the API Rest-y involves additional work to map URI paths to specific handlers and to import the meaning of the HTTP request in the scheme of things. Offcourse many frameworks exist to make this task easier. Nevertheless, as of today, it is still easier to expose a set of methods using SOAP than it is to expose them using regular HTTP.

    Caching
    Since HTTP based / Rest-ful APIs can be consumed using simple GET requests, intermediate proxy servers / reverse-proxies can cache their response very easily. On the other hand, SOAP requests use POST and require a complex XML request to be created which makes response-caching difficult

    VERDICT
    In the end I believe SOAP isn’t that simple, it requires greater implementation effort and understanding on the client side while HTTP based or REST based APIs require greater implementation effort on the server side. API adoption can increase considerably if a HTTP based interface is provided. Infact, an HTTP-based API with XML/JSON responses represents the best of both breeds and is easy to implement on the server as well as easy to consume from a client.

    Tuesday, December 21, 2004

    Webservice Tools (Disco and wsdl -- .Net FrameWork Tools Series)

    All of us have worked with web services but very of us have worked with the command line tools provided by .Net framwork. Today we will discuss .Net Webservice Tools disco.exe and wsdl.exe.

    Disco.exe :-

    Discovey is the most important step when it comes to webservices. This tool discovers the URL's of XML webservices on a given web server. Discovery of webservices is important for its consumers as from the discovery document they can know about the various services they can consume.

    This tool locates the webservice. If it finds the webservice it creates .wsdl, .xsd, .disco, and .discomap files and if it does not then the error message displayed to the user.

    Let us use this tool to discover our webservice and create the above files:

    1) Go to the VS.NET command prompt

    2) Run disco.exe /? for checking out all the options

    3) This is a list of some important options :

    /nosave - Do not save the discovered documents or results to disk (for example wsdl, xsd and disco files). The default is to save the documents.

    /out: The output directory to save the discovered documents in. The default is the current directory. Short form is '/o:'.

    /username:

    /password:

    /domain: The credentials to use when the connecting to a server that requires authentication. Short forms are '/u:', '/p:' and '/d:'.

    /proxy:The url of the proxy server to use for http requests.The default is to use the system proxy setting.

    4) To discover the disco files of the webservice give the URL of the web service as

    disco.exe http://localhost/MyWebservice/service1.asmx /nosave .

    If the URL is found the following message will be displayed to you :

    Microsoft (R) Web Services Discovery Utility

    [Microsoft (R) .NET Framework, Version 1.0.3705.0]

    Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

    Disco found documents at the following URLs:

    http://localhost/MyWebservice/service1.asmx?disco

    http://localhost/MyWebservice/service1.asmx?wsdl

    5) We will now perform the same command without /nosave option to save the document in MyWebSerDocs folder. Please see to it

    that the directory exists as this tool does not create the directory

    disco.exe http://localhost/MyWebservice/service1.asmx /out:MyWebSerDocs

    6)You should see a similar output

    Microsoft (R) Web Services Discovery Utility

    [Microsoft (R) .NET Framework, Version 1.0.3705.0]

    Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

    Disco found documents at the following URLs:

    http://localhost/MyWebservice/service1.asmx?disco

    http://localhost/MyWebservice/service1.asmx?wsdl

    The following files hold the content found at the corresponding URLs:

    MyWebSerDocs\service1.disco <- http://localhost/MyWebservice/service1.asmx?disco

    MyWebSerDocs\service1.wsdl <- http://localhost/MyWebservice/service1.asmx?wsdl

    The file MyWebSerDocs\results.discomap holds links to each of these files.

    7) Check out the directory it should have all the above 3 files i.e. Service1.disco, Service1.wsdl and results.discomap

    8) Similarly you can discover any web service with this tool and create the .disco, .wsdl and discomap files.

    9) These files that we have generated with disco.exe can be used as an input to wsdl.exe to create our webservice proxy class.



    WSDL.exe :-

    This tool is used to generate webservice proxy class. This file contains all the details for its clients to use when they are interacting

    with the webservice.

    Let us create the webservice proxy class:

    1) Go to VS.Net command prompt

    2) Type wsdl.exe /? to view all the options

    3) I have listed a few important options below :

    - A url or path to a WSDL contract, an XSD schema or .discomap document.

    /language: - The language to use for the generated proxy class. Choose from 'CS','VB', 'JS' or provide a fully-qualified name for a class implementing

    System.CodeDom.Compiler.CodeDomProvider. The default is 'CS' (CSharp).

    /server Generate an abstract class for an xml web service implementation using ASP.NET based on the contracts. The default is to generate client proxy classes.

    /out:

    The filename for the generated proxy code. The default name is derived from

    the service name. Short form is '/o:'.

    /protocol: Override the default protocol to implement. Choose from 'SOAP', 'HttpGet','HttpPost', or custom protocol as specifiied in the configuration file.

    4) To generate the proxy class you can either use .wsdl or .discomap file that we have generated using the disco.exe or you can give the webservice url as input.

    5) We will create our proxy class using the .wsdl and .discomap files that we had generated.

    6) Type the following command :

    wsdl.exe Service1.wsdl

    or

    wsdl.exe results.discomap.

    Service1.cs class will be generated.

    7) You can use this proxy class with any of the client application to interact with the webservice.

    8) Create a client application say a Windows application and add the proxy class to it by choosing Add Existing Item and selecting out proxy class Service1.cs. Create the webservice object and call any method on it.

    Service1 myWebSer = new Service1();

    MessageBox.Show(myWebSer.HelloWorld());

    WebServices Series Part - 5 ( Sync & Async Communication )

    we saw how we can communicate with web services using callbacks. Today we will discuss the other two techniques Polling and Waithandles.

    Polling :

    When we made a call to our web service using callback we passed it reference to our fucntion and in return we get an IAsyncResult object back which acted as a token.

    In case of polling we will again call BeginCoSine method but not pass any parameter for callback. This method will return us an IAsyncResult object. Polling works on one of the properties of this object called IsCompleted. After calling the method we will have to continuous POLL for the value of this property. When the webservice is completed with its processing the value IAsyncResult object will be set to true.After that we call the EndCoSine function passing it our IAsyncResult object (which acts as a token) to retrieve our result.

    Add another button to your windows form and put in the below code :

    private void button3_Click(object sender, System.EventArgs e)
    {
    int noOfSec = 0;
    oIAsyncResult = trigoService.BeginCoSine(90,null,null);
    while(oIAsyncResult.IsCompleted == false)
    {
    noOfSec++;
    Thread.Sleep(1000);
    }
    MessageBox.Show("Completed in " + noOfSec);
    label1.Text = Convert.ToString(trigoService.EndCoSine(oIAsyncResult));
    }


    Here we are checking out the value of IsCompleted property in a while loop. After the value is set to true we message the number of seconds that were taken to POLL. Finally we call EndCosine function passing our IAsyncResult object.

    WaitHandles :

    This method also uses one of the properties of IAsyncResult object called AsyncWaitHandle.

    This approach is generally used when u dont want to leave the thread in which you are communicating with the webservice. Thus in this case you will wait for the webservice to complete its work without releasing the thread in which you were communicating. You can achieve this by using the WaitHandle returned by AsyncWaitHandle property of IAsyncResult object.

    After you have called the BeginCosine method and got your IAsyncResult Token object you can use its AsyncWaitHandle property to get WaitHandle object then use its WaitOne method to wait for the thread. WaitOne has multiple overloads some also take the amount of time you want to wait.

    Add another button to your windows form and add the following code snippet

    private void button4_Click(object sender, System.EventArgs e)
    {
    int noOfSec = 0;
    oIAsyncResult = trigoService.BeginCoSine(90,null,null);
    oIAsyncResult.AsyncWaitHandle.WaitOne();
    while(noOfSec++ < 5)
    {
    noOfSec++;
    Thread.Sleep(1000);
    }
    MessageBox.Show("Waited for " + noOfSec);
    label1.Text = Convert.ToString(trigoService.EndCoSine(oIAsyncResult));
    }

    You can also checkout the other static methods of the WaitHandle object like WaitAll and WaitAny. These methods are particularly used when you have multiple web service calls and you want to wait for either ALL of them to complete their processing and then move ahead or with ANY ONE of them to complete their processing and then move ahead.

    WAIT ALL :- Wait for all the web services to complete processing.
    WAIT ANY:- Wait for any one of them to complete processing.

    WebServices Series Part - 4 ( Sync & Async Communication )

    we will checkout how to consume our web service using SOAP in Synchronous and Asynchronous modes.

    To consume to our web service we need to create our web service client.

    Let us go ahead and create our web service client.

    Open a new windows application and Select References Right click and select Add Web Reference. Select our Trigonometry web service.

    WebReference to this web service will be added and along with it you will notice the reference.map , .disco and .wsdl files will be added for the web service. Our client interacts with our web service using a proxy class.

    To view the proxy class ... Select the solution in the Solution Explorer and click on Show All Files ... You will notice a file under the Reference map node called as Reference.cs.

    Open the Reference.cs file and Checkout the BASE CLASS for our webservice proxy class, it should be SoapHttpClientProtocol.

    You should also notice that for each method in our webservice there are three methods in the webservice proxy class.

    For Example these are the the three methods for Sine.

    public System.Double Sine(System.Double angel)
    public System.IAsyncResult BeginSine(System.Double angel, System.AsyncCallback callback, object asyncState)
    public System.Double EndSine(System.IAsyncResult asyncResult)

    Out of these three the first one is used for Synchronous communication and latter two for Asyncrhomous communication.

    Let us checkout Synchrounous Communication.

    Add a Form and add a button and a label to that Form

    Declare a class level variable for our webservice

    localhost.Service1 trigoService = new localhost.Service1();

    Write the following code in the button Click event

    private void button1_Click(object sender, System.EventArgs e)
    {
    label1.Text = Convert.ToString(trigoService.Sine(90));
    }

    Execute the apllication and checkout the results.

    One of methods of Asynchronous communication with web service is using Callbacks.

    To start with declare a class level variable for IAyncResult

    private IAsyncResult oIAsyncResult;

    To consume our web service in asynchronous mode let us add another button to our form and add the below code. When we communicate with our web service in a asynchronous mode we will have to call the BeginSine method and pass a CALLBACK function to it. After our web service is done with its processing it will callback our function to return the result.

    private void button2_Click(object sender, System.EventArgs e)
    {
    oIAsyncResult = trigoService.BeginSine(90,new AsyncCallback(CallBackForEndSine),null);
    }

    public void CallBackForEndSine(IAsyncResult oIAsyncResult)
    {
    double sineVal = trigoService.EndSine(oIAsyncResult);
    label1.Text = Convert.ToString(sineVal);
    }

    Here I am calling the BeginSine method and passing it the reference of our CallBack method CallBackForEndSine. You should notice that on Calling the BeginSine method I am returned an IAysncResult object. This object acts as a TOKEN between the client and the webservice. Hence when the Webservice is through with its processing and Callback method is called, to get the result from the webserivce we need to call EndSine method in the CallBack method passing IAsyncResult Token.

    The Difference between Asynchronous & Synchronous consumption of webservices can be noticed when there is a lot of processing done by a webservice. Thus if our webservice is consumed in a synchronous mode then our clients will have to wait for the processing to complete.

    To get a good taste of Asynchronous processing with our webservice we will make a little change in our methods. Before returning the result we will make the thread sleep for 20 secs.


    public double Sine(double angel)
    {
    Thread.Sleep(20000); // make a similar change in all the methods
    return System.Math.Sin(angel);
    }

    Compile the webservice. Update the webservice reference in your Client project and Execute your Client.

    Now if our web service is consumed synchrounsouly our client will have to wait for the result. Click on Button1 and check it out. The control will come back after 20 secs and till then your application will not respond to any other events. Now let us consume the webservice in an ansychronous mode by clicking on Button2. Since the communication is asynchronous the control will return back immediately rather than waiting for 20 seconds. Later the call back function will be called to deliver the results.

    WebServices Series Part - 3 (Accessing with HTTP POSTLOCALHOST, POST and GET)

    we will look how we can access web services with HTTP-GET and HTTP-POST protocols.


    .Net Framework 1.0 had all the three protocols i.e. HTTP-POST,HTTP-GET AND HTTP-SOAP enabled by default. But in version 1.1 only HTTP-SOAP is enabled by default. Hence if you want to access the web service my HTTP-POST or GET then you will have to enable it explicitly.



    Since HTTP-POST is not enabled by default in .Net framework 1.1 to test web service on the local machine there is a new protocol that has been added i.e. HTTPPOSTLOCALHOST. It is because of this protocol that while testing a web service you are able to view a test form on your local machine and not on a remote computer.


    Let us check out each of these protocols individually: -



    HTTPPOSTLOCALHOST: - This is enabled by default in .Net Framework version 1.1. Hence if you access your website as http://localhost you will get the test form but if you test the same web service with http:// from some other computer than the test form will not be displayed.



    e.g. Try and access our trigonometry web service from your machine and then access it from any other machine. You will notice that the test form will not be displayed.



    Before we look at HTTP-POST and HTTP-GET we need to checkout DefaultWsdlHelpGenerator.aspx.



    DefaultWsdlHelpGenerator.aspx is the default wsdl help file located at



    :\WINNT\Microsoft.NET\Framework\v1.1.4322\CONFIG.



    Open DefaultWsdlHelpGenerator.aspx and checkout showPost flag.


    bool showPost = true; // search for this



    Depending the value of showPost flag HTTP-GET or HTTP-POST protocol is used.Set its value to true if you want to see a POST test form and false to view a GET test form.



    Along with this you will have to include the appropriate protocols in the web services tag in Web.config.



    HTTP-POST: - To enable our web service with HTTP-POST we will have to make the following changes.



    1. Open the web.config and add the tag under as follows to enable the HTTP-POST protocol.











    2. Open DefaultWsdlHelpGenerator.aspx and set the value of showPost to true. By default it is set to true.
    3. Run the web service and checkout the HTTP-POST form.
    4. On execution you will see the xml output of your webservice. Checkout the URL of output in the browser it will not show any querystring parameters.



    http://namrathas/Trigonometery/MyService.asmx/CoSine



    5. Execute your web service from some other computer and you will be able to view HTTP-POST test form.



    HTTP-GET: - To enable our web service with HTTP-GET we will have to make the following changes.



    1. Open the web.config and add to protocols tage s follows to enable our webservice with HTTP-GET protocol also.












    2. Open DefaultWsdlHelpGenerator.aspx and set the value of showPost to false. So now HTTP-POST form will be displayed. All the parameters will be passed as querystring.



    http://namratas/Trigo/MyService.asmx/CoSine?angel=5



    3. Run the web service and checkout the HTTP-GET form. Its looks the same as HTTP-POST form. You will notice the difference on execution.
    4. Execute the web service and you will see the xml output of your webservice. Checkout the URL of output in the browser it will show query string parameters passed to the web method.



    HTTP-SOAP: - Whenever our webservice is accessed from any application HTTP-SOAP protocol is used. HTTP-SOAP is enabled by default. We will look in to more details of HTTP-SOAP when we look into consuming webservices.



    Hence if want our webservice to be enabled for all the four protocols we need to add them in the tag.












    WebServices Series Part - 2 (WebMethod and WebService Properties)

    After creating our basic web service yesterday we will look into the web method properties and supported data types.

    Well Whenever we create a web service all those methods of the web service which we want to be web-callable have to be marked with the web method attribute and should have a public access modifier. Web Method Attribute is basically used to make a method web callable.

    WebMethod attribute has the following properties like :-


    * Description
    * TransactionOption
    * CacheDuration
    * BuffferResponse
    * EnableSession
    * MessageName




    Description :- It is used for commenting the web method. The description that is added here is included in WSDL. Add the

    webmethod description property above any method as shown below. When you call the webservice from a browser you view the web service.



    e.g. [WebMethod(Description = "My WebMethod Description comes here")]



    Enable Session :- WebServices are a type of ASP.NET applications hence you can access your ASP.NET application Session from your webservice. By default the session is disabled i.e. set to false. You can enable it by setting its value to true.



    e.g. [WebMethod(EnableSession ="true")]



    Message Name :- Message Name property is used give an alias to a method name. This is esp. useful when you are using overloaded methods.



    e.g. Yesterday we created 3 method in our webservice for cos,sin and tan which took double as an input parameter. Today we will create their overloads that will take int as an input parameter


    [WebMethod]
    public double Tan(int angel)
    {
    return System.Math.Tan(angel);
    }

    [WebMethod]
    public double Sine(int angel)
    {
    return System.Math.Sin(angel);
    }

    [WebMethod]
    public double CoSine(int angel)
    {
    return System.Math.Cos(angel);
    }



    If you compile this code it will compile successfully. It is on execution that you we view the below error message



    Error Message: Both Double Tan(Int32) and Double Tan(Double) use the message name 'Tan'. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.



    Web service needs to identify each method uniquely hence we will give alias to the methods by setting the MessageName property.



    e.g. [WebMethod(MessageName = "SinInt")]
    [WebMethod(MessageName = "CosInt")]
    [WebMethod(MessageName = "TanInt")]

    TransactionOption : - If our web service is a part if any COM/MTS transaction application then we can enable the Transaction Option of the webservice thus making it a part of the 2 phase commit protocol of MTS.



    e.g. [WebMethod(TransactionOption = "Supported")]



    The dafault is same as COM/MTS transactions i.e. Required.



    CacheDuration :- Like any other ASP.NET applications WebServices also support output caching. Output caching gives a performance boost coz the same request is not executed repeatedly instead the result is return from the cache itself. This feature is

    esp. useful when you are retrieving chunks of data from the database for listing and other purposes. But caching should preferably used with only those methods where we know that the data will not change frequently.



    e.g. [WebMethod(CacheDuration = "180")]

    This means the output of this method will be cached for seconds.



    BuffferResponse :- We all are familiar with the buffering property rite from our ASP days where used Response.Buffer = true so that

    the response is only sent when the Response for the request is completely generated or when the buffer is full. WebServices also use the same logic. If we want our response to be sent across only after its complete processing is done we need to buffer our response by setting its property to true.



    e.g. [WebMethod(BufferResponse = "true")]



    By default it is set to true.



    These properties were webmethod properties. We also have some web service properties like



    * Description.
    * Name
    * NameSpace



    Description :- This is same as web method property. It is used to comment web service and give it a description that is included in WSDL.



    e.g. [WebService(Description = "My WebService Description comes here")]
    public class MyService : System.Web.Services.WebService

    {
    Name :- By default the name of the class is used as the service name but incase we want to give a different service name which will to exposed to our clients and in WSDL we can set the Name property. This property will override the default value i.e. the class name.



    e.g. [WebService(Name = "MyNewName")]
    public class MyService : System.Web.Services.WebService
    {



    NameSpace :- The default name space is http://tempuri.org. You can change this to http://localhost/MyWebServices. You should set namespace to any unique value.



    e.g. [WebService(Namespace = "http://localhost/MyWebServices")]
    public class MyService : System.Web.Services.WebService
    {

    WebServices Series Part - 1

    WebServices Series Part - 1