Web Services in C#

An Overview

Let's think of a scenario where I am planning to show information on regional, national and international news, weather information, ongoing sports scores and other personalized content in a web site. Just think about how much effort and time it will take me to develop this application if I write the code for all these functionalities. On the other hand all these functionalities are already provided by other available sites. So, what if I can use this existing logic in my application? But, the question here is “how I can use someone else's business logic in my application?”.

For situations of this sort (& many other), we have techniques like Web Services.

With Web Services, you can reuse someone else's business logic instead of replicating it yourself, using just a few lines of code. This technique is similar to what programmers currently do with libraries of APIs, DLLs or plug-ins. The main difference is that Web Services can be located remotely on another server.

When HTML pages (or the HTML output generated by ASP.NET web forms) are rendered in a browser for the end user, Web Services are invoked by other applications. They are pieces of business logic that are hosted somewhere on the internet and can be accessed by other applications.

Note 1: Web Services are not limited to the .NET Framework. The standards were defined before .NET was released and they are exposed, used and supported by vendors other than Microsoft.

Note 2: Web Services are cross-platform; a service written in one language can be invoked by an application in some other language. The only requirement for accessing a service is an internet connection to make the HTTP request.

Since a web service is cross-platform, there should be some commonly understandable language for requesting a service and getting a response from the service. Such a standard common language is XML. That's why Web Services are built on XML-based standards for exchanging data.

As a result, the set of data types Web Services can use is limited to the set of data types recognized by the XML Schema standard. So you can use simple data types such as strings and numbers to communicate with a web service and you can't send proprietary .NET objects such as a FileStream, an Image or an EventLog. This restriction makes a lot of sense. Since other programming languages have no way to interpret these .NET objects, even if you could devise a way to send them over the wire, the client might not be able to interpret them, that would thwart interoperability.

Note 3: If you need to work with .NET proprietary objects, you can go for .NET remoting. It is a distributed technology that allows use of .NET objects. But non-.NET clients can't consume it.

Here's the list of supported data types

  • Built-in types (The Basics): The built in C# data types such as short, int, long, ushort, uint, ulong, float, double, decimal, bool, string, char, byte and DateTime.

  • Objects: You can use an object of any user defined class. If your class has methods, these methods will not be transmitted to the client.

  • Arrays: Arrays of any supported type (built-in or custom objects). You can also use an ArrayList (that is simply converted into an array).

  • Enumerations: Enums are supported. However, a web service uses the string name of the enumeration value, not the underlying integer.

  • XmlNode: Objects based on System.Xml.XmlNode represent a portion of an XML document. You can use this to send arbitrary XML.

  • DataSet and DataTable : DataSet and DataTable are allowed. But other ADO.NET data objects, such as DataColumns and DataRows, aren't supported.

Create a web service

A web service is a simple asmx page.

Here I will use Visual Studio 2012 (though you can use any editor), with the .Net Framework 3.5 to create a web service.

Up to framework 3.5, Visual Studio provides a direct template for Web Services. Starting from 4, it doesn't provide any direct template, so you need to create a web application and add a web service to your application (right-click on your web application → Add → New Item → WebService).

Let's create a simple service that will return a sum of 2 integers.

Open Visual Studio in Administrator mode. File → New → Project then select .Net Framework 3.5 (on the top) then select ASP.NET web service application then name your project (I named it MyWebServiceDemo) then click OK.

web service

Visual Studio will create a web service boilerplate (Service1.asmx and Service1.asmx.cs) for you. Now, let's analyze this template created by Visual Studio.

Note the following In Service1.asmx.cs:

  1. An additional namespace “System.Web.Services” is included along with the 4 default namespaces that Visual Studio includes for web application.

  2. The “Service1” class is inherited from “System.Web.Services.WebService”. By inheriting a class from “System.Web.Services.WebService”, you can access built-in ASP.NET objects such as (Application, Session, User, Context, server). If you don't need built-in objects of .Net, you don't need to inherit your service class from “WebService”.

  3. “Service1” is decorated with a “WebService(Namespace = "http://tempuri.org/" )” attribute. If you want to expose a class as a service, you need to decorate it with the “WebService” attribute. This WebService attribute has several properties like:

    • Namespace: This property makes a service uniquely identifiable. This is an XML property. A client application may be consuming several services, so there is the probability of a naming collision. To avoid this, it's service providers responsibility to use a unique namespace.

    • Name: Use this property to provide a descriptive name to your service.

    • Description: To provide a brief description on the service.

  4. “Service1” has another attribute “WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)”. This is to indicate the standard which service is following. If the service does not confirm to this standard, you will get an exception.

  5. One more attribute service is decorated with is “[System.Web.Script.Services.ScriptService]”, to make a service accessible from the client script, it should be decorated with this attribute.

  6. The Service1 class has a method HelloWorld that is decorated with a “[WebMethod]” attribute. The methods of the service that are to be accessed by the client application should be decorated with this attribute. There may be some method that the service is using for some internal functionality, client applications don't need to access them. Don't decorate such methods with a WebMethod attribute. The WebMethod attribute also has Name and Description properties that you can use to provide a self describing name or description respectively.

