Web Service Convert Currency

  • Published on

  • View

  • Download

Embed Size (px)


<p>To Web Service vi C# step-by-stepCreated-By:V Vn Hi Website: http://vovanhai.wordpress.com</p> <p> 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-&gt;New-&gt;Project</p> <p> 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</p> <p>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-&gt;New Item.</p> <p>Chn Template Web Service, t tn cbo n l ConvertWS.asmx, Nhn nt Add ta c 1 web service tn ConvertWS trn Solution Explorer.</p> <p>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;</p> <p>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)</p> <p>{ return usd * USD_RATE; } [WebMethod] public double EUR2VND(double eur) { return eur * EUR_RATE; } } }</p> <p>4. Chy ng dng bng cch nhn F5, VS s trin khai ng dng. Kt qu nh sau</p> <p>Nu bn mun xem WSDL ca service, ta nhn link Service Description ta c</p> <p>Ghi nh li link ny cn dng v sau : http://localhost:1291/ConvertWS.asmx?WSDL</p> <p>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</p> <p>Nhp usd c gi tr 100, nhn Invoke, kt qu nhn c l</p> <p>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-&gt;New Project</p> <p>Chn Template Windows Form Application, t tn l ConsumeConvertWS, nhn OK, ta c</p> <p>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</p> <p>t tn cho Web reference name l convertWS. Nhn nt Add Reference. Ta c kt qu sau trn Project Explorer</p> <p>3. Thit k form nh hnh sau</p> <p>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;</p> <p>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);</p> <p>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(); } } }</p> <p>5. Chy th nghim</p> <p>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!</p>