Upload
lihua
View
39
Download
2
Embed Size (px)
DESCRIPTION
Công nghệ NET Web Service. Lương Trần Hy Hiến, Khoa CNTT, ĐH Sư phạm TpHCM. Nội dung. Giới thiệu XML Giới thiệu Web Services Xây dựng Web Services Sử dụng Web Services. CƠ BẢN VỀ XML. XML(e X tensible M arkup L anguage): - PowerPoint PPT Presentation
Citation preview
1
Công nghệ NET
Web ServiceCông nghệ NET
Web Service
Lương Trần Hy Hiến, Khoa CNTT, ĐH Sư phạm TpHCM
2
Nội dungNội dung
• Giới thiệu XML• Giới thiệu Web Services• Xây dựng Web Services• Sử dụng Web Services
CƠ BẢN VỀ XMLCƠ BẢN VỀ XML
• XML(eXtensible Markup Language):
– Là một bộ qui luật về cách chia một tài liệu ra làm nhiều phần, rồi đánh dấu và ráp các phần khác nhau lại để dễ nhận diện chúng
Khái niệm cơ bảnKhái niệm cơ bản
<name><first>John</first><middle>Fitzgerald Johansen</middle> <last>Doe</last>
</name>
start -tag
end -tag
element
element content,Parsed Character DATA
(PCDATA),text content,
text node
root element
Khái niệm cơ bảnKhái niệm cơ bản
• Tất cả start-tag đều có end-tag.• self-closing tag: <middle/>• Những tag không thể chồng chéo nhau
– Sai : <P>Some <STRONG>formatted <EM>text</STRONG>, but</EM>no grammar no good!</P>
– Đúng : <P>Some <STRONG>formatted <EM>text</EM></STRONG><EM>,
but</EM>no grammar no good!</P>
• Mỗi tài liệu XML chỉ có duy nhất 1 root element.– Sai : <name>John</name><name>Jane</name> – Đúng : <names>
<name>John</name><name>Jane</name>
</names>
Cấu trúc của tài liệu XMLCấu trúc của tài liệu XML
SỬ DỤNG XML READERSỬ DỤNG XML READER
– AttributeCount: Số thuộc tính của node– EOF: đi đến cuối stream– Depth: Độ sâu của node hiện hành– IsEmptyElement: chỉ ra node hiện hành
có rỗng không– Name: tên node hiện hành– Value: lấy giá trị của node hiện hành– ValueType: lấy kiểu giá trị của node hiện
hành
Các phương thức của XMLReaderCác phương thức của XMLReader• Closed(): Đóng XMLReader• GetAttribute()• IsStartElement()• MovetoAttribute()• MovetoNextElement()• ReadtartElement()• ReadContent()
SỬ DỤNG XML READERSỬ DỤNG XML READER
• Khai báo không gian tên:– Using System.Xml
• Mở tài liệu XML:
• Tạo đối tượng: sử dụng phương thức Create()
SỬ DỤNG XML READERSỬ DỤNG XML READER
• Đọc tài liệu:
• Đọc thuộc tính:– Có thể truy cập qua tên hoặc chỉ số:
SỬ DỤNG XML READERSỬ DỤNG XML READER
Ghi dữ liệu XML với XmlWriterGhi dữ liệu XML với XmlWriter• Các thuộc tính:
– Settings: tạo đối tượng XmlWriterSettings
– writeState: trạng thái của bộ ghi• Phương thức:
– Close(): đóng dòng dữ liệu hiện hành– Create(): tạo đối tượng XmlWriter– WriteAttributes: ghi tất cả các thuộc tính
tại vị trí hiện hành trong đối tượng XmlReader
Ghi dữ liệu XML với XmlWriterGhi dữ liệu XML với XmlWriter
– WriteAttributeString: ghi thuộc tính với giá trị chỉ định
– WriteChars: ghi khối lượng lớn dữ liệu trong bộ đệm tại một thời điểm
– WriteElementString: ghi một phần tử chứa chuỗi giá trị chỉ định
– WriteStartAttribute: ghi mở đầu thuộc tính
– WriteStartDocument: ghi khai báo XML– WriteStartElement: ghi tag mở– WriteString: ghi nội dung văn bản
Sử dụng XmlWriterSử dụng XmlWriter
• Bắt đầu và kết thúc tài liệu–WriteStartDocument()
• Ghi chỉ thị: <?xml version=“1.0”>
–WriteEndDocument()• Đóng các thuộc tính và phần tử
Ghi dữ phần tửGhi dữ phần tử
• Ví dụ:<name>
<firstName>Laura</firstName>
</name>Writer.WriteStartElement(“name”)Writer.WriteElementString(“firstName”,
“Laura”)
Using (XmlWriter writer=XmlWriter.Create(xmlFilePath))
{ Writer.WriteStartDocument(false);
Writer.WriteComment(“This XML file represents thi details of a table Employees”)
Writer.WriteStartElement(“Employees”);
Writer.WriteStartElement(“Employee”);
Writer.WriteAttributeString (“id”, “1”);
Writer.WriteStartElement(“name”);
Writer.WriteElementString(“firstName”,” Laura”);
Writer.WriteElementString(“lastName”,” Callahan”);
Writer.WriteEndElement();
Writer.WriteElementString(“City”,” Seattle”);
Writer.WriteElementString(“state”,” WA”);
Writer.WriteElementString(“zipcode”,” 98122”);
Writer.WriteEndElement();
Writer.WriteEndElement();
Writer.WriteEndDocument();
Writer.Flush();
lblResult.Text=”File is written successfully”;
}
17
Giới thiệu Web ServicesGiới thiệu Web Services
Giới thiệu Web ServicesGiới thiệu Web Services
• Các nhược điểm của DCOM (Distributed Component Object Model):– Gửi các thông tin dưới dạng nhị phân– Bị chặn bởi Firewall và Proxy Server– Truy cập thông qua giao thức Web TCP/IP.
• Webservices xây dựng trên SOAP (Simple Object Access Protocol) =>gọi thực hiện và trả về kết quả là dạng TEXT (XML) =>có khả năng hoạt động xuyên qua Firewall:– Khả năng làm việc trên mọi hệ điều hành– Mở rộng khả năng phối hợp giữa các ứng dụng– Tăng cường giao tiếp giữa Client/Server
• Ví dụ web services: – Đường sắt Việt Nam cung cấp các web services cho phép đặt
mua vé tàu– Các nhà hàng, khách sạn cung cấp các web services cho phép
đặt tiệc, đặt phòng– …
Web ServicesWeb Services
Network
Network may be LAN, WAN, MAN, or the Internet
Web Services: Internet-based modular applications
Web Service
Web Services SolutionsWeb Services Solutions
Two of the most popular Web Services Solutions:
SUNTM OPEN NET ENVIRONMENT (SUN ONE)
Characteristics of Web ServicesCharacteristics of Web Services
Should be registered
Uses XML
Use standard Web protocols
Accessed over the Web
Web Services
Have a service interface
Supported by loosely coupled
applications
Integrated Just In Time
Important ComponentsImportant Components
Service Broker
ServiceRequestor
ServiceProvider
Service Registry
PUBLISH
FIND
BIND
1. Create a Web service
2. Define service interface and invocation methods for the Web service
3. Publish the Web service on the Internet or intranet
5. Invoke the Web service
6. Unpublish the Web service when not needed
4. Search for the Web service
Life CycleLife Cycle
Working of a Web ServiceWorking of a Web Service
Soap ProtocolXML over HTTP
Parameters
Return Value
Get Type info
(XML Schema)
Service Description
Web Server (With Web Service)
Client Application
Inte
rnet
Application ModelApplication Model
Web Services
Web Services
Databases
WinForm
WebForm
Java
Web ServiceConsumer
Web Browser
Users
CommunicationCommunication
Web Service Consumer
Web ServiceP
roxy
WSDL
download
SOAP Message
Call Web Method through Proxy
27
Xây dựng 1 Web ServicesXây dựng 1 Web Services
Tạo Web ServiceTạo Web Service
Tạo Web Services (tt)Tạo Web Services (tt)
Kiểm tra Web ServiceKiểm tra Web Service
Sử dụng Web Services do mình tạo raSử dụng Web Services do mình tạo ra
Sử dụng Web Services do mình tạo raSử dụng Web Services do mình tạo ra
Sử dụng Web Service miễn phí trên mạngSử dụng Web Service miễn phí trên mạng
• www.webservicex.net
Các webservices thông dụngCác webservices thông dụng• http://www.webservicex.net/currencyconvertor.asmx?wsdl• http://www.webservicex.net/globalweather.asmx?wsdl• http://www.webservicex.net/country.asmx?wsdl
Các bước sử dụng webserviceCác bước sử dụng webservice
Các bước sử dụng webservice (tt)Các bước sử dụng webservice (tt)
Các bước sử dụng webservice (tt)Các bước sử dụng webservice (tt)
Các bước sử dụng webservice (tt)Các bước sử dụng webservice (tt)
Các bước sử dụng webservice (tt)Các bước sử dụng webservice (tt)
• Sau khi thêm các dịch vụ:
Thể hiện các thông tin của webserviceThể hiện các thông tin của webservice
Thể hiện các thông tin của webserviceThể hiện các thông tin của webservice
Virtual BankVirtual Bank
Online Shopping Cart
V-BankWeb Service
Web Browser
Server
Client
Bank Account Web ServiceBank Account Web Service
• Account(Id, PinCode, Name, Balance)• Withdraw(Id, PinCode, Money)• Deposit(Id, PinCode, Money)• Transfer(Id, PinCode, Money, Id2)
ATM User InterfaceATM User Interface