Sử dụng .Net Webservice trong C#

ngày 31-03-2016

Ở bài hướng dẫn Cách tạo Webservice, tui đã trình bày cách tạo web service và cách đưa lên internet như thế nào.
 
Trong bài tập này, Tui sẽ dùng C# để lấy dữ liệu từ server thông qua các service đã được cung cấp ở bài trước. 
 
Lý do Tui dùng C# trước là để các bạn dễ tưởng tượng ra cách thức lấy dữ liệu trên Webservice trước khi dùng KSOAP API trong Android để tương tác (vì tui cảm thấy nó hơi khó 1 xí).
 
Từ WebProject ở bài Cách tạo Webservice, bạn tạo thêm 1 Windows Form Project để lấy dữ liệu từ Service đó như sau:
 
 
– Khi bấm Nút “Get List Catalog”: Chương trình sẽ triệu gọi web service (hàm getListCatalog) lưu trên http://testdrthanh.somee.com/mywebservice.asmx và hiển thị vào ListBox (listbox_cate)
 
– Khi chọn Catalog bất kỳ trong listbox_cate thì sẽ hiển thị danh sách sản phẩm thuộc danh mục đang chọn vào listbox_product.
 
– Khi bấm nút “Delete Selected Product”: Sẽ xóa Product đang chọn trong listbox_product.
 
Sau đây là cách lấy dữ liệu từ webservice ở trên:
 
1- Bấm chuột phải vào Project/ chọn Add Service Reference:
 
 
2- Màn hình chọn WebService hiển thị ra như bên dưới đây:
 
 
3- Bấm chọn nút “Advanced“:
 
 
4- Ở màn hình trên ta chọn “Add Web Reference…“:
 
 
Mục số 1: Ta copy dán đường dẫn webservice vào
 
Mục số 2 đặt tên cho Web Reference rồi nhấn nút “Add Reference“.
 
Sau khi bạn nhấn nút “Add Reference” thì Project sẽ được thay đổi như sau:
 
 
File app.config tự động xuất hiện và bên trong nó có thông tin sau:
 
 
– Bây giờ ta tiến hành Coding cho các control trên giao diện như sau:
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace TestWebService
{
 public partial class Form1 : Form
 {
 public Form1()
 {
 InitializeComponent();
 }
 bool isFinish = false;
 com.somee.testdrthanh.mywebservice test = new com.somee.testdrthanh.mywebservice();
 //Nút hiển thị danh sách Catalog
 private void button1_Click(object sender, EventArgs e)
 {
 isFinish = false;
 
 List<com.somee.testdrthanh.Catalog> listCate = test.getListCatalog().ToList();
 listbox_cate.DataSource = listCate;
 listbox_cate.DisplayMember="CateName";
 listbox_cate.ValueMember="CateId";
 isFinish = true;
 }
 //xử lý hiển thị danh sách sản phẩm theo danh mục
 private void listbox_cate_SelectedIndexChanged(object sender, EventArgs e)
 {
 if (!isFinish) return;
 if (listbox_cate.SelectedItem != null)
 {
 com.somee.testdrthanh.Catalog cate = listbox_cate.SelectedItem as com.somee.testdrthanh.Catalog;
 List<com.somee.testdrthanh.Product> listProduct = test.getListProductByCatalogId(cate.CateId).ToList();
 listbox_product.DataSource = listProduct;
 listbox_product.DisplayMember = "ProductName";
 listbox_product.ValueMember = "ProductId";
 }
 }
 //xử lý nút xóa Product
 private void button2_Click(object sender, EventArgs e)
 {
 if (listbox_product.SelectedItem == null)
 return;
 DialogResult ret = MessageBox.Show("Muốn xóa Product này hả?", "Xác nhận xóa", MessageBoxButtons.YesNo);
 if (ret == DialogResult.Yes)
 {
 com.somee.testdrthanh.Product p = listbox_product.SelectedItem as com.somee.testdrthanh.Product;
 bool isDelete= test.deleteProduct(p.ProductId);
 if (isDelete)
 {
 com.somee.testdrthanh.Catalog cate = listbox_cate.SelectedItem as com.somee.testdrthanh.Catalog;
 List<com.somee.testdrthanh.Product> listProduct = test.getListProductByCatalogId(cate.CateId).ToList();
 listbox_product.DataSource = listProduct;
 listbox_product.DisplayMember = "ProductName";
 listbox_product.ValueMember = "ProductId";
 }
 else
 {
 MessageBox.Show("Không xóa được");
 }
 }
 }
 }
}
 
Khi thực thi thì ta có kết quả như sau:
 
 
 Bạn cố gẳng thử cho hết các Servive  được cung cấp ở trên.
 
 Các bạn có thể tải source code và CSDL ở đây:
http://www.mediafire.com/download/9dubk49ukme269k/code_bai44_bai45.rar
 
 Bài tập sau các bạn sẽ được học cách lấy dữ liệu từ Web Service bằng thự viện KSOAP API trong Android.
 
 Chúc các bạn thành công
 
Nguồn: Thầy Trần Duy Thanh - Giảng viên Trung Tâm Tin Học Đại học Khoa Học Tự Nhiên