ĐÀO TẠO DOANH NGHIỆP : SỞ KHOA HỌC CÔNG NGHỆ TỈNH ĐỒNG NAI

ENTERPRISE TRAINING: DONG NAI DEPARTMENT OF SCIENCE AND TECHNOLOGY.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

HÌNH ẢNH TẬP HUẤN LỚP SHAREPOINT WORKFLOW VÀ KIẾN TRÚC SHAREPOINT

PHOTOS OF SHAREPOINT WORKFLOW AND ARCHITECTURE CLASS.

Showing posts with label Advance ASP NET. Show all posts
Showing posts with label Advance ASP NET. Show all posts

Thursday, February 2, 2012

Lesson 04: Web Services And Windown Communication Foundation(WCF)

Lesson 04: Web Services And Windown Communication Foundation(WCF)
Bài Tập: Tạo mới Solution với tên là Exercise04Soln
Ø     Bài Tập 1: Hiểu cơ bản về ASP.NET Web Service Application
·        Thêm mới ASP.NET Web Service Application Project vào solution tên là : DataWebService
·        Đổi tên Service1.asmx thành DataService.asmx
·        Bật sang chế độ code behide DataService.asmx.cs
ü     Đổi HelloWord Web Service interface(tên method) thành GetHelloWord
ü     Khai báo method mới tên là GetMessage(string message) và trả ra thông điệp
·        Tạo mới folder trong bất kì đĩa nào đặt tên là PublishWebServices
·        Publish Web Service này đến folder PublishWebServices
Ø     Bài Tập 2 :Cách lấy dữ liệu từ Web Service bằng ASP.NET
·        Thêm mới ASP.NET Application Project vào solution tên là : DataWebApplication
·        Open Default.aspx chế độ Design
·        Thêm 2 literal và 2 button vào Default.aspx
·        Add Reference đến DataWebService.dll trong PublishWebServices\BIN folder
·        Khai báo code trong sự kiện của button GetHelloWord
ü     Gọi GetHelloWord interface(tên method) và hiển thị thông điệp lên literal1
           protected void Button1_Click(object sender, EventArgs e)
 {
            DataWebService.Service1 service = new DataWebService.Service1();
            Literal1.Text = service.HelloWorld();
                   }
·        Khai báo code trong sự kiện của button GetMessage
ü     Gọi GetMessage interface(tên method) và hiển thị thong điệp lên literal2
  protected void Button2_Click(object sender, EventArgs e)
        {
            DataWebService.Service1 service = new            DataWebService.Service1();
            Literal2.Text = service.GetMessage("hung");
        }
·        Chạy F5 và click trên button sau đó xem kết quả
Ø     Bài Tập 3 :Hiểu biết về sự kết hợp giữa Web Service và ADO.NET
·        Tiếp tục với DataWebService
·        Thêm Web Service tên là Students.asmx
·        Đổi HelloWord thành GetName(string id)
·        Trả ra giá trị bằng cách gọi method GetValue(string id)
[WebMethod]
        public string GetName(string id)
        {
            return getValues(id);
     }
·        Khai báo method tên là GetValue(string id)
ü     Kết nối tới database StudentManagementForProfessional
ü      Lấy và trả ra StudentId của Student table mà có StudentId là id được truyền vào
string getValues(string id)
        {
            SqlConnection sqlConnection = null;
            SqlCommand sqlCommand = null;

            string connectionString = "server=(local);database=StudentManagementForProfessional;integrated security=true";
            DataTable dataTable = new DataTable();
            using (sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();
                string commandText = "select StudentId from Students where StudentId='" + id + "'";
                sqlCommand = new SqlCommand(commandText, sqlConnection);
                sqlCommand.CommandType = CommandType.Text;
                using (SqlDataReader reader=sqlCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        id = reader["StudentId"].ToString();
                    }
                }
            }
            return id;
        }
·        Publish Web Service này
Ø     Bài Tập 4 : Cách lấy dữ liệu dùng ADO từ Web Service bằng ASP.NET
·        Thêm Web Form tới ASP.NET tên là Default2.aspx
·        Thêm TextBox,Literal và Button Default2.aspx