Now let's see the mark up. Right-click on Service1.asmx in Solution Explorer then select view mark up. In Service1.asmx, you will see that there is only a WebService directive with some attributes, since a service will be invoked by some application not by any end user. So the asmx page has no mark up.

  1. <%@ WebService Language="C#" CodeBehind="Service1.asmx.cs" 
  2. Class="MyWebServiceDemo.Service1"%>   
  • The “WebService” directive, indicates this asmx page is a web service.
  • The “Language="C#"”, is to indicate language used for this service.
  • The “CodeBehind” property is nothing to do with ASP.NET or web service, this is completely a Visual Studio property, that is used to map the asmx page with it's code behind page.
  • The “Class” property holds fully qualified name of service class. This marks the entry point of service like main() in C programming language.

Now, run your application by hitting F5, http://localhost:56655/Service1.asmx will open in your browser (the port number may vary). You will find a link for Service Description, that will redirect to the WSDL document of the service, another link for HelloWorld (list for methods exposed by service) that will redirect to a page for testing this method.

Implementing a web service

Now
let's implement the service. Rename the “Service1” file in Solution Explorer to something convenient like “MyService”. Change the class name from Service1 to MyService. Open the mark up (asmx) page.

my service

As you can see here Visual Studio is unable to resolve “Service1” in the class property, since the class indicates a fully qualified name of the service and we renamed our Service1 class to MyService. So Visual Studio is unable to resolve it. So, now change the class property to “MyWebServiceDemo.MyService”. Change the “CodeBehind” property from “Service1.asmx.cs” to “MyService.asmx.cs” as we renamed the file also.

MyService.asmx

  1. <%@ WebService Language="C#" CodeBehind="MyService.asmx.cs"  
  2. Class="MyWebServiceDemo.MyService"%>  
MyService.asmx.cs
  1. using System.Web.Script.Serialization;  
  2. using System.Web.Services;  
  3.   
  4. namespace MyWebServiceDemo  
  5. {  
  6.     // Use "Namespace" attribute with an unique name,to make service uniquely         discoverable  
  7.     [WebService(Namespace = "http://tempuri.org/")]  
  8.     // To indicate service confirms to "WsiProfiles.BasicProfile1_1" standard,   
  9.     // if not, it will throw compile time error.  
  10.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  11.     // To restrict this service from getting added as a custom tool to toolbox  
  12.     [System.ComponentModel.ToolboxItem(false)]  
  13.     // To allow this Web Service to be called from script, using ASP.NET AJAX  
  14.     [System.Web.Script.Services.ScriptService]  
  15.     public class MyService : WebService  
  16.     {  
  17.         [WebMethod]  
  18.         public int SumOfNums(int First, int Second)  
  19.         {  
  20.             return First + Second;  
  21.         }  
  22.     }  
  23. }  
Now, the service is ready to be used, let's compile and test it.

Test a web service

Let's run the project by hitting F5. The “http://localhost:56655/MyService.asmx” page will open that has a link for the Service description (the WSDL document, documentation for web service) another link for SumOfNums, which is for test page of SumOfNums method.

