Windows Communication Foundation
Unified framework for
rapidly building
service-oriented applications
What Does WCF Replace?
ASMX
MSMQ WSE
COM+
.NET
(Enterprise
Remoting
Services)
UNDERSTANDING WCF
PRINCIPLES
Services and Clients
Client Message Service
Message
Endpoints
Client Service
Endpoint
Endpoint Message Endpoint
Address, Binding, Contract
Client Service
Endpoints
Endpoint A B C
C B A Message A B C
Address Binding Contract
(Where) (How) (What)
WCF Architecture: Messaging Runtime
Service Contract
Client and
Dispatcher Behaviors
Protocol(s) Protocol(s)
Encoder
Binding
Encoder
Transport Transport
Address
The what
CONTRACTS
Three Types of Contracts
Service Data Message
Contract Contract Contract
Allows defining
Defines Operations,
Defines Schema application-specific
Behaviors and
and Versioning headers and
Communication
Strategies unwrapped body
Shape
content
Allows control over
What does your What obect data is
the SOAP structure
service do used
of messages
Ways to Talk
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)
What does your service do?
SERVICE CONTRACTS
Service Contract
using System.ServiceModel;
[ServiceContract]
public interface ICalculate
{
[OperationContract]
double Add( double a, double b);
[OperationContract]
double Subtract( double a, double b);
}
Service Contract: OneWay
[ServiceContract]
public interface IOneWayCalculator
{
[OperationContract(IsOneWay=true)]
void StoreProblem (ComplexProblem p);
}
Service Contract: Duplex Asymmetric
[ServiceContract(Session=true,
CallbackContract=typeof(ICalculatorResults)]
public interface ICalculatorProblems
{
[OperationContract(IsOneWay=true)]
void SolveProblem (ComplexProblem p);
}
public interface ICalculatorResults
{
[OperationContract(IsOneWay=true)]
void Results(ComplexProblem p);
}
What object data needs to flow back and forth?
DATA CONTRACTS
Data Contract
[DataContract]
public class ComplexNumber
{
[DataMember]
public double Real = 0.0D;
[DataMember]
public double Imaginary = 0.0D;
public ComplexNumber(double r, double i)
{
this.Real = r;
this.Imaginary = i;
}
}
Defines the mapping between the type and a SOAP envelope
MESSAGE CONTRACTS
Message Contract
[MessageContract]
public class ComplexProblem
{
[MessageHeader]
public string operation;
[MessageBody]
public ComplexNumber n1;
[MessageBody]
public ComplexNumber n2;
[MessageBody]
public ComplexNumber solution;
// Constructors…
}
BINDINGS
Bindings & Binding Elements
Binding
HTTP Text Security Reliability TX
Transport Encoders Protocol
TCP HTTP Text Security Reliability
MSMQ IPC Binary TX .NET
Custom
Custom Custom
Standard Bindings
Binding Interop Security Session TX Duplex
BasicHttpBinding BP 1.1 N, T N N n/a
WSHttpBinding WS M, T, X N, T, RS N, Yes n/a
WSDualHttpBinding WS M RS N, Yes Yes
WSFederationBinding Federation M N, RS N, Yes No
NetTcpBinding .NET T, M T ,RS N, Yes Yes
NetNamedPipeBinding .NET T T, N N, Yes Yes
NetPeerTcpBinding Peer T N N Yes
NetMsmqBinding .NET T, M, X N N, Yes No
MsmqIntegrationBinding MSMQ T N N, Yes n/a
N = None | T = Transport | M = Message | B = Both | RS = Reliable Sessions
Bindings & Behaviors: Security
Client Service
A B C
C B A A B C
Be A B C
Be
Bindings Insert
Claims in
Messages
Behaviors
Implement
Security Gates
Feature Overview
Security
Claims based end-to-end security
Secure end-to-end message exchanges
Secure access to resources
Record resource access requests
X509, Username/Password, Kerberos, SAML,
custom credentials
Message security
Confidentiality and integrity
Transport or message level
Access to resources
Authentication and authorization
Bindings & Behaviors: Transactions
Client Service
A B C
C B A A B C
Be A B C
Be
Bindings Flow
Transactions
Behaviors
AutoEnlist and
AutoComplete
Bindings & Behaviors: Reliable Sessions
Client Service
A B C
C B A A B C
A B C
Bindings provide
Session and
Guarantees
Feature Overview
Reliability and Transactions
End-to-end Reliable messaging
In-order guarantees
Exactly once guarantees
Transport-Independent Sessions
Integration with ASP.NET Sessions in IIS-Hosted
compatibility mode
Transactions
Guaranteed atomic success or failure across services
Code vs. Config
Defining Endpoints
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0 ">
<system.serviceModel>
<services>
<service serviceType="CalculatorService">
<endpoint address="Calculator"
bindingSectionName="basicProfileBinding"
contractType="ICalculator" />
</service>
</services>
</system.serviceModel>
</configuration>
Configuring Bindings
<endpoint address="Calculator"
bindingSectionName="basicProfileBinding"
bindingConfiguration="Binding1"
contractType="ICalculator" />
<bindings>
<basicProfileBinding>
<binding configurationName="Binding1"
hostnameComparisonMode="StrongWildcard"
transferTimeout="00:10:00"
maxMessageSize="65536"
messageEncoding="Text"
textEncoding="utf-8"
</binding>
</basicProfileBinding>
</bindings>
Custom Bindings
<bindings>
<customBinding>
<binding configurationName="Binding1">
<reliableSession bufferedMessagesQuota="32"
inactivityTimeout="00:10:00"
maxRetryCount="8"
ordered="true" />
<httpsTransport manualAddressing="false"
maxMessageSize="65536"
hostnameComparisonMode="StrongWildcard"/>
<textMessageEncoding maxReadPoolSize="64"
maxWritePoolSize="16"
messageVersion="Default"
encoding="utf-8" />
</binding>
</customBinding>
</bindings>
WCF Summary
Application
… Error Metadata Instance
Service Model Behavior Behavior Behavior
Throttling Transaction Type Integ. Concurrency
Behavior Behavior Behavior Behavior
Messaging … Secure
Channel
Reliable
Channel
Text/XML
Encoder
… HTTP TCP Queue Binary
Channel Channel Channel Encoder
Hosting WAS ASP.NET WPF WinForm NT Service COM+
Environments
WCF Summary
WCF is the future of distributed computing
It combines the best of all existing Microsoft distributed
computing stacks
It uses WS-* standards for interoperability and .NET
value-add for performance and integration with existing
solutions
WCF is available for Windows Vista, Windows XP SP2,
Windows Server 2003, Windows Server 2008