Pages

Monday, November 29, 2010

Calling Web service using javascript overloaded web method

Here is the code for calling the webservice using javascript with overloaded web methods
Default.aspx
<script type="text/javascript">
       function CallWebService() {
           WS.a(ReuqestCompleteCallback, RequestFailedCallback);
           WS.b('Amit',ReuqestCompleteCallback, RequestFailedCallback);
       }
       function ReuqestCompleteCallback(result)
       {
           // Display the result.
           var divResult = document.getElementById("divMessage");
           divResult.innerHTML = result;
       }
       function RequestFailedCallback(error) {
           var stackTrace = error.get_stackTrace();
           var message = error.get_message();
           var statusCode = error.get_statusCode();
           var exceptionType = error.get_exceptionType();
           var timedout = error.get_timedOut();
 
           // Display the error.  
           var divResult = document.getElementById("Results");
           divResult.innerHTML = "Stack Trace: " + stackTrace + "  " +  "Service Error: " + message + "  " +  "Status Code: " + statusCode + "  " +  "Exception Type: " + exceptionType + "  " +  "Timedout: " + timedout;
          }
    </script>
<form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="~/WS.asmx" />
        </Services>
        </asp:ScriptManager>
    </div>
    <div>
            <input type="button" value="Call Web Service" onclick="CallWebService();" />
        </div>
        <div id="divMessage">
        </div>
    </form>
WebService == WS.asmx
1. You can't use method overloading in web service since wsdl 1.1 does not supports it. That is the reason why you  are using alias name for the overloaded methods
2. When you use alias name then the webmethod is exposed as alias name instead of original name in the wsdl. So you need call your webmethod using alias name from your client. Your client have no idea about the original method name when alias is used. 
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WS : System.Web.Services.WebService
{
    public WS()
    {
  }
  [WebMethod(Description = "HelloWorld Returns 'Hello World'", MessageName = "a")]
    public string HelloWorld()
    {
        return "Hello World";
    }
    [WebMethod(Description = "HelloWorld Returns Given Name", MessageName = "b")]
    public string HelloWorld(string Name)
    {
        return Name;
    }
}

Thursday, November 18, 2010

Web Services - Method Overloading

This is not as same as we have in the Class. It will be build successfully but when we use or consume, it will generate error message - "Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods". when WSDL (Web Service Description Language) is generated, it will not be able to make the difference between methods because WSDL does not deal on the base of parameters.
To overcome this just do the following: Also add the following line above the class

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[WebMethod(MessageName = "Print1", Description = "Print 1", EnableSession = true)]
    public int A(int a, int b)
    {         return 1;     }
    [WebMethod(MessageName = "Print2", Description = "Print 2", EnableSession = true)]
    public int A(int x)
    {         return 2;     }
Reason
By passing the 'MessageName Property’ it changes the method name in the WSDL as shown below:
<wsdl:operation name="A">
            <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Print 1</wsdl:documentation>
            <wsdl:input name="Print1" message="tns:Print1SoapIn" />
            <wsdl:output name="Print1" message="tns:Print1SoapOut" />
</wsdl:operation>
<wsdl:operation name="A">
            <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Print 2</wsdl:documentation>
            <wsdl:input name="Print2" message="tns:Print2SoapIn" />
            <wsdl:output name="Print2" message="tns:Print2SoapOut" />
</wsdl:operation>

About Multicast Delegates

Multicast Delegate
Delegate that holds the reference of more than one method.
Multicast delegates must contain only methods that return void, otherwise run-time exception will be thrown.
Example
delegate void DelegateMulticast(int x);
Class ClassX
{
    static void A(int x)
    {
        Console.WriteLine("Hello A");
    }
    static void B(int x)
    {
        Console.WriteLine("Hello B");
    }
    public static void Main()
    {
        DelegateMulticast func = new DelegateMulticast(A);
        func += new DelegateMulticast(B);
        func(1);             // Both A and B are called
        func -= new DelegateMulticast(A);
        func(2);             // Only B is called
    }
}
Description
As you see that there are 2 methods named A & B with one parameter and void as return type.
Create delegate object
DelegateMulticast func = new DelegateMulticast(A);
Then the Delegate are added and removed using the += operator and -= operator resp.