Let's use method overloading of the OOP concept. Add the following WebMethod in MyService class.
  1. [WebMethod]  
  2. public float SumOfNums(float First, float Second)  
  3. {  
  4.       return First + Second;  
Hit F5 to run the application, you will get “Both Single SumOfNums(Single, Single) and Int32 SumOfNums(Int32, Int32) use the message name 'SumOfNums'. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.” error message. We just used method overloading concept, so why this error message? This is because these methods are not unique for a client application. As the error message suggests let's use the MessageName property of the WebMethod attribute as shown below:
  1. [WebMethod (MessageName = "SumOfFloats")]  
  2. public float SumOfNums(float First, float Second)  
  3. {  
  4.       return First + Second;  
  5. }  
Now, compile and run the application. Again it's showing some different error message “Service 'MyWebServiceDemo.MyService' does not conform to WS-I Basic Profile v1.1”. As, WsiProfiles.BasicProfile1_1 doesn't support method overloading we are getting this exception. Now, either remove this “[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]” attribute or make it “[WebServiceBinding(ConformsTo = WsiProfiles.None)]”.
  1. using System.Web.Script.Serialization;  
  2. using System.Web.Services;  
  3.   
  4. namespace MyWebServiceDemo  
  5. {  
  6.     [WebService(Namespace = "http://tempuri.org/")]  
  7.     [WebServiceBinding(ConformsTo = WsiProfiles.None)]  
  8.     [System.ComponentModel.ToolboxItem(false)]  
  9.     [System.Web.Script.Services.ScriptService]  
  10.     public class MyService : WebService  
  11.     {  
  12.         [WebMethod]  
  13.         public int SumOfNums(int First, int Second)  
  14.         {  
  15.             return First + Second;  
  16.         }  
  17.         [WebMethod(MessageName = "SumOfFloats")]  
  18.         public float SumOfNums(float First, float Second)  
  19.         {  
  20.             return First + Second;  
  21.         }  
  22.     }  
  23. }
Now you can use method overloading in the service.

The Test page

Click on SumOfNums, you will be redirected to “http://localhost:56655/MyService.asmx?op=SumOfNums”. You will see here that just “?op=SumOfNums” is appended to the service URL. This page has 2 text boxes for 2 input values (First, Second) that the SumOfNums method takes as an input parameter and a button “Invoke”, clicking on which you'll be redirected to: “http://localhost/WebServiceForBlog/MyService.asmx/SumOfNums” that is has the value that the SumOfNums method returned in XML format. Similarly, by clicking on “SumOfNums MessageName="SumOfFloats"”, you will be redirected to “http://localhost:56655/MyService.asmx?op=SumOfFloats”. So the “SumOfNums MessageName="SumOfFloats"” method will be known as “SumOfFloats” for client applications.

Now, the question is, from where does this test page come? We never added any mark up but still a page was rendered!

The test pages aren't part of the Web Services; they're just a frill provided by ASP.NET. The test page is rendered by ASP.NET using the web page c:\[WinDir]\Microsoft. NET\Framework\[Version] \Config\DefaultWsdlHelpGenerator.aspx. “Reflection” concept to render the test page.

You can also modify this test page for which you simply need to copy the DefaultWsdlHelpGenerator.aspx file to your web application directory, modify it and give it a name. I named it “MyWsdlHelpGenerator.aspx” and then changed the web.config file for the application by adding the <wsdlHelpGenerator> element, as shown here:
  1. <configuration>  
  2.     <system.web>  
  3.         <webServices>  
  4.             <wsdlHelpGenerator href="MyWsdlHelpGenerator.aspx"/>  
  5.         </webServices>  
  6.     </system.web>  
  7. </configuration>  
The WSDL document

Web Services are self-describing, that means ASP.NET automatically provides all the information the client needs to consume a service as a WSDL document. The WSDL document tells a client what methods are present in a web service, what parameters and return values each method uses and how to communicate with them. WSDL is a XML standard. We'll explore the WSDL document in our next articles.

Host web service

Since we will add a reference to this service and consume it from various applications and the port number is supposed to change, let's host this service on IIS to have a specific address of a service. Open IIS, go to the default web sites node then seelct Add Application then provide an alias name (I gave it WebServiceForBlog) then browse to the physical location of your service for the physical path field then click “OK”. You can now browse with an alias name (like http://localhost/WebServiceForBlog/) to test if the application was hosted properly. You'll get a “HTTP Error 403.14 – Forbidden” error since there is no default document set for this application. Add a “MyService.asmx” page as a default document. Now you can browse your service.

How to access a web service from a client application

Here our primary focus will be on consuming a web service. Let's quickly create a web service (or modify, if you have already created one).

MyService.asmx
  1. <%@ WebService Language="C#" CodeBehind="MyService.asmx.cs"  
  2. Class="MyWebServiceDemo.MyService" %>  
MyService.asmx.cs
  1. using System.Web.Script.Serialization;  
  2. using System.Web.Services;  
  3.   
  4. namespace MyWebServiceDemo  
  5. {  
  6.     [WebService(Namespace = "http://tempuri.org/")]  
  7.     [WebServiceBinding(ConformsTo = WsiProfiles.None)]  
  8.     [System.ComponentModel.ToolboxItem(false)]  
  9.     [System.Web.Script.Services.ScriptService]  
  10.     public class MyService  
  11.     {  
  12.         // Takes 2 int values & returns their summation  
  13.         [WebMethod]  
  14.         public int SumOfNums(int First, int Second)  
  15.         {  
  16.             return First + Second;  
  17.         }  
  18.   
  19.         // Takes a stringified JSON object & returns an object of SumClass  
  20.         [WebMethod(MessageName = "GetSumThroughObject")]  
  21.         public SumClass SumOfNums(string JsonStr)  
  22.         {  
  23.             var ObjSerializer = new JavaScriptSerializer();  
  24.             var ObjSumClass = ObjSerializer.Deserialize<SumClass>(JsonStr);  
  25.             return new SumClass().GetSumClass(ObjSumClass.First, ObjSumClass.Second);  
  26.         }  
  27.     }  
  28.   
  29.      // Normal class, an instance of which will be returned by service  
  30.     public class SumClass  
  31.     {  
  32.         public int First, Second, Sum;  
  33.   
  34.         public SumClass GetSumClass(int Num1, int Num2)  
  35.         {  
  36.             var ObjSum = new SumClass  
  37.             {  
  38.                 Sum = Num1 + Num2,  
  39.             };  
  40.             return ObjSum;  
  41.         }  
  42.     }  
  43. }  
Compile this application. Since we will consume this service from other applications we need a fixed address for this service, the port numbers are supposed to vary. So, host the web service on IIS (refer to the previous article for hosting the web service). I hosted it with the virtual directory “WebServiceForBlog”.

How to consume a web service

Let's add another project to our solution. You can also create a new project. I am adding to the same solution, so that it will not need me to open several instances of Visual Studio. When developing and testing a web service in Visual Studio, it's often preferred to add both the web service and the client application to the same solution. This allows you to test and change both pieces at the same time. You can even use the integrated debugger to set breakpoints and step through the code in both the client and the server.

Right-click on solution, select Add → New project then seelct ASP.NET Empty Application then name your application, I am naming it “ServiceConsumer”.

Add a Web Form, I name it “Home.aspx”.

On the click of a button in this page, we will call our service.

For calling a Web Service you need a proxy object that will handle the complexities of sending a SOAP request and response messages.

To create this proxy class, you need a reference to the service class. Right-click on this project then select Add service reference, a window will open, type the URL of your service, click on discover, you will see all the webmethods exposed by your service listed. At the bottom of the window, there'll be a field for Namespace. Provide a name for the namespace in which the proxy class of the referenced service will be generated, I am giving it “MyServiceReference”. Now, click on “Go”.

By adding a service reference, we created a proxy class of the referenced service to the current project (client app).

The proxy class wraps the calls to the web service's methods. It takes care of generating the correct SOAP message format and managing the transmission of the messages over the network using HTTP and converting the results received back to the corresponding .NET data types.

Now, add mark up in Home.aspx to receive inputs. Here's my mark up.

 

  1. <table>  
  2.     <tr>  
  3.         <td>First Number:</td>  
  4.         <td><input type="text" id="Text1" runat="server" /></td>  
  5.     </tr>  
  6.     <tr>  
  7.         <td>Second Number:</td>  
  8.         <td><input type="text" id="Text2" runat="server" /></td>  
  9.     </tr>  
  10.     <tr>  
  11.         <td><input type="button" onserverclick="AddNumber" value="Add" runat="server" /></td>  
  12.         <td><div id="divSum" runat="server"></div>  
  13.         <div id="divSumThroughJson" runat="server"></div></td>  
  14.     </tr>  
  15. </table>  
Home.aspx.cs
  1. using System;  
  2. using ServiceConsumer.MyServiceReference;  
  3.   
  4. namespace ServiceConsumer  
  5. {  
  6.     public partial class Home : System.Web.UI.Page  
  7.     {  
  8.         protected void Page_Load(object sender, EventArgs e)  
  9.         {}  
  10.         protected void AddNumber(object Sender, EventArgs E)  
  11.         {  
  12.             int Num1, Num2;  
  13.             int.TryParse(txtFirstNum.Value, out Num1);  
  14.             int.TryParse(txtSecondNum.Value, out Num2);  
  15.             // creating object of MyService proxy class  
  16.             var ObjMyService = new MyServiceSoapClient();  
  17.   
  18.             // Invoke service method through service proxy  
  19.             divSum.InnerHtml = ObjMyService.SumOfNums(Num1, Num2).ToString();  
  20.   
  21.             var ObjSumClass = new SumClass { First = Num1, Second = Num2 };  
  22.             var ObjSerializer = new JavaScriptSerializer();  
  23.             var JsonStr = ObjSerializer.Serialize(ObjSumClass);  
  24.             divSumThroughJson.InnerHtml =ObjMyServiceProxy.GetSumThroughObject(JsonStr).Sum.ToString();  
  25.         }  
  26.     }  
  27. }  
How to consume a web service from a client script

Add another project to your solution. I named it “ConsumeServiceFromClientScript”. Add a web form (let's say Default.aspx).

Creating a JavaScript proxy

JavaScript proxies can be automatically generated by using the ASP.NET AJAX ScriptManager control's Services property. You can define one or more services that a page can call asynchronously to send or receive data using the ASP.NET AJAX “ServiceReference” control and assigning the Web Service URL to the control's “Path” property.

Add a ScriptManager control like the following inside the form tag:
  1. <asp:ScriptManager runat="server">  
  2.      <Services>  
  3.           <asp:ServiceReference Path="http://localhost/WebServiceForBlog/MyService.asmx"/>  
  4.       </Services>  
  5. </asp:ScriptManager>  

When you add a reference to a web service (MyService.asmx) from a page using a ScriptManager control, a JavaScript proxy is generated dynamically and referenced by the page. Now if you the check page source in a browser, you can notice that:

http://localhost/MyWebServiceDemoService/MyService.asmx/js

Or:

http://localhost/MyWebServiceDemoService/MyService.asmx/jsdebug

Is included in your page with a script tag depending on whether debugging is enabled In your project.

Add an empty Web form to your project. I named it “Default.aspx”.

Default.aspx

  1. <body>  
  2.     <form id="form1" runat="server">  
  3.        <asp:ScriptManager runat="server">  
  4.             <Services>  
  5.                 <asp:ServiceReference Path="http://localhost/WebServiceForBlog/MyService.asmx"/>  
  6.             </Services>  
  7.         </asp:ScriptManager>  
  8.         <div>  
  9.             <table>  
  10.                 <tr>  
  11.                     <td>First Number:</td>  
  12.                     <td><input type="text" id="txtFirstNum" /></td>  
  13.                 </tr>  
  14.                 <tr>  
  15.                     <td>Second Number:</td>  
  16.                     <td><input type="text" id="txtSecondNum" /></td>  
  17.                 </tr>  
  18.                 <tr>  
  19.                     <td><input type="button" onclick="AddNumber();" value="Add" /></td>  
  20.                     <td><div id="divSum1"></div><div id="divSum2"></div><div id="divSum3"></div><div id="divSum4"></div><div id="divSum5"></div></td>  
  21.                 </tr>  
  22.             </table>  
  23.         </div>  
  24.     </form>  
  25.     <script type="text/javascript" src="Scripts/jquery-1.7.1.js"></script>  
  26.     <script type="text/javascript" src="Scripts/json2.js"></script>  
  27.     <script type="text/javascript">  
  28.         function AddNumber() {  
  29.             var FirstNum = $('#txtFirstNum').val();  
  30.             var SecondNum = $('#txtSecondNum').val();  
  31.   
  32.             $.ajax({  
  33.                 type: "POST",  
  34.                 url: "http://localhost/WebServiceForBlog/MyService.asmx/SumOfNums",  
  35.                 data: "First=" + FirstNum + "&Second=" + SecondNum,   
  36.           // the data in form-encoded format, it would appear on a querystring  
  37.                //contentType: "application/x-www-form-urlencoded; //charset=UTF-8"                                    //form  encoding is the default one  
  38.                 dataType: "text",  
  39.                 crossDomain: true,  
  40.                 success: function (data) {  
  41.                     $('#divSum1').html(data);  
  42.                 }  
  43.             });  
  44.   
  45.             // i/p in JSON format, response as JSON object  
  46.             $.ajax({  
  47.                 type: "POST",  
  48.                 url: "http://localhost/WebServiceForBlog/MyService.asmx/SumOfNums",  
  49.                 data: "{First:" + FirstNum + ",Second:" + SecondNum + "}",  
  50.                 contentType:"application/json; charset=utf-8",// What I am ing  
  51.                 dataType: "json"// What I am expecting  
  52.                 crossDomain: true,  
  53.                 success: function (data) {  
  54.                     $('#divSum2').html(data.d);  
  55.                 }  
  56.             });            
  57.   
  58.         // i/p as JSON object, response as plain text  
  59.             $.ajax({  
  60.                 type: "POST",  
  61.                 url: "http://localhost/WebServiceForBlog/MyService.asmx/SumOfNums",  
  62.                 data: { First: FirstNum, Second: SecondNum },  
  63.                 contentType: "application/x-www-form-urlencoded; charset=UTF-8",  
  64.                 crossDomain: true,  
  65.                 dataType: "text",  
  66.                 success: function (data) {  
  67.                     $("#divSum3").html(data);  
  68.                 }  
  69.             });  
  70.   
  71.         // Url encoded stringified JSON object as I/p & text response  
  72.             var ObjSum = new Object();  
  73.             ObjSum.First = FirstNum;  
  74.             ObjSum.Second = SecondNum;  
  75.             $.ajax({  
  76.                 type: "POST",  
  77.                 url: "http://localhost/WebServiceForBlog/MyService.asmx/GetSumThroughObject",  
  78.                 data: "JsonStr=" + JSON.stringify(ObjSum),  
  79.                 dataType: "text",  
  80.                 crossDomain: true,  
  81.                 success: function (data) {  
  82.                     $('#divSum4').html($(data).find('Sum').text());  
  83.                 }  
  84.             });  
  85.   
  86.             // Call SOAP-XML web services directly   
  87.             var SoapMessage = '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> \  
  88.                                       <soap:Body> \  
  89.                                         <SumOfNums xmlns="http://tempuri.org/"> \  
  90.                                           <First>'+FirstNum+'</First> \  
  91.                                           <Second>'+SecondNum+'</Second> \  
  92.                                         </SumOfNums> \  
  93.                                       </soap:Body> \  
  94.                                     </soap:Envelope>';  
  95.             $.ajax({  
  96.                 url: "http://localhost/WebServiceForBlog/MyService.asmx?op=SumOfNums",  
  97.                 type: "POST",  
  98.                 dataType: "xml",  
  99.                 data: SoapMessage,  
  100.                 complete: ShowResult,  
  101.                 contentType: "text/xml; charset=utf-8"  
  102.             });  
  103.         }  
  104.         function ShowResult(xmlHttpRequest, status) {  
  105.             var SoapResponse = $(xmlHttpRequest.responseXML).find('SumOfNumsResult').text();  
  106.             $('#divSum5').html(SoapResponse);  
  107.         }  
  108.     </script>  
  109. </body>  
Note: For demo purposes, I have hard-coded the service URL. But it's not a good practice. You can keep the service URL in the web.config and use RegisterClientScriptBlock or RegisterStartUpScript depending on your requirements to use this service URL in client script.

Call SOAP-XML Web Services directly

In order to call the web service, we need to supply an XML message that matches the operation definition specified by the web service's WSDL. In the test page “http://localhost/WebServiceForBlog/MyService.asmx?op=SumOfNums” you can see a sample of SOAP 1.1/1.2 request and response format. From which you can get the operations schema. Now, you just need to exchange the type names for the operation's parameters, with their actual values. Copy the SOAP envelope to the Default.aspx page and replace the place holders (type names) with an actual value.

A sample for this type of call is included in the code snippet above. The variable “SoapMessage” in the preceding example contains the complete XML message that we're going to send to the web service. On completion of the ajax request, we will call a call back method named “ShowResult”.

Note: .NET 1.x supports SOAP 1.1 only. From .NET 2.0 Both SOAP1.1 and SOAP1.2 are supported unless one is restricted explicitly. If you want to change this, you can disable either one through the web.config file:
  1. <configuration>  
  2.       <system.web>  
  3.     <webServices>  
  4.            <protocols>  
  5.            <!-- To disable SOAP 1.2 -->  
  6.            <remove name="HttpSoap12"/>  
  7.            <!-- To disable SOAP 1.1 -->  
  8.            <remove name="HttpSoap"/>  
  9.            </protocols>  
  10.     </webServices>  
  11.        </system.web>  
  12. </configuration>  
References

Book: Programming .NET Web Services By Alex Ferrara, Matthew MacDonald
Book: Pro ASP.NET 3.5 in C# by Matthew MacDonald , Mario Szpuszta
Understanding ASP.NET AJAX Web Services