Professional Documents
Culture Documents
Communication
Foundation
(formerly known as Indigo)
Client Service
Message
Endpoints
• The unit of communication
• The ABC of WCF
– Address – where to expose
– Binding – how to expose
– Contract – what to expose
• Defined in code or in configuration
Service
A B C
Message
C B A A B C
A B C
Configuration
<system.serviceModel>
<services>
<service
name="OrderService.OrderManager"
<!-- use base address provided by host -->
<endpoint address="http://host:8080/OrderService"
binding="wsHttpBinding"
contract="OrderService.IOrderManager" />
</service>
</services>
</system.serviceModel>
Address
• Defines where a service is located
• Specifies a URI where the service is located
– Relative or absolute
• Address consist of
– Scheme
• HTTP, TCP, Named pipes, MSMQ
– Machine
– [Port]
– Path
• Examples
– http://www.mystore.com/StoreFront
– net.tcp://mycomputer:9000/StoreFront
Binding
• Describes how a service communicates
• Specifies set of binding elements
– Transport; http, tcp, np, msmq
– Encoding format; text, binary, MTOM, ...
– Security requirements
– Reliable session requirements
– Transaction requirements
• Set of predefined standard bindings
– Can be customized
• Custom binding
Predefined bindings
Binding Interop Security Session Transactions Duplex
basicHttpBinding Basic Profile 1.1 Transport No n/a n/a
<bindings>
<wsHttpBinding>
<binding configurationName="OrderServiceBinding"
maxMessageSize="65536"
transferTimeout="00:10:00"
hostnameComparisonMode="StrongWildcard"
messageEncoding="Text"
</binding>
</wsProfileBinding>
</bindings>
</system.serviceModel>
Contract
• Defines what a service communicate
• Supports contract first or code first models
– Contract first = interfaces
– May use WSDL first as well
– Message vs. RPC style APIs
• Service contract
• Data contract
Service Contracts
• Describes what a service does
• Maps CLR types to WSDL
• [ServiceContract] defines the service contract
• [OperationContract] specifies operations
• One-way, request-reply, duplex MEPs,
Sessions, Faults
A Service Contract
Peer
net.p2p://...
Transport
Throttling
Behavior
TCP WS-RM
net.tcp://... Protocol
Transport
Metadata
Behavior
net.pipe://...
NamedPipe WS-Coord
Transport Protocol One-Way
Error Transaction
Behavior Behavior
net.msmq://...
MSMQ Duplex
Transport Channel
Custom Security
Duplex Behavior Behavior
xxx://...
Custom Custom
Transport Protocol
Description Runtime
Listeners
Behaviors Endpoints
Runtime Address
Binding
Sites
Extensions
receives messages
• Dispatches message to service
operation
• Attributes, code and config are used to
dynamically create a ServiceHost
instance
– Precedence: attribute, code, config
Hosting
• ASP.NET, .SVC format (<%@ServiceHost %>)
– IIS 6: HTTP transport only
– IIS 7: All transports
• Windows Activation Service, WAS
– Same process management, health, and activation features
as IIS without requiring a Web server.
• Self hosted, ServiceHost
– Hosted in any CLR appDomain
– All transports
• IIS/WAS is the expected hosting model
Metadata
• Exposed through WS-
MetadataExchange Endpoints
– Enabled by default, can be disabled
through behavior
• Consumed using svcutil.exe
– Generates code and configuration
Diagnostics
• Logging
– Request and reply messages
• Tracing
– End-to-end trace of [correlated] events
• WMI events
• Performance monitor counters
• Event Log
Programming WCF
Building a WCF application
• Application Design
• Service
1. Define Contracts
2. Implement Contracts
3. Define Endpoints
4. Host & Run Service
• Client
1. Generate Proxy from Metadata
2. Implement & Run Client
Application Design
Application Design
Order Client Admin Client
Order Service
Define Contracts
• Service Contracts
– Order client and admin API (duplex)
• Data Contracts
– Shopping cart
– Item
Implementing Contracts
• One class per service interface
• Class may specify ServiceBehavior
attributes
Defining Endpoints
• One per service interface
– wsHttpBinding,
wsHttpDualBinding
– Rember WCF ABC!
• Can be specified in code or
configuration
– Configuration is to be preferred
Endpoint Configuration
<?xml version="1.0" encoding="utf-8" ?>
<configuration
xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.serviceModel>
<services>
<service
name="OrderService.StoreFront" >
<endpoint
address="http://localhost:8000/StoreFront"
binding="wsHttpBinding"
contract="OrderService.IStoreFront" />
</service>
<service
name="OrderService.StoreFrontAdmin" >
<endpoint
address="http://localhost:8000/StoreFrontAdmin"
binding="wsDualHttpBinding"
contract="OrderService.IStoreFrontAdmin" />
</service>
</services>
</system.serviceModel>
</configuration>
Hosting
• Self hosted in console application
– One ServiceHost per ServiceType
Generating Proxies
• Generated dynamically from service
– svcutil.exe
– Note: service must be running
• Generate proxy and configuration files
– svcutil http://localhost:8000/StoreFront
/out:proxy.cs /config:app.config
– Proxy generator add-in for Visual Studio
Implementing the Client
• Client configuration obtained from
generated code
• Statless server calls
– Remember to close proxy correctly
Implementing the Admin Client
• Again, client configuration obtained
from generated code
• Duplex callback interface
implementation
– Requires stateful connection!
Summary
Mappings to SOA
• Boundaries are Explicit
– Service and data contracts
• Services are autonomous
– Independent deployment, versioning, and security
• Share Schema, not Class
– Integration based on message formats and
exchange patterns, not classes and objects
• Policy-based compatibility
– Service compatibility based on policy assertions
(behaviors)
Summary
• WCF is the next generation Web
Services from Microsoft
• Learn your ABC
• Define contracts first
• Supports building SOA
– But not limited to only that
• Unified programming model
References
• WCF Official Web Site
http://windowscommunication.net/Default.aspx?tabindex=0&tabid=1
• WCF on MSDN
http://msdn.microsoft.com/webservices/indigo/default.aspx