You are on page 1of 42

Windows

Communication
Foundation
(formerly known as Indigo)

Mathias Olausson (mo@callista.se)


Callista Knowledgebase
Agenda
• Introduction
• WCF Concepts
• Programming WCF
Introduction
What is WCF?
• Part of the new WinFX platform
• New, unified programming model for
building connected applications on the
Windows platform
• Scale-invariant
– “Pay-as-you-go” model
What is WCF?
• Platform for advanced Web Services
– Standards based
– Building secure, reliable, transacted
solutions
– Best choice for implementing SOA
applications
• Integration across platforms
• Client- or server-side component
What is WCF?
• Unified Technologies
– ASMX Web Services
– Web Service Enhancements, WSE
– .NET Remoting
– Enterprise Services, COM+
– MSMQ
• Interoperability with existing
investments
Meta information
• History
– Publicly previewed at PDC 2003
– Currently in beta 2
– Due for release in 2006
• Supported platforms
– Windows XP SP2
– Windows Server 2003
– Windows Vista
– Windows Server ”Longhorn”
• Requirements
– .NET Framework 2.0
WCF Concepts
Clients and Services

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

wsHttpBinding WS Message Yes Yes n/a

wsFederationBinding WS-Federation Message Yes Yes Yes

wsDualHttpBinding WS Message Yes Yes Yes

netTcpBinding .NET Transport Yes Yes Yes

netNamedPipeBinding .NET Transport Yes Yes Yes

netMsmqBinding .NET Transport Yes Yes No

netPeerTcpBinding Peer Transport No No Yes


msmqIntegrationBinding MSMQ Transport No Yes No
Configuration
<system.serviceModel>
<services>
<service
name="OrderService.OrderManager"
<!-- use base address provided by host -->
<endpoint address="http://host:8080/OrderService"
binding="wsHttpBinding"
bindingConfiguration="OrderServiceBinding"
contract="OrderService.IOrderManager" />
</service>
</services>

<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

// Define a service contract.


[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface IDataContractCalculator
{
[OperationContract]
ComplexNumber Add(ComplexNumber n1, ComplexNumber n2);
[OperationContract]
ComplexNumber Subtract(ComplexNumber n1, ComplexNumber n2);
[OperationContract]
ComplexNumber Multiply(ComplexNumber n1, ComplexNumber n2);
[OperationContract]
ComplexNumber Divide(ComplexNumber n1, ComplexNumber n2);
}
Data Contract
• Defines data structures
• Maps CLR types to XML Schema
• [DataContract] specifies types
• [DataMember] specifies members
– Can be applied to private fields!
A Data Contract
[DataContract]
public class ComplexNumber
{
[DataMember]
public double Real = 0.0D;
[DataMember]
public double Imaginary = 0.0D;
}
Behavior
• Modifies or extends service or client runtime
behavior
• Examples
– Instancing; Singleton, PrivateSession,
SharedSession, PerCall
– Concurrency; Multiple, Reentrant, Single
– Throttling; connections, threading
– Metadata customization
– Transactions; AutoEnlist, Isolation, AutoComplete
Features Summary
Address Binding Contract Behavior
HTTP WS-Security Request/ Instancing Concurrency
http://... Response
Transport Protocol Behavior Behavior

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

Externally visible, Opaque, per-service,


per-endpoint endpoint, or operation
ServiceHost

Description Runtime

Listeners
Behaviors Endpoints

Runtime Address

Binding
Sites

Extensions

• Formats, sends and Contract

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

You might also like