Recent Post

Sunday, May 26, 2013

Cơ bản với WCF

by Unknown  |  at  8:19 AM









WCF – Một số khái niệm cơ bản




WCF (Windows Communication Foundation):

- Cấu trúc của WCF service.

- Mô hình kết nối ABC.

- Các loại Contract.



Khái niệm


“Windows Communication Foundation (WCF) is Microsoft’s unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and interoperate with existing investments.” (MSDN).


WCF (Windows Communication Foundation) là công nghệ mới nhất được thiết kế theo kiến trúc hướng dịch vụ (Service Oriented Architecture – SOA). Mục đích của WCF là tạo một công nghệ thống nhất cho các công nghệ giao tiếp trước đây như: Web Services, .NET Remoting, Microsoft Message Queuing và COM+.


Ba thành phần chính của một WCF service là:


- Service class: định nghĩa các contact (sẽ trình bày ở bên dưới).


- Hosting environment: WCF service có thể được host lên nhiều “môi trường” khác nhau, bao gồm IIS, Windows service, Selt-hosting.


- End point: Các “cổng” kết nối giữa client và service.
Mô hình kết nối ABC


Việc giao tiếp giữa client và WCF service được thực hiện thông qua các Endpoint. Như vậy một service có thể làm chấp nhận và xử lý nhiều yêu cầu khác nhau thông các endpoint riêng biệt. Mỗi endpoint được tạo thành từ ba thành phần với tên gọi tắt rất dễ nhớ là ABC. Trong đó:


- A – Address (Where): địa chỉ của service.


- B – Binding (How): Cách thức giao tiếp với service. Thành phần này xác định loại giao thức kết nối giữa client và service (như HTTP, TCP, MSMQ,…), kênh xử lý và kiểu mã hóa thông điệp. Danh sách các kiểu binding:



Name

Purpose


BasicHttpBinding

Interoperability with Web services and clients supporting the WS-BasicProfile 1.1 and Basic Security Profile 1.0.


WSHttpBinding

Interoperability with Web services and clients that support the WS-* protocols over HTTP.


WSDualHttpBinding

Duplex HTTP communication, by which the receiver of an initial message does not reply directly to the initial sender, but may transmit any number of responses over a period of time by using HTTP in conformity with WS-* protocols.


WSFederationBinding

HTTP communication, in which access to the resources of a service can be controlled based on credentials issued by an explicitly-identified credential provider.


NetTcpBinding

Secure, reliable, high-performance communication between WCF software entities across a network.


NetNamedPipeBinding

Secure, reliable, high-performance communication between WCF software entities on the same machine.


NetMsmqBinding

Communication between WCF software entities by using MSMQ.


MsmqIntegrationBinding

Communication between a WCF software entity and another software entity by using MSMQ.


NetPeerTcpBinding

Communication between WCF software entities by using Windows Peer-to-Peer Networking.



- C – Contact (What): Bản mô tả các chức năng của service. Cụ thể, đây là các class (structure, enum,…) được định nghĩa bên service để với các phương thức mà client có thể yêu cầu service thực hiện.



Contract


Contract được coi là một bản “hợp đồng” quy định cách thức và phương tiện để hai bên có thể hợp tác và làm việc với nhau. Trong WCF, các contact được coi như những bản mô tả nhiệm vụ, kiểu dữ liệu của service. Client sẽ có được các contact sau khi tham chiếu đến service và thông qua đó để gửi và nhận các thông điệp. Như vậy, service và client không sử dụng cùng các kiểu dữ liệu như nhau, mà chỉ sử dụng chung các contact.


Service Contract


Mô tả các công việc của service, bao gồm các kiểu dữ liệu truyền đi và kiểu dữ liệu trả về. Cụ thể, đây là các khai báo phương thức kèm với các tham số và kiểu trả về của service. Loại contract này có thể được chia ra làm hai phần là service contract (tương ứng với các interface) và operation contract (là các method).


Data Contract


“A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged.” (MSDN)


Mô tả các kiểu dữ liệu tự tạo mà client có thể sử dụng. Các contract này dùng để chứa các dữ liệu cần thiết để truyền tải giữa client và service. Nếu service chỉ sử dụng các kiểu dữ liệu đơn giản (như int, string, DateTime,…) thì service đó không cần đến Data Contract.


Message Contract


Tương tự như Data Contract nhưng kiểu contract này nhằm mục đích định nghĩa cấu trúc của các thông điệp được truyền tải dưới dạng SOAP (Simple Object Access Protocol). Một thông điệp SOAP được chia thành 2 phần là header và body.





MSDN – Mối quan hệ của các loại contract với với common language runtime (CLR)






Fault Contract


Loại contact này giúp quản lý các ngoại lệ mà service ném ra khi thực hiện các yêu cầu của client. Khi cần thiết, service sẽ ném ra một FaultException nếu như muốn client bắt được ngoại lệ này.


// nguồn của anh yinyang congdongcviet






examples mình làm về wcf




File Đính Kèm



Monday, May 6, 2013

Khắc phục lỗi phân trang sai khi tìm kiếm dữ liệu trong asp.net

by Unknown  |  at  11:22 PM
^^! xem demo bằng vs2010 và sql 2008 nha

file đính kèm

hình ảnh kèm theo

Saturday, May 4, 2013

Đổi màu Dòng Gridview khi rê chuột trong asp.net

by Unknown  |  at  12:08 PM


Cuối tuần rảnh nghịch chơi có cái đó nhìn cũng được thì up lên chơi

mở file đính kèm bằng sql 2008 và vs2010 nha.

bắt đầu bằng các chúng ta viết javascript cho gridview cần sự kiện :


<script type="text/javascript">

var lastColor;

function DG_changeBackColor(row, highlight) {

if (highlight) {

row.style.cursor = "hand";

lastColor = row.style.backgroundColor;

row.style.backgroundColor = 'yellow';

}

else

row.style.backgroundColor = lastColor;

}



</script>







sau đó gọi đến sự kiện RowDataBound của gridview cần đổi màu :





protected void gvw_danhsach_nhapma_bk1_RowDataBound(object sender, GridViewRowEventArgs e)

{

if ((e.Row.RowType == DataControlRowType.Pager) || (e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.Footer))

{

return;

}

else

{

//Thêm sự kiện để thay đổi mày nền khi di chuyển con trỏ qua 1 dòng

e.Row.Attributes.Add("onmouseover", "DG_changeBackColor(this, true);");

e.Row.Attributes.Add("onmouseout", "DG_changeBackColor(this,false);");

// e.Row.Attributes.Add("onclick", "DG_changeBackColor(this,true);");

}

}




file đính kèm




sau khi làm xong được như hình :

Tuesday, April 2, 2013

Chuột phải trong datagridview

by Unknown  |  at  3:21 AM


nay gặp phải trường hợp người dùng muốn in lại dữ liệu chọn trên datagridview lên làm cái chọn chuột phải hiện cái menustrip lên cho nó hoành tráng :))




// sự kiện load nho load cai menustrip lên


public void BC34GiaoDich_Load(object sender, EventArgs e)

{



sukieninlai();

}

// khai báo in lại


ContextMenuStrip contextMnStrip = new ContextMenuStrip();

private void sukieninlai()

{

contextMnStrip.Items.Add("In Lại");

contextMnStrip.MouseClick += new MouseEventHandler(Print_InLai);

}

//sự kiện in lại

private void Print_InLai(object serder, MouseEventArgs e)

{

//MessageBox.Show("Sự kiện in lại");

if (mact != null && mact != "")

{

ChiTietBC34 fromct = new ChiTietBC34(IDBuuCucGuiBC37, dtpNgay.Value, mact, idbuucucnhanct, sochuyenthuct, sotuict, trongluongct, tuifct, ghichuct);

fromct.Show();

}

else {

MessageBox.Show("Dữ liệu theo lô chưa lưu! Chưa thể in lại");

return;

}

}

// khai báo biến cho from in lại


string mact;

int idbuucucnhanct;

string sochuyenthuct;

int sotuict;

double trongluongct;

string tuifct;

int ghichuct;

// sự kiện click chuột phải để in lại

private void dgvBC34_MouseDown(object sender, MouseEventArgs e)

{

DataGridView.HitTestInfo hti;

if (e.Button == MouseButtons.Right)

{

hti = dgvBC34.HitTest(e.X, e.Y);

if (hti.Type == DataGridViewHitTestType.Cell)

{

if (!dgvBC34.Rows[hti.RowIndex].IsNewRow)

{

if (hti.Type == DataGridViewHitTestType.Cell && hti.RowIndex == hti.RowIndex)

{



contextMnStrip.Show(dgvBC34, new Point(e.X, e.Y));

dgvBC34.Rows[hti.RowIndex].Selected = true;

dgvBC34.CurrentCell = dgvBC34[hti.ColumnIndex, hti.RowIndex];

//Truyền biến sang from chi tiết

try

{



mact = this.dgvBC34.CurrentRow.Cells["Ma"].Value.ToString();

idbuucucnhanct = Int32.Parse(this.dgvBC34.CurrentRow.Cells["BuuCucNhan"].Value.ToString());

sochuyenthuct = this.dgvBC34.CurrentRow.Cells["SoChuyenThu"].Value.ToString();

sotuict = Int32.Parse(this.dgvBC34.CurrentRow.Cells["SoTui"].Value.ToString());

trongluongct = double.Parse(this.dgvBC34.CurrentRow.Cells["TrongLuong"].Value.ToString());

tuifct = this.dgvBC34.CurrentRow.Cells["TuiF"].Value.ToString();

ghichuct = Int32.Parse(this.dgvBC34.CurrentRow.Cells["GhiChu"].Value.ToString());



}

catch { MessageBox.Show("Anh/Chị Hãy xem lại dữ liệu chọn"); return; }



}





}

}

}

}

//// không có file đính kèm vì nó trong project của mình ko thể sent đươc ^^!

Monday, April 1, 2013

In report trên asp.net

by Unknown  |  at  11:06 PM


làm web báo cáo ko thể in được theo cái nut in của crytal report ức chế chơi kiểu convert nó về pdf rùi in ^^!


đây là 1 vidu cũng hay , mọi ng xem thấy hay thì thanks phát hehe


+ database Test có 1 bảng a(id,ten)


load dữ liệu in


CrystalReport1 cr = new CrystalReport1();





protected void Page_Load(object sender, EventArgs e)


{


SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString);


SqlDataAdapter da = new SqlDataAdapter("select *from a", conn);


DataSet ds = new DataSet();


da.Fill(ds);


DataTable dt = new DataTable();


dt = ds.Tables[0];





cr.SetDataSource(dt);


CrystalReportViewer1.ReportSource = cr;


CrystalReportViewer1.DataBind();


}


// đây nút xuất ra nè


protected void Button1_Click(object sender, EventArgs e)


{





MemoryStream oStream ; // using System.IO


oStream = (MemoryStream)


cr.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);


Response.Clear();


Response.Buffer = true;


Response.ContentType = "application/pdf";


Response.BinaryWrite(oStream.ToArray());


Response.End();


}


file đính kèm ^^!

Kiểm tra máy in off hay online

by Unknown  |  at  5:59 PM


Đang in mà máy in nó tự động offline thì toi :)) đó là trường hợp máy in cơ quan mình , ức chế code luôn cho nó ^^!

đoạn code sau sẽ kiểm tra xem máy in đang off hay on nha hehe bạn còn có thể tự bật nó bằng code nữa nha ^^! rảnh thì làm tiếp phần đó










// lấy tên các máy in theo máy




private void Form1_Load(object sender, EventArgs e)

{

foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)

{

comboBox1.Items.Add(printer);

// MessageBox.Show(printer);

}




}

//kiểm tra off hay ko?




private void button2_Click(object sender, EventArgs e)

{

ManagementObjectCollection MgmtCollection;

ManagementObjectSearcher MgmtSearcher;

//Perform the search for printers and return the listing as a collection

MgmtSearcher = new ManagementObjectSearcher("Select * from Win32_Printer");

MgmtCollection = MgmtSearcher.Get();

foreach (ManagementObject objWMI in MgmtCollection)

{

string name = objWMI["Name"].ToString().ToLower();

if (name.Equals(comboBox1.Text.ToLower()))

{

if (objWMI["WorkOffline"].ToString().ToLower().Equals("true"))

{

// printer is offline by user

MessageBox.Show("khong mo");

//objWMI["WorkOffline"].ToString() = "false";

}

else

{

// printer is online

MessageBox.Show("da mo");

}




}




}

}

Bạn có thể xem file đính kèm ^^!



Thursday, March 28, 2013

Mã hóa chuỗi bằng c#

by Unknown  |  at  2:39 AM
Sau đây là 2 ví dụ làm mã hóa chuỗi theo dich chuyển và rsa mà tôi demo qua
các bạn có thể tham khảo

XML với C#

by Unknown  |  at  2:37 AM
xml dùng với c# thật tuyệt với nó sẽ giúp bạn trong nhiều trường hợp đó
sau đây là các thao tác cơ bản với xml theo 2 các mà tôi biết

Gửi mail bằng C#

by Unknown  |  at  2:34 AM
1 ứng dụng gửi mail bằng c# cũng tạm ổn, bạn hãy đặt thời gian cho nó mà làm bom thư,keylog =))

Các thao thác cơ bản trực tiếp trên gridview của asp.net

by Unknown  |  at  2:30 AM
Nhiều lúc bạn ko muốn dùng tool có sẵn của VS khi dùng gridview để thao tác với dữ liệu thì hãy dùng code sau để thêm sửa xóa trược tiếp dữ liệu trên gridview nè

Proudly Powered by Blogger.