This article is to learn the simplest method of creating and consuming web services.
Introduction
Here by, i have created a program to create and consume (call) the web service in a simplest way. Often, i have seen people stumbling on web services (even i used to), but after trying it in my own way i have enough confidence to work with it.
Steps to Create Web Service
- Select File menu option > Create new web site > Select template “ASP .Net Web Service”
- This will add WebService.asmx and it will add in App_code > WebService.cs / WebService.vb
- In WebService.cs / WebService.vb, implement your Web Method(s).
Consuming (Calling) Web Service
Web Services can be consumed in 2 ways :
- Select Project / Web site > right-click > Add Web Reference
Follow the wizard. If you select option “Web service in this solution”, it will do following things
- Creates App_WebReferences folder
- Create localhost having 2 files – <WebServiceName>.disco & <WebServiceName>.wsdl
- Then write following code in .aspx > Button click event :
- [Here, WebService is the name of the web service.]
//**************USING ADD WEB REFERENCE************************
localhost.WebService objWebService =
new localhost.WebService();Response.Write(objWebService.AddTwoNumbers(
Convert.ToInt16(txtNum1.Value), Convert.ToInt16(txtNum2.Value)));
2. By coding (in .aspx’s Button Click event)
protected
void btnSubmit_Click(object sender, EventArgs e){
//*************************BY CODE******************************XmlDocument xmlDoc =
new XmlDocument();MSXML2.XMLHTTP xmlHTTP;
DataSet MyDS;
//CREATING SOAP ENVELOPEstring soap =@"<?xml version=""1.0"" encoding=""utf-8""?><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/"">
<soap:Body>
<AddTwoNumbers xmlns=""http://tempuri.org/"">
<Num1>"
+ txtNum1.Value.ToString();soap +=
@"</Num1>";soap +=
@"<Num2>" + txtNum2.Value.ToString();soap +=
@"</Num2></AddTwoNumbers >
</soap:Body>
</soap:Envelope>"
;try{
//SET WEB SERVICE PATH, CAN ALSO BE READ FROM FILEstring WSPath = "http://localhost:2598/RnD-CSharp-WebSite2008/Web%20service/WebService.asmx"; //this can be any path where you Web service resides//GENERATE REQUESTxmlHTTP =
new MSXML2.XMLHTTP();xmlHTTP.open(
"Post", WSPath, false, "", "");xmlHTTP.setRequestHeader(
"SOAPAction", "http://tempuri.org/AddTwoNumbers ");xmlHTTP.setRequestHeader(
"Content-Type", "text/xml; charset=utf-8");//MAKING A CALL TO WEB SERVICExmlHTTP.send(soap.ToString());
//LOADING RESPONSE OF WEB SERVICE & DISPLAYING TO USERxmlDoc.LoadXml(xmlHTTP.responseText.ToString());
MyDS =
new DataSet();MyDS.ReadXml(
new System.Xml.XmlNodeReader(xmlDoc));int returnVal = Convert.ToInt16(MyDS.Tables[1].Rows[0][0].ToString());Response.Write(returnVal);
}
catch (System.Net.WebException ex){
throw ex;}
finally{
xmlDoc =
null;xmlHTTP =
null;MyDS =
null;}
}
//End brace of click event
WEB METHOD written in WebService.cs :
[WebMethod]
public int AddTwoNumbers(string Num1, string Num2){
return Convert.ToInt16(Num1) + Convert.ToInt16(Num2);}
Conclusion
So friends, hows it ? Easy na, now give it a try :) Happy coding buddies !
[NOTE : If you like this article, please vote for it]