·        Add Reference đến DataWebService.dll trong PublishWebServices\BIN folder

·        Khai báo code trong sự kiện của button Show Data
ü     Gọi GetName interface(tên method) với id là giá trị trên TextBox và hiển thị StudentId lên literal
              protected void Button1_Click(object sender, EventArgs e)
        {
DataWebService.Students student = new DataWebService.Students();
            Literal1.Text= student.GetName(TextBox1.Text);
        }
·        Chạy F5 và click trên button sau đó xem kết quả


Ø     Bài Tập 5 :Hiểu biết về sự kết hợp giữa Web Service và ADO.NET
·        Tiếp tục với DataWebService
·        Tạo 1 Class mới tên DataProvider
·        Khai báo method tên là GetValue(string id)
ü     Kết nối tới database StudentManagementForProfessional
ü      Lấy và trả ra StudentId của Student table mà có StudentId là id được truyền vào
    public string getValues(string id)
        {
            SqlConnection sqlConnection = null;
            SqlCommand sqlCommand = null;

            string connectionString = "server=(local);database=StudentManagementForProfessional;integrated security=true";
            DataTable dataTable = new DataTable();
            using (sqlConnection = new SqlConnection(connectionString))
            {
                sqlConnection.Open();
                string commandText = "select StudentId,FirstName +' '+ LastName as FullName from Students where StudentId='" + id + "'";
                sqlCommand = new SqlCommand(commandText, sqlConnection);
                sqlCommand.CommandType = CommandType.Text;
                using (SqlDataReader reader = sqlCommand.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        id = reader["FullName"].ToString();
                    }
                }
            }
            return id;
        }
·        Bật sang Students.asmx.cs
ü     Khai báo method tên là GetFullName(string id)
ü      Trả ra giá trị bằng cách gọi method GetValue(string id) của class DataProvider
             public string GetFullName(string id)
           {
            DataProvider dataProvider = new DataProvider();
            return dataProvider.getValues(id);
     }
·        Publish Web Service này

Ø     Bài Tập 6 : Cách lấy dữ liệu dùng ADO từ Web Service bằng ASP.NET
·        Thêm Web Form tới ASP.NET tên là Default3.aspx
·        Thêm TextBox,Literal và Button Default3.aspx

·        Add Reference đến DataWebService.dll trong PublishWebServices\BIN folder hoặc Add Web Reference đến Students.asmx (chạy Students.asmx copy link để add vào)

·        Khai báo code trong sự kiện của button Show Data
ü     Gọi GetFullName interface(tên method) với id là giá trị trên TextBox và hiển thị StudentId lên Literal
    protected void Button1_Click(object sender, EventArgs e)
        {
            DataWebService.Students student = new DataWebService.Students();
            Literal1.Text = student.GetFullName(TextBox1.Text);
  }
·        Chạy F5 và click trên button sau đó xem kết quả
Ø     Bài Tập 7 : Cách lấy dữ liệu dùng ADO từ Web Service bằng Windows Forms
·        Tạo Windows Form Application Project  tên là WindowsFormsApplication
·        Thêm TextBox,Label và Button Form1

·        Add Reference đến DataWebService.dll trong PublishWebServices\BIN folder hoặc Add Web Reference đến Students.asmx (chạy Students.asmx copy link để add vào)
·        Add Reference System.Web.Service
·        Khai báo code trong sự kiện của button Show Data
ü     Gọi GetFullName interface(tên method) với id là giá trị trên TextBox và hiển thị StudentId lên lablel
     private void button1_Click(object sender, EventArgs e)
        {
            DataWebService.Students student = new DataWebService.Students();
            label1.Text = student.GetFullName(textBox1.Text);
  }
·        Chạy F5 và click trên button sau đó xem kết quả
Ø     Bài Tập 8 : Hiểu cơ bản về Windows Communication Foundation(WCF)
·        Tạo WCF Service Application Project vào Solution  tên là DataWCFService
·        Bật IService1.cs
ü     Thêm interface GetHelloWord(string message)
                      [OperationContract]
        string GetHelloWorld(string message);
