ผู้ตอบมากที่สุด
Silverlight Call Web Service แบบ Synchronous

คำถาม
-
ผมอยากจะเรียก Service แบบไม่ Async จะทำได้ไหมครับ
เนื่องจากผม convert โปรเจคเก่า มาเขียนเป็น silverlight โดยไม่ได้แก้ไข Web Service
แล้วติดปัญหาตรงที่ว่าการตรวจสอบบางส่วนต้องทำการเรียก service 2 ครั้งต่อเนื่องกัน
ผมได้สร้าง method สำหรับการตรวจสอบนั้น โดยมีการเรียกใช้ service 2 ครั้ง(หรือมากกว่า)ภายใน method นี้
แล้ว return ค่าออกมาแบบไม่ใช้ callback function ประมาณนี้
public bool ValidateXY()
{
var x = <call service1>...
var y = <call service2>...
if (x == y)
return true;
else
return false;
}
เพราะผมแยกโปรเจคสำหรับ reference service ต่างหากเลย (silverlight class library)
ผมได้ลองค้นหาข้อมูลมาบ้างแล้วแต่ยังหาทางออกไม่ได้ครับ
ManualRaiseEvent ก็ใช้ไม่ได้ ใช้แล้วค้างไปเลย (ไม่แน่ใจว่าเพราะ callback มันอยู่ thread เดียวกับ UI ใช่หรือไม่)
ขอบคุณครับ
3 สิงหาคม 2554 9:24
คำตอบ
-
คุณต้องทำการ validate ค่าในการ callback ครั้งที่ 2 ค่ะ
Supa Sethasiripong [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- ทำเครื่องหมายเป็นคำตอบโดย supa_sModerator 9 สิงหาคม 2554 6:20
9 สิงหาคม 2554 2:26ผู้ดูแล -
ใน Silverlight จะต้องใช้เป็นแบบ asynchronous ค่ะ
หน้าตาของโค้ดจะประมาณนี้
public bool ValidateXY ()
{.
service1Proxy.DoWorkCompleted += Service1Completed;
<call service1> ...
}Service1Completed()
{
x = e.Result;
service2Proxy.DoWorkCompleted += Service2Completed;
<call service2> ...}
Service2Completed()
{
y = e.Result;
<Do something with x and y>
}
Supa Sethasiripong [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- ทำเครื่องหมายเป็นคำตอบโดย supa_sModerator 9 สิงหาคม 2554 6:20
9 สิงหาคม 2554 2:28ผู้ดูแล
ตอบทั้งหมด
-
ถ้าทำแบบนั้น มันจะไม่ได้ค่าจาก method ValidateXY() สิครับ เพราะมันจะไปจบที่ callback function ของการเรียก service ครั้งที่ 2
ที่ผมต้องการคือ สร้าง method แล้วในนั้นมีการเรียกใช้ service มากกว่า 1 ครั้ง แล้ว return ค่ากลับไป
แต่ถ้าการเรียก service แบบ async มันจะไม่สามารถ return ค่าได้ เพราะมันจะกระโดดไปทำ callback funtion ที่ register delegate ไว้
ปล. ทำไมเนื้อหาโพสข้างบนมันไม่ค่อยชัดเลยครับ
5 สิงหาคม 2554 1:39 -
คุณต้องทำการ validate ค่าในการ callback ครั้งที่ 2 ค่ะ
Supa Sethasiripong [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- ทำเครื่องหมายเป็นคำตอบโดย supa_sModerator 9 สิงหาคม 2554 6:20
9 สิงหาคม 2554 2:26ผู้ดูแล -
ใน Silverlight จะต้องใช้เป็นแบบ asynchronous ค่ะ
หน้าตาของโค้ดจะประมาณนี้
public bool ValidateXY ()
{.
service1Proxy.DoWorkCompleted += Service1Completed;
<call service1> ...
}Service1Completed()
{
x = e.Result;
service2Proxy.DoWorkCompleted += Service2Completed;
<call service2> ...}
Service2Completed()
{
y = e.Result;
<Do something with x and y>
}
Supa Sethasiripong [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- ทำเครื่องหมายเป็นคำตอบโดย supa_sModerator 9 สิงหาคม 2554 6:20
9 สิงหาคม 2554 2:28ผู้ดูแล