Friday, December 26, 2008

Steps to Create a WebService








First, create a website by clicking on File -- New Website.
• From “Visual Studio Installed Templates” click on “Asp.NET Web Service”. See
figure below. Name the figure as “Mathis Web Service”.
By default, the .NET editor has made a default web service method called as "Hello
Word" which returns a string data type. Let us rename "Service.vb" to "Maths.vb"
and "Service.asmx" to "Maths.asmx". Let’s replace the “HelloWorld” with
following code below :-
_
Public Function AddTwoNumbers(ByVal Number1 As Integer, ByVal
Number2 As Integer) As Integer
Return Number1 + Number2
End Function
After the web service is done click on add Web reference. Normally for
components, we do an “Add Reference” and for Webservices, we do “Add Web
Reference”.
You will be shown with a list of web services, which are known to the solutions. As
we are looking for our “Maths” web service, which exist in the same solution, we
click “Web services in this solution”.

Your editor has located the “Maths” webservice.Select the web service
After you have clicked on “Maths” web service you will see a search progress bar
as shown in figure below. This process will start the web service, reference it and
create a proxy for the client, so that using it client can absorb the web service.
Finally, you are able to see your web service, which is ready for use. Click on Add Reference and
you will see a “Local host” reference in your .NET solution.
Figure: - 4.15 Starting the webservice and creating the proxy for your solution.
• We need to make a client who will absorb this “Maths Webservice”. Add
“WebserviceClient.aspx” and create a UI as shown below. In the button, click put in
the following code. “LocalHost.ClsMaths” is the proxy object by which you can
make calls to the webservice.

Sub cmdCalculate_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim pobjMaths As New localhost.ClsMaths
lblResultDisplay.Text =
Convert.ToString(pobjMaths.AddTwoNumbers(Convert.ToInt16(txtNumber1.Tex
t), Convert.ToInt16(txtNumber2.Text)))
End Sub

No comments: