A Web Services Sample
Hi Friends,
I am a new Blogger and I am sure that their will be lot of mistakes. All comments and advice welcome!
Thanks to http://www.webservicex.net/ . They have got really cool web services.
The Currency Converter used as a reference in this Blog is provided by webservicex.
Special Thanks & Regards to Armand du Plessis for giving great valid tips for Blogging.
-Cheers
Kishor
What is a Web Service?
Web services also called application services, allow different applications from different sources to communicate with each other without time-consuming custom coding, and because all communication is in XML, Web services are not tied to any one OS or programming language. For example, .NET languages can work with Perl ,Windows applications can talk with UNIX applications, What a wonderful World!!!.
To make things more clear, consider the following scenario.
If, our requirement is to develop a Universal currency converter, the task of developing the project from the scratch is very complicated. What if, somebody already did this Project and Published the service in a ready to use API format(Application Programming Interface). In this case, we will get the privilege of calling various APIs to accomplish the essential steps in the currency conversion. This is the key to every WebService.
How a WebService Project is created ?
In this sample, the process of creating a Universal Currency Converter WebService in Visual Studio2005 IDE/Smartphone2003 setup is illustrated.
*Internet connectivity is a prerequisite for running WebServices.
The step by step process is creating our Universal Currency Converter WebService is as follows:
1.Create a Smartphone2003, Device Application Project.
2. Now go to the Project Solution Explorer and find the References section, Right Click and select 'Add Web Reference'
3. An 'Add Web Reference' window is popped; in the URL enter 'http://www.webservicex.net/CurrencyConvertor.asmx' and click 'Go'.
4. Typically 'net.webservicex.www' is displayed in Web reference name:, if step3 is successful.
5.Now click on 'Add Reference' and we are ready to use the Universal Currency Converter WebService from our Program.
The step by step process of using the WebService APIs from our program is as follows:
1.Select the Object Browser and expand the Project section.
2. Expand the 'ProjectNameCreated.net.webservicex.www' namespace. My project name is Simulation, so my name space will be' Simulation.net.webservicex.www'
3.Here we can see the Currency enumeration and the CurrencyConvertor class and these are the key elements in consuming this WebService.
The screenshot of the sample Universal CurrencyConverter GUI is shown below :
Sample Universal Currency Converter Code:
1: //On FormLoad Event
2: private void Form1_Load(object sender, EventArgs e)
3: {
4: comboBox1.SelectedIndex = 0;
5: comboBox2.SelectedIndex = 0;
6: }
7: //When Exit is Clicked
8: private void menuItem2_Click(object sender, EventArgs e)
9: {
10: Application.Exit();
11: }
12: //When Convert is clicked
13: private void menuItem1_Click(object sender, EventArgs e)
14: {
15: Simulation.net.webservicex.www.CurrencyConvertor cc = new Simulation.net.webservicex.www.CurrencyConvertor();
16: Simulation.net.webservicex.www.Currency from = Simulation.net.webservicex.www.Currency.AED;
17: Simulation.net.webservicex.www.Currency to = Simulation.net.webservicex.www.Currency.AED;
18:
19: switch (comboBox1.SelectedIndex) {
20:
21: case 1:
22: from = Simulation.net.webservicex.www.Currency.AFA;
23: break;
24: case 2:
25: from = Simulation.net.webservicex.www.Currency.ALL;
26: break;
27: case 3:
28: from = Simulation.net.webservicex.www.Currency.DZD;
29: break;
30: case 4:
31: from = Simulation.net.webservicex.www.Currency.ARS;
32: break;
33: // ...
34: // See attached sample for full source code
35: }
36:
37: switch (comboBox2.SelectedIndex) {
38: case 1:
39: to = Simulation.net.webservicex.www.Currency.AFA;
40: break;
41: case 2:
42: to = Simulation.net.webservicex.www.Currency.ALL;
43: break;
44: case 3:
45: to = Simulation.net.webservicex.www.Currency.DZD;
46: break;
47: case 4:
48: to = Simulation.net.webservicex.www.Currency.ARS;
49: break;
50:
51: // ...
52: // See attached sample for full source code
53: }
54:
55: //error check for characters
56: if (textBox1.Text == "")
57: MessageBox.Show("The amount can't be null");
58: if (!Microsoft.VisualBasic.Information.IsNumeric(textBox1.Text))
59: MessageBox.Show("Enter amount as numbers..");
60: if (comboBox1.SelectedIndex == 0 | comboBox2.SelectedIndex == 0)
61: MessageBox.Show("Select a valid currency...");
62: else {
63: double value = cc.ConversionRate(from, to);
64: double amount = double.Parse(textBox1.Text);
65: double xpliedAmt = amount * value;
66: textBox2.Text = amount.ToString() + " " + from.ToString() + " = " + xpliedAmt.ToString() + " " + to.ToString();
67: }
68: }
What happens when a Web Service is run?
Please check this link :
http://msdn2.microsoft.com/en-us/library/aa480728.aspx
Sample Web Services and links:
https://www.mainfunction.com/DotNetInAction/Technologies/display.aspx?ID=2650&TypeID=38
http://msdn2.microsoft.com/en-us/library/aa480534.aspx
http://www.xmethods.net/sd/2001/BabelFishService.wsdl[Translator WebService]
http://services.aonaware.com/DictService/DictService.asmx[Mobile Dictionary]
http://www.webservicex.net/WCF/Services.aspx?CID=3