In this post I'm going to show you
- how to create a web service.
- how to use that created web service through another program.
1. Visual Studio -> New Project -> Web category -> ASP.NET Web Service Application.
Create a web service application (server side) called “TempConverter_ASP.NET_Server".
2. When the Project is created, you get a Service.asmx and a Service.asmx.cs. You can delete this and
Proj. R+Click -> add new item -> web service called “Converter”.
3. Inside public class Converter : System.Web.Services.WebService { } remove 'HelloWorld' method & add the methods.
Converter.asmx.cs
_____________________________________________________________________________________
[WebMethod] // add for methods to indicate that they can be accessed outside the web service
public string FToC(double F)
{
return ((F - 32) * 5 / 9).ToString();
}
[WebMethod]
public string FToK(double F)
{
return ((F - 32) * 5 / 9 + 273.15).ToString();
}
[WebMethod]
public string CToF(double C)
{
return ((C * 9 / 5) + 32).ToString();
}
[WebMethod]
public string CToK(double C)
{
return (C + 273.15).ToString();
}
[WebMethod]
public string KToC(double K)
{
return (K - 273.15).ToString();
}
[WebMethod]
public string KToF(double K)
{
return ((K - 273.15) * 9 / 5 + 32).ToString();
}
_____________________________________________________________________________________
4. To test the service you can right click on Converter.asmx in the Solution Explorer in Visual Studio
and choose "View in Browser".
The test page displays the Converter service name along with the names of the methods and a link to the service description (WSDL).
How to use that created web service through another program
(C# client)
1. Now create a windows forms application “TempConverter_CSharp_Client" to call the web service you implemented.
2.To access the Converter API which is exposed as a web service you will have to add a Service
Reference in your project.
R+clicking on the project in solution explorer -> Add Service Reference.
In the address box of the displayed enter the location of WSDL for Converter Web Service. (You can copy the url of the WSDL for this). Also enter the name “proxy” in the namespace box in the window.
3. Add using TempConverter_WebService_Client_CSharp.proxy;
4. Now in the button click event of “Convert now” button...
_____________________________________________________________________________________
private void button1_Click(object sender, EventArgs e)
{
ConverterSoapClient client = new ConverterSoapClient();
if(comboBox1.Text == "K" && comboBox2.Text == "F")
{
label2.Text = "Answer is " + client.KToF(Convert.ToDouble(textBox1.Text));
}
if (comboBox1.Text == "K" && comboBox2.Text == "C")
{
label2.Text = "Answer is " + client.KToC(Convert.ToDouble(textBox1.Text)) ;
}
if (comboBox1.Text == "F" && comboBox2.Text == "K")
{
label2.Text = "Answer is " + client.FToK(Convert.ToDouble(textBox1.Text)) ;
}
if (comboBox1.Text == "F" && comboBox2.Text == "C")
{
label2.Text = "Answer is " + client.FToC(Convert.ToDouble(textBox1.Text)) ;
}
if (comboBox1.Text == "C" && comboBox2.Text == "K")
{
label2.Text = "Answer is " + client.CToK(Convert.ToDouble(textBox1.Text));
}
if (comboBox1.Text == "C" && comboBox2.Text == "F")
{
label2.Text = "Answer is " + client.CToF(Convert.ToDouble(textBox1.Text)) ;
}
}
_____________________________________________________________________________________
(Java client)
1. First start tomcat server. If you don't have tomcat installed download and install tomcat.
After installation go to localhost:8080 and check if tomcat is running.
2. Eclipse -> Server Window -> D+Click Tom Cat Server -> Change 3 Port numbers to any port number (ie: 1005 , 1010 , 1009 ), the R+Click Tom Cat Server -> Start.
3. File -> New -> Other -> Web Services -> Web Service Client -> Copy Paste A public WSDL to service definition -> Drag Left Slider To top Test Client -> "Client Project : ......." to change project name -> next -> select correct Output folder -> Next -> Finish.
Run and check for results.
Hope this helped....
Please leave a comment.Suggestions for improvements are also welcomed :)
...Happy Coding...
No comments:
Post a Comment