1
WCF Overview
Peter Himschoot
peter@u2u.net
Blog:http://www.u2u.info/Blogs/Peter
Agenda
• Shed some light on the many .NET versions
2
• Understand WCF
– Why?
– Concepts
– Code
• Some features
– Client Callbacks
– Messaging
– JSON
A History of .NET
Framework Runtime Visual Studio C# VB.NET
1.0 1.0 2002 1.0 7.0
1.1 1.0 2003 1.0 7.0
2.0 2.0 2005 2.0 8.0
(generics) (generics)
3.0 2.0 2005 2.0 8.0
(WCF,WPF,WF)
3.5 2.0 2008 3.0 9.0
(LINQ) (LINQ)
5
Why?
Questions We Hear a Lot…
6
“How should I build “How can I send
service-oriented messages securely &
systems?” reliably?”
“How can I develop
interoperable “What API
applications?” should I use?”
MS Distributed Technologies
.NET 1.0/2.0
• System.Web.Services (ASMX)
– XML, HTTP, SOAP/WSDL/XSD
– Focused on interop
• System.EnterpriseServices (COM+)
– DCOM RPC, TCP
– Focused on high-performance transaction processing
• System.Messaging (MSMQ)
– Focused on durable transacted messaging
• System.Runtime.Remoting (.NET Remoting)
– HTTP or TCP, SOAP or Binary
– Tightly coupled to .NET type system
WCF’s goals
• Unify the developer experience for building distributed apps
– Consistent conceptual model across varying scenarios
– Common set of idioms + metaphors that work everywhere
• Scenario parity with the existing stacks
– Critical for adoption
• Architectural foundation for the future
– WS-* (.NET 3.0)
– Web-style services (.NET 3.5)
– Workflow Services (.NET 3.5)
– Distributed durable workflows
– Large-scale pub/sub
– <your scenario here>
WCF Fundamentals
• Windows Communication Foundation (WCF)
9
– is a runtime for message based communication
– development API
• for creating systems that send messages between
services and clients
• unified API across different transport mechanisms
• For applications that communicate with other
applications
– on the same computer system
– on external computer system
– in another company accessible over the Internet.
10
Concepts
Clients and Services
Client Service
Message
11
Endpoints
Client Service
Message Endpoint
Endpoint Endpoint
Endpoint
12
Address, Binding, Contract
Client Service
Message A B C
C B A A B C
A B C
Address Binding Contract
Where? How? What?
Endpoint
13
Some Standard Bindings
BasicHttpBinding BP 1.1 TM
WsHttpBinding WS TM
WsDualHttpBinding WS TM
NetTcpBinding .NET TM
NetNamedPipesBinding .NET TM
NetMsmqBinding MSMQ TM
NetPeerTcpBinding Peer TM
T = Transport Security | M = Message Security | O = One-Way Only
15
Coding with VS2008
Create Service Contracts
16
WCF References
17
[ServiceContract]
• Defines what the service can do
18
Operation
• Operation =
19
Response = f (Request)
[DataContract]
• Define the message
20
Deeper Example
21
[FaultContract]
• Operations can return errors (Fault msg)
22
• Operations should say so
Implement the Service
• Add another WCF Service Library
23
– Implementation should be loosely coupled
Throwing Errors
• FaultException<T>
24
Host the Service
• Hosting should be loosely coupled
25
WeatherService.svc
26
<%@ ServiceHost
Service="WCF_ServiceImpl.WeatherServiceImpl"
%>
Web.config
• Add service endpoints
27
– Contract
– Binding
– Address
Web.config with endpoint
28
View in Browser…
29
Add Meta Data
• Added through behavior
30
Hosting Options
• IIS
31
– Or Cassini for development
• Console
• Windows application
• WAS
– Windows Activation Service
• …
The WCF Client
• Client can be any kind of project
32
– Console, Windows
– ASP.NET, Silverlight
–…
Create the Client
• Create client
33
• Then Add Service Reference
Add Service Reference
34
App.config
• Service reference also add .config
35
Create Proxy
36
From
.config
37
Client Callbacks
WCF Message Patterns
38
One Way
Client Service
Request-Reply
Duplex (Dual)
• One Way:
– Datagram-style delivery
• Request-Reply
– Immediate Reply on same logical thread
• Duplex
– Reply “later” and on backchannel (callback-style)
Contract: Callback
• Client can pass callback interface
39
• Client becomes server
• Only possible on some bindings
– netTcpBinding, wsDualHttpBinding, …
Client Proxy Instantiation
• Client needs to pass itself to proxy
40
• Typically implemented by a class
Server-side Callback
• Callback is passed in OperationContext
41
42
Messaging
43
ASP.NET, JSON, Silverlight