Powered by Blogger.

Saturday, July 17, 2010

How to create and use Web service ? - The simplest way

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 :

  1. 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 – .disco & .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 ENVELOPE string soap = @" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> " + txtNum1.Value.ToString(); soap += @""; soap += @"" + txtNum2.Value.ToString(); soap += @" "; try {
//SET WEB SERVICE PATH, CAN ALSO BE READ FROM FILE string WSPath = "http://localhost:2598/RnD-CSharp-WebSite2008/Web%20service/WebService.asmx"; //this can be any path where you Web service resides //GENERATE REQUEST xmlHTTP = 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 SERVICE xmlHTTP.send(soap.ToString()); //LOADING RESPONSE OF WEB SERVICE & DISPLAYING TO USER xmlDoc.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 !

Download


 Download source code for How to create & consume Web service ? - The simplest way




by Psmiles18

No comments:

Post a Comment

  ©Template by Dicas Blogger.

TOPO