Search

Showing posts with label Webservice. Show all posts
Showing posts with label Webservice. Show all posts

Thursday, May 15, 2008

Hello Client .net webservice

หลังจากที่เราได้สร้าง Mathservice เรียบร้อยแล้วนะครับ ต่อไปเราก็จะมาเรียกใช้ว่ามันจะใช้งานได้หรือเปล่าว
เริ่มกันเลย

Create new window application project in ProjectType select Visual C# template : Window Application then rename project to MachClient in Name field. Drag textbox , lable and button to From like this.
Then right click on MathClient project contectype munu are appear select Add web refference...
Select browse to : Web service in this solution
Select you service in my solution contain two service. I select MathService.
Wait for vs search webservice in solution and then you can view service list

Back to Form1 click button1 and put this code

if (comboBox_Operator.SelectedIndex == -1)
{
MessageBox.Show("กรุณาเลือก operator");
return;
}

MyService.Service myservice = new HelloClient.MyService.Service();

if (comboBox_Operator.SelectedIndex == 0)
{
label_Result.Text = myservice.add(Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text)).ToString();
}
else if (comboBox_Operator.SelectedIndex == 1)
{
label_Result.Text = myservice.sub(Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text)).ToString();
}
else if (comboBox_Operator.SelectedIndex == 2)
{
label_Result.Text = myservice.multi(Int32.Parse(textBox1.Text), Int32.Parse(textBox2.Text)).ToString();
}

Explain:
Read text check condition if textbox1 and textbox2 is not empty.
Green text create instance of MathService.

แล้วทีนี้ก็ลอง รันโปรเจคดูนะครับจะได้ผลเป็นยังไงบวกลบคูณเลขถูกเปล่าว อันนี้ผมเขียนแบบง่าย ๆ ไม่มีการรตรวจสอบว่าถ้า input เป็น text จะทำยังไง ง่ายอีกละ ก็ให้ exception แล้วบอกว่า อะไรก็ได้เช่น กรุณาป้อนตัวเลขค่ะ เอาไว้มาต่อกันครั้งหน้าดีกว่าครับ

Wednesday, May 14, 2008

How to user UDDI

สำหรับหลาย ๆ คนที่ชอบเล่นเว็บเซอร์วิสก็คงรู้ดีครับว่า UDDI คืออะไรและใช้งานอย่างไร ส่วนตัวผมแล้วไม่ค่อยได้ Deploy หรอกครับเพราะไม่มี Host เป็นของตัวเองเลยไม่่รู้ว่าจะเอาไป Deploy ที่ไหนดี

สำหรับคนที่เริ่มจะศึกษา หรืออยากลองเรียก service เล่น ๆ อยากทราบถึงวิธีการใช้ UDDI และจะหา UDDI ได้ที่ไหน ที่นี่มีคำตอบครับ

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.