Web Service Convert Currency

Embed Size (px)

Text of Web Service Convert Currency

To Web Service vi C# step-by-stepCreated-By:V Vn Hi Website: http://vovanhai.wordpress.com

y chng ta c 1 v d v to 1 web service cho vic chuyn i tin t t tin Vit Nam sang tin USD v Euro, cng nh vic chuyn ngc li. IDE s dng y chng ta dng b VS 2008, v ngn ng lp trnh C#. A. To Web Service 1. Trong mi trng Visual Studio, Ta chn File->New->Project

y ta chn Template l ASP.Net Web Service Application, in v tn Prject l ConvertCurrency, chn th mc lu tr. Nhn OK, ta c

2. Xa Service1.asmx bng cch chn n v nhn phm delete. Ta s thm 1 service mi bng cch nhn chut phi ln project, chn Add->New Item.

Chn Template Web Service, t tn cbo n l ConvertWS.asmx, Nhn nt Add ta c 1 web service tn ConvertWS trn Solution Explorer.

3. Code cho cc service methods nh sauusing using using using using using using System; System.Collections; System.ComponentModel; System.Data; System.Web; System.Web.Services; System.Web.Services.Protocols;

namespace ConvertCurrency { /// /// Web service ny dng chuyn i ngoi t t tin Vit sang USD, /// Euro v ngc li /// [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem(false)] public class ConvertWS : System.Web.Services.WebService { private const double USD_RATE = 16740; private const double EUR_RATE = 18960; [WebMethod] public double VND2USD(double dong) { return dong/USD_RATE; } [WebMethod] public double VND2EUR(double dong) { return dong / EUR_RATE; } [WebMethod] public double USD2VND(double usd)

{ return usd * USD_RATE; } [WebMethod] public double EUR2VND(double eur) { return eur * EUR_RATE; } } }

4. Chy ng dng bng cch nhn F5, VS s trin khai ng dng. Kt qu nh sau

Nu bn mun xem WSDL ca service, ta nhn link Service Description ta c

Ghi nh li link ny cn dng v sau : http://localhost:1291/ConvertWS.asmx?WSDL

5. th service, ta c th chn bt k link no trong 4 link EUR2VND, USD2VND VND2EUR, VND2USD. y ta th link USD2VND, kt qu nh sau

Nhp usd c gi tr 100, nhn Invoke, kt qu nhn c l

Nh vy l chng ta to c 1 Web Service, by gi chng ta c th trin khai 1 ng dng client c ri. B. To Consumer 1. To ng dng Windows Application bng cch nhn phi chut ln Solution trong project Explorere, chn Add->New Project

Chn Template Windows Form Application, t tn l ConsumeConvertWS, nhn OK, ta c

Nhn chut phi ln project va to, chn Set as StartUp Project m bo project va to l project s c thc thi khi nhn F5. 2. Trong Project Explorer, nhn chut phi ln References, chn Add Web Reference G vo URL n WSDL vo URL, nhn nt Go, kt qu nh sau

t tn cho Web reference name l convertWS. Nhn nt Add Reference. Ta c kt qu sau trn Project Explorer

3. Thit k form nh hnh sau

t tn cho TextBox l txtMoney, Label hin th kt qu l lblKetQua 4. Vit code cho cc ntusing using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Text; System.Windows.Forms;

namespace ConsumeConvertWS { public partial class Form1 : Form { private ConsumeConvertWS.convertWS.ConvertWS ws; public Form1() { InitializeComponent(); ws = new ConsumeConvertWS.convertWS.ConvertWS(); } private void VND2USD_Click(object sender, EventArgs e) { double mon = Double.Parse(txtMoney.Text); double usd = ws.VND2USD(mon); lblKetQua.Text = usd.ToString(); } private void VND2EUR_Click(object sender, EventArgs e) { double mon = Double.Parse(txtMoney.Text); double usd = ws.VND2EUR(mon);

lblKetQua.Text = usd.ToString(); } private void USD2VND_Click(object sender, EventArgs e) { double mon = Double.Parse(txtMoney.Text); double usd = ws.USD2VND(mon); lblKetQua.Text = usd.ToString(); } private void EUR2VND_Click(object sender, EventArgs e) { double mon = Double.Parse(txtMoney.Text); double usd = ws.EUR2VND(mon); lblKetQua.Text = usd.ToString(); } } }

5. Chy th nghim

C. Kt lun Vy l chng ta to c 1 web service n gin v 1 client tiu th web service ny. Cc bn nu c tham vng tm hiu su hn v web service vi VS th c th tham kho thm trong b MSDN. Chc may mn!