Search This Blog

2012/04/22

Web Method Overloading


     We usually do method overloading with our normal csharp classes yet when we casually try to overload a web method we come across an error while consuming it.Today we will take this matter to logical end as we are not in hurry to complete the task.

Lets add a webservice to your website add two method to add integers as follows

[WebMethod]
    public int AddInt(int num1,int num2) {
        return num1 + num2;
    }

[WebMethod]
    public int AddInt(int num1, int num2,int num3)
    {
        return num1 + num2+ num3;
    }

Code in your aspx page should look similar to

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Math : System.Web.Services.WebService {

    public Math () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public int AddInt(int num1,int num2) {
        return num1 + num2;
    }
    [WebMethod]
    public int AddInt(int num1, int num2,int num3)
    {
        return num1 + num2+ num3;
    }
}
Now compile code you will not get any error, make this asmx page as start page and run it you will be greeted with following error

Both Int32 AddInt(Int32, Int32, Int32) and Int32 AddInt(Int32, Int32) use the message name 'AddInt'.  Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.


Now let’s find what went wrong

Error ask us to give different MessageName to overloaded methods lets do the same

[WebMethod(MessageName="AddTwoInts")]
    public int AddInt(int num1,int num2) {
        return num1 + num2;
    }
[WebMethod(MessageName = "AddThreeInts")]
    public int AddInt(int num1, int num2,int num3)
    {
        return num1 + num2+ num3;
    }

Modify your code to accommodate different MessageName to overloaded methods re run

Again we got some error message this time bit long

Service 'Math' does not conform to WS-I Basic Profile v1.1. Please examine each of the normative statement violations below. To turn off conformance check set the ConformanceClaims property on corresponding WebServiceBinding attribute to WsiClaims.None.
R2304: Operation name overloading in a wsdl:portType is disallowed by the Profile. A wsdl:portType in a DESCRIPTION MUST have operations with distinct values for their name attributes. Note that this requirement applies only to the wsdl:operations within a given wsdl:portType. A wsdl:portType may have wsdl:operations with names that are the same as those found in other wsdl:portTypes.
 -  Operation 'AddInt' on portType 'MathSoap' from namespace 'http://tempuri.org/'.
To make service conformant please make sure that all web methods belonging to the same binding have unique names.

After reading this bulky error message it can be concluded that finger is pointed toward following line in our code

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

WS-I-BASIC PROFILE 1.0 specification seems to not allowing our beloved method overloading lets not confirm to it.
Modify this line as bellow

[WebServiceBinding(ConformsTo = WsiProfiles.None)]

Now rerun now you should be able to consume this webservice without any error which has method overloading.

Now let’s analyze why we have to improvise our code?

    The WS-I organization (Web Services Interoperability Organization) publishes non-proprietary Web services specifications to promote the interoperability of Web services across platforms. WsiProfiles enumeration is used by the ConformsTo property of the WebServiceBindingAttribute attribute to indicate the WSI specification to which the Web service claims to conform.
     WS-I Basic Profile 1.0 specification relates to conformant use of WSDL to describe a web service. To go deep into this standard read reverences bellow.

References:

2012/04/21

SOAP for Webservices- Quick Introduction


Quite often than not IT infrastructure of an organization is made up of multiple vendor providing solutions using multiple technologies, Lot of Replication of data stores and functionality is required for communication between these multiple vendor systems to alleviate this XML standard was introduced.
   Though XML provide flexibility to structure data for developer but for better communication between disparate systems some rules for structuring data in xml document are needed, such rules will pave a way higher degree of automation in communication. Simple Object Access Protocol (SOAP) provides the rule that will
Help to standardize the communication using xml document.
    SOAP let us to expose and consume complex data structures e.g. dataset or table.


More about SOAP
     "Simple Object Access Protocol" was designed to be a platform and language-neutral alternative to previous middleware technologies like CORBA and DCOM. SOAP was also designed from the ground up to be extensible, so that other standards could be integrated into it
A SOAP message is an ordinary XML document containing the following elements.
·         Envelope: (Mandatory)
defines the start and the end of the message.
·         Header: (Optional )
Contains any optional attributes of the message used in processing the message, either at an intermediary point or at the ultimate end point.
·         Body: (Mandatory)
Contains the XML data comprising the message being sent.
·         Fault: ( Optional )
An optional Fault element that provides information about errors that occurred while processing the message
For details about of SOAP please refer links provide in reference section of article.
Here is a link to a site that let you to see by naked eye how soap request and soap response may look like

A SOAP Example


If a request is send to web service to invoke GetAccountBalance method having AccountCode as input parameter which returns Balance and the namespace for the function is defined in http://www.conflux.org/accountbalance then SOAP request and SOAP Response may look like

A SOAP request:


POST /URL_FromWhichCallMade HTTP/1.1
Host: www. conflux.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.conflux.org/accountbalance ">
  <m: GetAccountBalance >
    <m: AccountCode >00888786555</m: AccountCode >
  </m: GetAccountBalance >
</soap:Body>
</soap:Envelope>

The 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.conflux.org/accountbalance">
  <m: GetAccountBalance >
    <m: Balance >4523</m: Balance >
  </m: GetAccountBalance >
</soap:Body>

</soap:Envelope>

Here we have seen how soap request & soap response look like point to note that both are XML document with additional soap related nodes.

<soap:Envelope> act as root just like <html> in any html page,   it contain <soap:Body>
Just like <body> tag in html. <soap:Envelope> can also contain <soap:Header>
 <soap:Fault>.
References: