Search

Wednesday, May 14, 2008

Create simple ASP.NET webservice

วันนี้ว่าง ๆ ก็เลยลองสร้าง asp.net webservice เล่นดูบ้าง
สิ่งที่ต้องเตรียม
1. Visual Studio 2005.
2. เตรียมใจ (เหอะๆ )


บอกก่อนว่าเว็บเซอร์วิสที่จะสร้างเนี่ยมันง่ายแสนจะง่ายแค่คลิก ๆ ไม่กี่คลิกก็ได้แล้ว
เซอร์วิสที่จะทำ ก็มี
1. บวกเลข
2. ลบเลข

3. คูณเลข


มาลงมือกันเลย

On the File menu, click New and then click Project. Under Project types click Visual C# Projects, then click ASP.NET Web Service under Templates. Type MathService in the Location text box to change the default name (WebService1) to MathService

Click OK after visual studio create initial c# code like below.

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld() {
return "Hello World";
}
}

Change a namespace to http://localhost/mathservice and create method add , sub and multi


[WebMethod]
public int add(int a, int b)
{
return a + b;
}

[WebMethod]
public int sub(int a, int b)
{
return a - b;
}

[WebMethod]
public int multi(int a, int b)
{
return a * b;
}

Save and right click on service.asmx select view in browser from pop up menu then you see mathservice list on your browser.

Make it easy enjoy it.

No comments: