How to create & consume Web service ? - The simplest way

Psmiles18
Posted by in ASP.NET category on for Beginner level | Views : 14892 red flag
Rating: 5 out of 5  
 3 vote(s)

This article is to learn the simplest method of creating and consuming web services.


 Download source code for How to create & consume 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 – <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 ENVELOPE

string 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 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 !

[NOTE : If you like this article, please vote for it]

Page copy protected against web site content infringement by Copyscape

About the Author

Psmiles18
Full Name: Payal Desai
Member Level: Starter
Member Status: Member
Member Since: 5/7/2010 5:28:43 AM
Country: India



Login to vote for this post.

Comments or Responses

Posted by: Ap.maurya on: 6/15/2010
Really very easy way.

Login to post response

Comment using Facebook(Author doesn't get notification)