Introduction: This article demonstrates how to create a web service using visual studio 2005 and also demonstrates how to consuming a web service using SOAP protocol.
please follow the steps to create a web service in visual studio 2005
step 1: creating a web service
->Start Microsoft Visual Studio .NET or Visual Studio 2005. ->Create a new ASP .NET Web service project. Name the Web service "DictionaryService". ->Change the name of the Solution file to "DictionaryService" for consistency. ->Change the name of the default Web service that is created from Service1.asmx to "DictionaryService.asmx".
Define methods that encapsulate the functionality of your service. Each method must be flagged with a WebMethod attribute in front of it. Without this attribute, the method will not be exposed from the service.
->Add the following method to the "DictionaryService" class that you just created:
#region DictionaryService [WebMethod(Description = "Returns the the tamil meaning for a given english word")] public string EnglishToTamil(string strENG_Word) { DataTable dt = new DataTable(); dt = ReturnTAM(strENG_Word); string strTam = dt.Rows[0][0].ToString(); return strTam; } public DataTable ReturnTAM(string strENG_Word) { string strTamil = ""; string strSQL = "select tam from dictionary where eng='" + strENG_Word + "'"; string connectionString = "Data Source=server;Initial Catalog=database;Integrated Security=True"; SqlConnection sqlCon = new SqlConnection(connectionString); sqlCon.Open();
SqlCommand sqlCmd = new SqlCommand(strSQL, sqlCon); SqlDataReader sqlDR = sqlCmd.ExecuteReader();
DataTable dt = new DataTable(); dt.Load(sqlDR); sqlCon.Close(); return dt; } #endregion
->Click Build on the Build menu to build the Web service. ->Browse to the "DictionaryService.asmx" Web service page to test the Web service. If you set the local computer to host the page, the URL is http://localhost/DictionaryService/DictionaryService.asmx.
Step 2: Consume Web Service Using SOAP Protocol
For consuming a web service using SOAP protocol, you need to create a Web application.
->create a new ASP.Net web application. ->click on “add reference“ and then select “Interop.MSXML2.dll” and click on add. ->Create one label to display result (ID = lblResult). ->Add the following source code in the code behind
using MSXML2; protected void Page_Load(object sender, EventArgs e) { string strSoapEnvelope= ""; //SOAP Envelope strSoapEnvelope = ""; strSoapEnvelope += " strSoapEnvelope += "xmlns:xsi = \"http://www.w3.org/2001/XMLSchema-instance\" "; strSoapEnvelope += "xmlns:xsd= \"http://www.w3.org/2001/XMLSchema\" "; strSoapEnvelope += "xmlns:soap= \"http://schemas.xmlsoap.org/soap/envelope/\">"; strSoapEnvelope += ""; strSoapEnvelope += ""; strSoapEnvelope += "Advertisement"; strSoapEnvelope += ""; strSoapEnvelope += ""; strSoapEnvelope += "";
MSXML2.XMLHTTP xmlHTTP = new XMLHTTP(); xmlHTTP.open("Post", "http://localhost:2495/DictionaryService/DictionaryService.asmx", false, "", ""); xmlHTTP.setRequestHeader("SOAPAction", "http://tempuri.org/EnglishToTamil"); xmlHTTP.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); xmlHTTP.send(strSoapEnvelope.ToString()); lblResult.Text = xmlHTTP.responseText.ToString(); }
here is the Result ?????????
Conclusion:
There are many difference implementations for SOAP Service but the standard is there and we can start to build some useful applications on it. Although ASP.NET or SOAP web services have n number of features, if you want to consume the exposed Web Services in you clients, please make sure you have the latest libraries installed because standards continue evolving and all vendors try their best achieve the edge in such open standards war.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|