·        Bật Service1.svc.cs
ü     Khai báo method GetHelloWord(string message) và trả ra 1 chuỗi thông điệp
     public string GetHelloWorld(string message)
        {
            return message;
  }
·        Publish WCF Service này đến folder PublishWebServices


Ø     Bài Tập 9 :Cách lấy dữ liệu từ WCF bằng ASP.NET
·        Thêm Web Form tới ASP.NET tên là Default4.aspx
·        Thêm 2 literal và 2 button vào Default4.aspx

·        Add Reference đến DataWCFService.dll trong PublishWebServices\BIN folder hoặc Add Service Reference đến Service1.asmx(chạy Service1.asmx copy link để add vào)

·        Khai báo code trong sự kiện của button GetHelloWord
ü     Gọi GetHelloWord interface(tên method) và hiển thị thong điệp lên literal1
     protected void Button1_Click(object sender, EventArgs e)
        {
            //ServiceReference1.Service1Client abc = new DataWebApplication.ServiceReference1.Service1Client();//add tu url
            //Literal1.Text= abc.GetHelloWorld("hung");

            DataWCFService.Service1 a = new DataWCFService.Service1();
            Literal1.Text = a.GetHelloWorld("hung");
  }
·        Khai báo code trong sự kiện của button GetData
ü     Gọi GetData interface(tên method) và hiển thị thông điệp lên literal2
    protected void Button2_Click(object sender, EventArgs e)
        {
            ServiceReference1.Service1Client service = new DataWebApplication.ServiceReference1.Service1Client();
            Literal2.Text = service.GetData(1);
  }
·        Chạy F5 và click trên button sau đó xem kết quả
Ø     Bài Tập 10 : Cách lấy dữ liệu dùng ADO từ WFC bằng Windows Forms
·        Tiếp tục với WindowsFormsApplication
·        Thêm Form2
·        Thêm TextBox,Label và Button Form2

·        Add Reference đến DataWCFService.dll trong PublishWebServices\BIN folder
·        Add Reference System.Web.Service(đã add ở trên) hoặc Add Service Reference đến Service1.asmx(chạy Service1.asmx copy link để add vào)
·        Khai báo code trong sự kiện của button Show Data
ü     Gọi GetFullName interface(tên method) với id là giá trị trên TextBox và hiển thị StudentId lên lablel
     private void button1_Click(object sender, EventArgs e)
        {
            DataWCFService.Service1 service = new DataWCFService.Service1();
            label1.Text = service.GetFullName(textBox1.Text);
  }
·        Chạy F5 và click trên button sau đó xem kết quả
Bài Tập Mở rộng: không cho người ta biết trong service viết gì(security)
Ø     Bài Tập 11 :Cách sử dụng file cs được build từ DataService.asmx
·        Chạy DataService.asmx copy link http://localhost:49191/DataService.asmx
·        Start/program/…../Visual studio command prompt
·        Sẽ build ra đĩa bất kì giả sử là D: gõ D: enter
·        wsdl http://localhost:49191/DataService.asmx  /out:newcs.cs

·        hoàn tất sẽ xuất hiện file newcs.cs trong đĩa D
·        Tạo mới ASP.NET Application tên là UseBuildCsFromasmx
·        Copy newcs.cs trong đĩa D vào project và sư dụng tương tự như Class bình thường
Ø     Bài Tập 12 :Cách sử dụng file dll được build từ newcs.cs
·        Start/program/…../Visual studio command prompt
·        Sẽ build ra đĩa chứa newcs.cs giả sử là D: gõ D: enter
·        csc /target:library /out:newcs.dll newcs.cs

·        hoàn tất sẽ xuất hiện file newcs.dll trong đĩa D
·        Tạo mới ASP.NET Application tên là UseDllBuildFromCsOfasmx
·        Add Reference đến newcs.dll trong đĩa D vào project và sử dụng tương tự như dll bình thường