You are on page 1of 29

Web Service

H Th Hong Vy

C BN

Web Service
Trong ng dng SOA dng Web Service, ta cn quan tm
To web service

Service

Chy th web service

Trin khai web service ln host

Tm web service cn thit

Client

S dng web service


i ng dn ca WS lc thc thi
4

To ASP.NET Web Service


File > New > Project > Language [Visual C#] > Web > ASP.NET Web Service Application
File > New > Web Site > ASP.NET Web Service

To ASP.NET Web Service


Click phi project > Add New Item > Web Service

Xy dng hm cho Web Service


View code trong file .asmx.cs Vit hm > thm attribute [WebMethod] cho hm
[WebMethod] public int Sum(int x, int y) { return x + y; }
7

Chy th Web Service

Chy th Web Service

Lu
Giao din kim th hm ca Web Service

ch chy c my cc b (localhost)

10

Trin khai Web Service


Ci IIS trn Vista/7
Control Panel > Programs and Features >

Turn Windows features on or off

11

Trin khai Web Service


Chy IIS
Control Panel > Administrative Tools >

Internet Information Services (IIS) Manager Add Application vo Default Web Site
Click phi Default Web Site > Add Application t Alias cho Web Service Chn th mc cha Web Service Physical Path
12

13

14

Lu
Khng nn t th mc cha Web Service

cc th mc ring ca user
Desktop My Documents ...

15

S dng Web Service

16

i ng dn Web Service

17

V d 1

NG DNG DCH PHP-VIT

18

M t
Xy dng 1 ng dng/web site cho php dch Php-Vit
c web service dch Php-Anh c web service dch Anh-Vit

19

M hnh
French Php-Anh

French

English

Vietnamese

Anh-Vit
Vietnamese
20

Ci t
Web Service Php-Anh (.asmx.cs)
[WebMethod] public string Translate(string french) { return Lookup(french); } public string Lookup(string word) { // Ci t hm tm t ting Php trong csdl // tr ra t ting Anh tng ng }
21

Ci t
Web Service Anh-Vit (.asmx.cs)
[WebMethod] public string Translate(string english) { return Lookup(english); } public string Lookup(string word) { // Ci t hm tm t ting Anh trong csdl // tr ra t ting Vit tng ng }
22

Ci t
ng dng dch Php-Vit
public string Translate(string french) { // Gi web service Php-Anh FEService proxyFE = new FEService(); string english = proxyFE.Translate(french); // Gi web service Anh-Vit EVService proxyEV = new EVService(); string vietnamese = proxyEV.Translate(english); return vietnamese; }
23

Lu
i vi .NET framework 4.0:
S khng tm thy: ASP.NET Web Service thay vo l WCF Cch to:
File>New Project > ASP.NET Empty Web application Right click project > add new item > web service

36

Lu
Khi s dng, thay v add web references th : add service references Chn advanced > add web references

37

Add service references


protected void btnTinhTong_Click(object sender, EventArgs e) { DemoWS.WebService1SoapClient myclient = new DemoWS.WebService1SoapClient(); int a = Convert.ToInt32(TextBox1.Text); int b = Convert.ToInt32(TextBox2.Text); int tong = myclient.TinhTong(a, b); Label1.Text = tong.ToString(); }

38

Add web references


protected void btnTinhTong_Click(object sender, EventArgs e) { DemoWS.WebService1 myclient = new DemoWS.WebService1(); myclient.Url = "http://localhost:20911/WebService1.asmx"; int a = Convert.ToInt32(TextBox1.Text); int b = Convert.ToInt32(TextBox2.Text); int tong = myclient.TinhTong(a, b); Label1.Text = tong.ToString(); }

39

WS .net framework 4.0


namespace webserviceNFW4 { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class WebService1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } } 40 }

Thank You!

Questions & Answers

41

Dynamic webservice
Link: http://www.codeproject.com/KB/webservices/Dynamic_Invoke_webser vice.aspx Link: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/391 38d08-aa08-4c0c-9a58-0eb81a672f54

42

You might also like