วันพฤหัสบดีที่ 6 มกราคม พ.ศ. 2554

07. การสร้าง WebService

จะประกอบไปด้วย 2 ไฟล์หลักคือ
  1. ระบบSvEn.cs เช่น DepositSvEn.cs มีหน้าที่สำหรับ เรียกใช้ pb service ที่ได้มีการ built ไว้แล้ว
  2. ระบบ.asmx เช่น  Deposit.asmx มีหน้าที่ประกาศไว้ให้ส่วน web sheet ทำการเรียกใช้งาน โดยจะไปทำการเรียกใช้ฟังก์ชั่น  ระบบSvEn.cs อีกที

ตัวอย่าง

- DepositSvEn.cs
ทำการประกาศตัวแปรเพื่อเรียกใช้ PB Service
รูปแบบ  private user_object_name ชื่อตัวแปร;
เช่น
 private n_cst_dbconnectservice svCon;
       private n_cst_deposit_service svDep;
       private n_cst_deposit_utility svDepUtil;

รูปแบบ

Public ชนิดข้อมูล ชื่อfucntion (Argument,Argument,…)
{
    svDep.ชื่อฟังก์ชันของPB ที่ต้องการ(Argument,Argument,…);
    return ค่าที่ต้องการ return;
}

เช่น

public String InitPrintBook(String deptAccountNo, String branchId)
       {
           String xmlPrintForm = "";
           svDep.of_init_printbook(deptAccountNo, branchId, ref xmlPrintForm);
           return xmlPrintForm;
       }

- Deposit.asmx
อ้างอิงฟังก์ชั่นที่มีการเรียกใช้ PB Service แล้วซึ่งอยู่ใน DepositSvEn.cs
รูปแบบ

[WebMethod]
Public ชนิดข้อมูล ชื่อfunction (String wsPass, Argument, Argument,…)
{
    วิธีที่ 1   
return new DepositSvEn(wsPass).ชื่อฟังก์ชั่นที่ได้ประกาศไว้แล้ว(Argument, Argument, …)
    หรือวิธีที่ 2
    DepositSvEn ชื่อตัวแปร = new DepositSvEn(wsPass);
Return ชื่อตัวแปร.ชื่อฟังก์ชั่นที่ได้ประกาศไว้แล้ว(Argument, Argument, …)
}
หมายเหตุ จำเป็นจะต้องมี wsPass เสมอ

เช่น

[WebMethod]
       public String InitPrintBook(String wsPass, String deptAccountNo, String branchId)
       {
           return new DepositSvEn(wsPass).InitPrintBook(deptAccountNo, branchId);
       
        //หรือ
           DepositSvEn dep = new DepositSvEn(wsPass);
           return dep.InitPrintBook(deptAccountNo, branchId);
       }

- เนื่องจากปัจจุบันได้เรียกใช้ WebService ผ่านทางเครื่อง webcenter เมื่อเราทำการเพิ่ม Service ใหม่เสร็จทุกครั้งจะต้องทำการ Built โดยไปที่ Solution Explorer คลิ๊กขวาที่ WebService -> Build รอจนกว่าจะเสร็จ



- เมื่อเสร็จแล้วก็ทำการ อัพเดท Service ใหม่ที่เราสร้างขึ้นเพื่อที่หน้าเวปจะได้ทำการเรียกใช้ได้
ไปที่  Solution Explorer-> Saving -> Web Reference -> เลือกระบบ -> คลิ๊กขวาเลือก Update Web Reference

หมายเหตุ ต้อง Update ทั้งเครื่อง webcenter และเครื่องผู้ใช้ด้วย

วิธีการเรียกใช้ Web Service จากเครื่อง webcenter

ไปที่ C:\WINDOWS\system32\drivers\etc เลือกไฟล์ hosts เปิดด้วย notepad หรืออื่นๆ

เพิ่มบรรทัดนี้เข้าไปในไฟล์ hosts

192.168.99.178     wshost   และทำการเซฟ


0 ความคิดเห็น:

แสดงความคิดเห็น