2. Nội dung
• Windows Communication Foundation là gì?
• Kiến trúc hướng dịch vụ (SOA) và WCF
• Làm gì để xây dựng một ứng dụng WCF?
• Một dịch vụ WCF hoạt động như thế nào?
• Hosting một dịch vụ WCF
3. Vấn đề phát sinh
Các phần mềm hiện nay yêu cầu khả năng tích
hợp cao
… Xây dựng các hệ thống có tính kết nối là rất khó!
1. Có rất nhiều công nghệ giải quyết cho
vấn đề truyền thông và tích hợp hiện nay
Chọn cái nào?
2. Nhu cầu thay đổi nền tảng ứng dụng
xảy ra bất cứ lúc nào
Làm sao để chuyển đổi mà tiết kiệm chi phí
5. Windows Communication Foundation
Mô hình thống nhất thay thế cho các công nghệ phân tán trước đó
ASMX Remoting
Dịch vụ Web Mô hình từ xa
ASP.NET
ES/COM+ WSE MSMQ
Lập trình nền Lập trình nền
thuộc tính Dịch vụ Web
nâng cao thông điệp
6. Mục tiêu thiết kế WCF
“Mô hình lập trình thống nhất nhằm phát triển nhanh các ứng
dụng hướng dịch vụ trên nền Windows”
Thống nhất về công nghệ
Giữ nguyên ưu điểm
Thích hợp cho nhiều Unification
mục đích sử dụng Thống nhất
Là một mô hình lập Tương tác và tích hợp
trình hướng dịch vụ Productive SO Interoperability với các ứng dụng trên
Đáp ứng 4 tiêu chí Programming & Integration các nền tảng khác
của lập trình hướng Đẩy mạnh lập Tương tác và nâng cấp
Khả năng tương
dịch vụ trình hướng dịch các ứng dụng phân tán
tác và tích hợp
vụ
hiện có
7. SOA và WCF
WCF đã được thiết kế với các nguyên lý sau của SOA
Rõ ràng về ranh Các ứng dụng và dịch vụ liên lạc với nhau
thông qua các thông điệp mà không quan tâm
giới đến việc xử lý và tiếp nhận
Dịch vụ và sử dụng dịch vụ là độc lập với các
Tự phát triển quá trình nâng cấp phiên bản, triển khai, hoạt
động và bảo mật.
Chia sẻ yêu cầu,
Các dịch vụ cung cấp các phép toánvà cấu trúc
không chia sẻ nội thông tin. Không bao gồm nội dung thông tin
dung
Các dịch vụ có thể thiết kế để độc lập với việc
Tương thích dựa
triển khai, thống nhất với các ứng dụng về
trên chính sách chuẩn giao tiếp
8. Các lợi ích của SOA
Dễ dàng thự thi
Độc lập triển khai, phát triển và quản lý
Nâng cao khả năng tái sử dụng công nghệ
Giảm chi phí cho việc cập
nhật và bảo trì phần mềm
9. Xây dựng một ứng dụng WCF
Xây dựng một dịch vụ WCF
Xây dựng một ứng dụng client
sử dụng dịch vụ WCF
10. Xây dựng dịch vụ WCF
• Định nghĩa các Contracts
• Data Contract
• Operation Contract
• Thi hành các dịch vụ
• Cấu hình, triển khai và kiểm thử các dịch vụ
• Cấu hình services trong configuration file
• Triển khai lên IIS 7
• Kiểm thử
11. Data Contract
• [DataContract]: cho biết một class được khai
báo có thể được serialized và deserialized như một
XML stream bởi WCF
• [DataMember]: cho biết
thuộc tính nào sẽ được
serialized và deserialized
12. Service Contract
• [ServiceContract ]: cho biết một interface là một
service contract. WCF dựa vào thuộc tính này
để sinh ra metadata cho client khi sử dụng
• [OperationContract]: cho biết phương thức nào sẽ được
đưa vào sử dụng
13. Thi hành các dịch vụ
• Một class được cung cấp bởi một dịch vụ WCF phải
thực thi đầy đủ một service contract.
14. Cấu hình các dịch vụ WCF
• ProductService.svc
• Web.config
<serviceModel>: chứa thông tin cấu
hình cho một dịch vụ Web WCF
<services>: chứa thông tin chi tiết
của mỗi service được thực thi
<service>: chỉ ra namespace và
class mà thực thi service
<endpoint>: cung cấp thông tin chi
tiết của service mà các ứng dụng
client yêu cầu theo thứ tự để liên
lạc với service
<behavior >: cho phép metadata
được công bố tới client
19. Kiến trúc ABCs của WCF
Service Host
Client Service
Bv A B C Bv
Bv C B A Message A B C Bv
Address Binding Contract
(Where) (How) (What)
Endpoint
20. Endpoints
• Address: Where?
– Chỉ ra nơi nào để gửi và nhận thông điệp
– Chỉ ra nơi nào để tìm ra Endpoints
• Binding: How?
– Chỉ ra làm cách nào để gửi thông điệp
– Chỉ ra làm sao để client liên lạc với Service endpoints
• Contract: What?
– Chỉ ra nhưng cái gì được bao gồm trong thông điệp
– Chỉ ra những dịch vụ nào client có thể sử dụng
• Behaviours:
– Triển khai chi tiết
21. Address
• Nơi để truyền và nhận thông điệp
• Cung cấp thông tin:
– Đường dẫn
– Giao thức vận chuyển
– Tên Host
– Cổng truy cập [transport]://[machine][:optional port]
http://localhost
http://localhost:8081
http://localhost:8081/Service
net.tcp://localhost:8082/Service
net.pipe://localhost/Pipe
22. Address URI Peer to Peer
Scheme net.p2p://localhost/MyService
NetPeerTcpBinding
Host
MSMQ
Port net.msmq://localhost/private/MyService
Path NetMsmqBinding
Named Pipes TCP
net.pipe://localhost/MyService net.tcp://localhost:8088/MyService
NetNamedPipeBinding NetTcpBinding
HTTP
Chia sẻ Port thông qua
http://localhost:80/MyService TCP Port Sharing Service
Chia sẻ Port thông qua BasicHttpBinding
HTTP.SYS WSHttpBinding
WSDualHttpBinding
WSFederationHttpBinding
23. Bindings (Gắn kết)
• Mô tả cách Client kết nối với services
• Định dạng dữ liệu mong muốn
• Có thể bao gồm các thông tin
– Giao thức vận chuyển
– Định dạng mã hóa thông điệp
– Cơ chế bảo mật
– Cơ chế giao dịch
Một số bindings phổ biến
BasicHttpBinding
– Tính tin cậy trong liên lạc NetTcpBinding
WSHttpBinding
NetMsqmqBinding
24. Contracts (Hợp đồng)
• [Service Contract]: Là một Interface
• [OperationContract]: Mô tả các phép toán có thể
thực thi bởi service
– Toàn bộ data phải được serializable
• [Data Contract]: Mô tả cấu trúc dữ liệu và cách mà dữ
liệu được serializable.
26. Hosting: Ứng dụng người dùng
class HelloHost {
static void Main(string[] args) {
ServiceHost host =
new ServiceHost(typeof(HelloService));
host.Open();
Console.ReadLine();
host.Close();
}
}
<service name="HelloService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000"/>
</baseAddresses>
</host>
…
</service>
27. Hosting: Ứng dụng người dùng
• Thuận lợi
– Kiểm soát toàn vẹn
– Không giới hạn trong việc binding/behavior
– Sử dụng các ứng dụng có giao diện
• Khó khăn
– Không có các chức năng quản lý Hosting
28. Hosting: Windows Service
public class WindowsService : ServiceBase {
ServiceHost host;
protected override void OnStart(string[] args) {
host = new ServiceHost(typeof(HelloService));
host.Open();
}
protected override void OnStop() {
host.Close();
}
}
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer {
public WindowsServiceInstaller() {
ServiceProcessInstaller spi =
new ServiceProcessInstaller();
ServiceInstaller si = new ServiceInstaller();
Installers.Add(spi);
Installers.Add(si);
}
}
29. Hosting: Windows Service
• Thuận lợi
– Tự động Start/Stop/Restart
– Có tool quản lý service
– Có thể thực thi ở tài khoản máy cũng như người
dùng
• Khó khăn
– Sự kích hoạt không có thông báo
31. Hosting: IIS7/WAS
• Thuận lợi
– Kích hoạt sẵn có trong IIS
– Hỗ trợ Named Pipes, TCP, and MSMQ
– Sử dụng công cụ quản lý IIS7 mới
• Khó khăn
– Chỉ hoạt động trên VISTA
32. Tóm tắt về WCF
• WCF là công nghệ cho tính toán phân tán
• WCF tích hợp tất cả các công nghệ phân tán tốt nhất
• Sử dụng chuẩn Web Services để tương tác và tích hợp
với các giải pháp đang tồn tại
• WCF sẵn có trong Windows Vista, Windows XP SP2,
Windows Server 2008
33. Tham khảo thêm….
Check out the WCF community site for a list of useful
resources: http://windowscommunication.net
Download and prototype on the WCF CTP Releases:
http://msdn.microsoft.com/windowsvista/getthebeta/d
efault.aspx
Leverage the community forum to post comments and
connect with the feature team:
http://forums.microsoft.com/MSDN/ShowForum.aspx?
ForumID=118&SiteID=1
Hinweis der Redaktion
Speaker NotesThese are the what we refer to as the three pillars of WCF. They are the three design goals of WCF that really sum up what that technology is all about. They’re what we’ll be drilling into for the remainder of this presentation.<click>The first design goal is unification. When we talk about unification, we’re talking about bringing together the various technologies available today for building distributed applications (COM+ and .NET Enterprise services, MSMQ, .NET Remoting, ASP.NET Web Services, Web Services Enhancements (WSE)) into a single, unified programming model. Why is this important? First, it reduces complexity by allowing us to focus on a single programming model rather than learn multiple programming models. Second, it allows us to compose/combine the functionality of today’s technologies in ways that we can’t today. Finally, it allows us to use a single programming model for building distributed applications that communicate with one another on a single machine, across multiple machines, and across the Internet.<click>The second design goal is interoperability and integration. We talk about interoperability and integration on two different pivots. First, WCF enables you to build services that speak advanced Web services protocols (WS-*), enabling your application to communicate with other WS-* compliant services running on other platforms. Second, we built WCF to provide a smooth upgrade and interop story for your distributed applications built on today’s technologies (WSE, ASMX, .NET Enterprise Services, System.Messaging, .NET Remoting). Why are Interop and Integration important?First, the ability to communicate with applications running on other platforms provides you with the flexibility you need when working in a heterogeneous environment.Second, the ability to communicate with existing applications protects your investments and provides you with an optional, incremental upgrade path to WCF.<click>The third design goal is all about productivity and service orientation. WCF provides developers with a highly productive programming model for building service-oriented applications. By using WCF’s attribute-based programming to define your services, you can dramatically reduce the amount of code you write to build secure, reliable services. WCF also facilitates the development of applications that adhere to the “four tenets of service-orientation” that we’ll drill into later in the presentation. These tenets help ensure that your services are extremely flexible and resilient to change, thereby reducing long-term maintenance costs.Transition to next slide:We’ll spend the rest of this presentation drilling into these 3 concepts, but before doing so, let’s quickly discuss some of the basics of WCF…
<serviceModel>: chứathông tin cấuhìnhchomộtdịchvụ Web WCF<services>:chứathông tin chi tiếtcủamỗi service đượcthựcthi<service>: chỉra namespace và class màthựcthi service<endpoint>: cungcấpthông tin chi tiếtcủa service màcácứngdụng client yêucầutheothứtựđểliênlạcvới service<behavior>: chophép metadata đượccôngbốtới client. Mặcđịnh WCF sẽkhôngcôngbốvìlý do bảomật
Self-Host: using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService), new Uri("net.tcp://localhost:9000"))){ host.AddServiceEndpoint( typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "HelloIndigo"); host.Open();Config Base Addresses: <host> <baseAddresses> <add baseAddress="http://localhost:8000"/></baseAddresses></host>Self-Host: using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService), new Uri("net.tcp://localhost:9000"))){ host.AddServiceEndpoint( typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "HelloIndigo"); host.Open();Config Base Addresses: <host> <baseAddresses> <add baseAddress="http://localhost:8000"/></baseAddresses></host>Get off UI thread: [ServiceBehavior(UseSynchronizationContext=false)]Get off UI thread: [ServiceBehavior(UseSynchronizationContext=false)]
Windows Service: Subclass ServiceBase, Override OnStart/OnStop, Subclass Installer (VS support)MethodInvoker del = delegate { this.listBox1.Items.Add(message); }; this.Invoke(del);http://msdn.microsoft.com/msdnmag/issues/01/12/NETServ/
Self-Host: using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService), new Uri("net.tcp://localhost:9000"))){ host.AddServiceEndpoint( typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "HelloIndigo"); host.Open();Config Base Addresses: <host> <baseAddresses> <add baseAddress="http://localhost:8000"/></baseAddresses></host>Self-Host: using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService), new Uri("net.tcp://localhost:9000"))){ host.AddServiceEndpoint( typeof(HelloIndigo.IHelloIndigoService), new NetTcpBinding(), "HelloIndigo"); host.Open();Config Base Addresses: <host> <baseAddresses> <add baseAddress="http://localhost:8000"/></baseAddresses></host>Get off UI thread: [ServiceBehavior(UseSynchronizationContext=false)]Get off UI thread: [ServiceBehavior(UseSynchronizationContext=false)]
Windows Service: Subclass ServiceBase, Override OnStart/OnStop, Subclass Installer (VS support)MethodInvoker del = delegate { this.listBox1.Items.Add(message); }; this.Invoke(del);http://msdn.microsoft.com/msdnmag/issues/01/12/NETServ/
Windows Service: Subclass ServiceBase, Override OnStart/OnStop, Subclass Installer (VS support)MethodInvoker del = delegate { this.listBox1.Items.Add(message); }; this.Invoke(del);http://msdn.microsoft.com/msdnmag/issues/01/12/NETServ/