.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

การส่งค่าแล้วเรียกใช้งาน webservice Options · View
paedotnet
Posted: Monday, October 06, 2008 6:09:58 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 344
Location: bkk

ลองดูตัวอย่างนะครับ
ในตัวอย่างนี้จะแบ่งออกเป็น 2 ส่วนคือส่วนของ web site ซึ่งส่วนนี้จะทำหน้าที่เรียกใช้งาน webservice
และอีกส่วนคือ webservice ซึ่งส่วนนี้จะทำหน้าที่ส่งข้อมูลไปที่ web site ที่เรียกใช้งาน
สิ่งแรกเรามาสร้าง web service กันก่อนเลยครับ
1. ไปที่ File->New Web site แล้วเรียก ASP.NET  Web Service ดังรูป

ใน web service นี้ผมตั้งชื่อเป็น ShowDetailWebService นะครับ
     จากนั้น ไฟล์ Service.cs จะถูกสร้างขึ้นมาแล้วมีหน้าตาดังรูป


2.  หลังจากที่สร้าง webservice เสร้จแล้วต่อไปก็คือสร้างเมธอดเพื่อส่งค่าและรับค่าโดยผมจะสร้างเมธอดรับค่าที่เป็น ชื่อ,นามสกุลมาจาก ทาง website
     แล้วก็ส่งข้อความดังนี้ออกไปที่ website ที่เรียกใช้ webservice ดังนี้ "สวัสดีครับ คุณ name lastname  เป็นยังไงบ้างครับ"
     ดูตัวอย่างเมธอดนะครับผมมี 1 เมธอดคือ ShowNameAndLastName ตัวอย่าง เมธอดนี้
 
   [WebMethod] public string ShowNameAndLastName(string name,string lastname) { name_str = name; lastname_str = lastname; return string.Format("สวัสดีครับคุณ {0} {1} เป็นยังไงบ้างครับ", name_str, lastname_str); } จะต้องสร้างตัวแปร name_str,lastname_str ด้วยเป็นชนิด string

3. จากนั้นทดสอบรัน webservice ดูจะมีเมธอดที่เราสร้างขึ้นมาใน webservice ดังรูป


4. จากนั้นไปเพิ่มโปรเจค  website ให้ไปที่ file->Add->new website แล้วตั้งชื่อว่า CallWebService  หลังจาก add แล้วจะ
    มีโปรเจคถูกสร้างขึ้นมาดังรูป


5. จากนั้นไปรัน  web service อีกครั้งหนึ่งเพื่อที่จะ  copy url ของ web service มาใช้หลังจากที่รันแล้ว
     หน้าแรก Service ให้ copy url มาดังรูป

 

แล้วก็ไปที่ชื่อ โปรเจค website ที่เราสร้างขึ้นมาคลิกขวาเลือก Add web reference...
    แล้วจะมีหน้า ต่าง Add Web Reference เกิดขึ้นมาให้เราเอา url ไปใส่ในช่อง url ดังรูป

จากนั้นคลิกที่ Go (ลูกศรสีเขียวข้างๆ ช่องของ URL)
6. จะเห็นว่าหลังจาก คลิกที่ Go แล้วตรง Web reference name: จะมีชื่อ localhost เกิดขึ้นมาซึ่งชื่อนี้เป็นชื่ออ้างอิงของ web service เราอาจจะเปลี่ยเป็น
    ชื่ออื่นๆก็ได้แต่ในตัวอย่างนี้ผมไม่เปลี่ยนนะครับใช้ชื่อ localhost เหมือนเดิม ก็ให้คลิกที่ปุ่ม Add Reference  หลังจากที่ add reference แล้วก็จะมี
    folder ของ localhost เกิดขึ้นมาดังรูป


7. หลังจากที่ได้ reference webservice เรียบร้อยแล้วต่อไปก็เรียกใช้งาน web service กัน ในหน้า form ของ website ก็ใช้ลาก textbox มา 2 textbox แล้ว
    ก็ลาก button มา 1 button แล้วลาก label มา 1 label ดังรูป

แล้วก็จัดการตั้งชื่อให้เรียบร้อยดังนี้
    NameTextBox,LastNameTextBox,CallButton,ShowLabel
8.  ให้ดับเบิลคลิกที่ ปุ่ม Callwebservice แล้วเขียนโค้ดดังนี


   protected void CallButton_Click(object sender, EventArgs e)
    {
        localhost.Service callservice = new localhost.Service();
        ShowLabel.Text = callservice.ShowNameAndLastName(NameTextBox.Text, LastNameTextBox.Text);
    }


แล้วทดสอบรันดูจะได้ดังรูป

ตัวอย่างโค้ด Service.cs

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 string name_str, lastname_str; public Service () { } [WebMethod] public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public string ShowNameAndLastName(string name,string lastname)
    {
        name_str = name;
        lastname_str = lastname;
        return string.Format("สวัสดีครับคุณ {0} {1} เป็นยังไงบ้างครับ", name_str, lastname_str);
    }
   
}

 

ตัวอย่างโค้ด Default.aspx.cs

  using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CallButton_Click(object sender, EventArgs e)
    {
        localhost.Service callservice = new localhost.Service();
        ShowLabel.Text = callservice.ShowNameAndLastName(NameTextBox.Text, LastNameTextBox.Text);
    }
}


[With great power comes great responsibility]
MR.L
Posted: Tuesday, October 21, 2008 8:42:55 AM
Rank: มือฝึกหัด
Groups: Member

Joined: 3/19/2008
Posts: 4
Location: Thailand

Great Fundamental Webservice

emit
Posted: Wednesday, October 22, 2008 5:33:56 PM
Rank: มือฝึกหัด
Groups: Member

Joined: 10/9/2008
Posts: 2
Location: thai

ถามครับ แบบนี้เรียก services จริงๆ error นิครับ

paedotnet
Posted: Thursday, October 23, 2008 6:30:13 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 344
Location: bkk

Error ว่ายังไงครับ ในตัวอย่างนี้ก็ไม่ Error นะครับแล้วยังแสดงผลลัพธ์ได้ด้วย ไม่ทราบว่าเขียนโค้ดผิดหรือเปล่าครับ



[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by