Professional Documents
Culture Documents
5 WCF
Programming
Introduction
Presented by Cuong@microsoft.com
Agenda
Basic Demo
Some tools for WCF programming
Self hosting and IIS hosting
Behaviors
Concurrency and Instancing Mode
Security
Unification
Productive
Service-Oriented
Programming
Interoperability &
Integration
technology stacks
Composable functionality
Appropriate for use on-machine, in
the intranet, and cross the Internet
Service-oriented programming
model
Supports 4 tenets of serviceorientation
Maximized developer productivity
WS-* interoperability with applications
running
on other platforms
Interoperability with todays
distributed stacks
Unified Model
ASMX
Enterprise
Services
Basic
Open
Interop
Fast
Secure
Binary
Txns
WSE
Remotin
g
Secure
Open
Interop
Fast
Secure
Binary
MSMQ
Queued
Txns
Before we have
Remote Procedure Call
COM+
.NET Remoting
ASP.net web service
Hard code TCP Socket
MSMQ
Flexibility
Environments
Productivity in development environment
Simplify Automated Integration Testing
Deployment options in production
Design for distribution, run local
Elements in WCF
Before we go further
WCF Service can be configured in code
WCF Service can be configured in XML or
mix.
03 different ways to host WCF
Self Hosting
IIS Hosting
Windows Service
Demo
Endpoints
Client
Service
Endpoint
Endpoint
Message
Endpoint
Client
Service
Message
(How)
(What)
<system.serviceModel>
<services>
<service name="WCFServiceApplication.Service1"
behaviorConfiguration="WCFServiceApplication.Service1Behavior">
<endpoint address="" binding="wsHttpBinding"
contract="WCFServiceApplication.IService1">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WCFServiceApplication.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false
and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
WCF Service
Configuration Editor
Client
Protocol(s
)
Service
Dispatcher
Contract
and
Behaviors
Protocol(s
)
Encoder
Encoder
Transport
Transport
Binding
Address
Formatter
Behavior
Transactio
n Behavior
Formatter
Behavior
Message
Inspector
Service
Code
Instancing
Behavior
Securit
y
Channe
l
TCP
Transpor
t
Securit
y
Channe
l
TCP
Transpor
t
Messaging Layer
Contracts
Data Contract
Describe data structures that are used by the service to
communicate with clients. A data contract maps CLR types to XML
Schema Definitions (XSD) and defines how they are serialized and
deserialized. Data contracts describe all the data that is sent to or
from service operations.
Message Contract
Map CLR types to SOAP messages and describe the format of the
SOAP messages and affect the WSDL and XSD definitions of those
messages. Message contracts provide precise control over the
SOAP headers and bodies.
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;
}
}
Demo
Basic data type data contract
Class data type data contract
Hierarchy data type data contract
Collection data contract
C:\WCF\Contracts\HRServi
ce
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 (callbackstyle)
Service Contract
using System.ServiceModel;
[ServiceContract]
public interface ICalculate
{
[OperationContract]
double Add( double a, double b);
[OperationContract]
double Subtract( double a, double b);
}
BeginOperation
New Thread
EndOperation
Demo C:\WCF\03_OneWay
Duplex Communication
Use
wsDualHttpBinding
Since two endpoints are
used, we can define two
different bindings,
protocol for each
channels
[ServiceContract(Session=true,
CallbackContract=typeof(IChat)]
public interface IChat
{
[OperationContract(IsOneWay=true)]
void Talk(string text);
}
Demo
Request Reply
One Way
One Service many EndPoints
Message Contract
[MessageContract]
public class ComplexProblem
{
[MessageHeader]
public string operation;
[MessageBody]
public ComplexNumber n1;
[MessageBody]
public ComplexNumber n2;
[MessageBody]
public ComplexNumber solution;
// Constructors
}
Bindings
Binding
HTTP
Transport
Encoders
TX
Protocol
TCP
HTTP
Text
Security
Reliability
MSMQ
IPC
Binary
TX
.NET
Custom
Custom
Custom
Interop
Security
Session
TX
Duplex
BasicHttpBinding
BP 1.1
N, T
n/a
WSHttpBinding
WS
M, T, X
N, T, RS N, Yes n/a
WSDualHttpBinding
WS
RS
N, Yes Yes
WSFederationBinding
Federation M
N, RS
N, Yes No
NetTcpBinding
.NET
T, M
T ,RS
N, Yes Yes
NetNamedPipeBinding .NET
T, N
N, Yes Yes
NetPeerTcpBinding
Peer
NetMsmqBinding
.NET
T, M, X
N, Yes No
N, Yes n/a
MsmqIntegrationBindi
MSMQ
ng
Yes
Transport-Independent Sessions
Integration with ASP.NET Sessions in IISHosted compatibility mode
Transactions
Guaranteed atomic success or failure
across services
Service
Bindings
provide
Session and
Guarantees
Be
Service
Be
Bindings Flow
Transactions
Behaviors
AutoEnlist and
AutoComplete
1- Multistep business process : Long running transaction WF
integrates with WCF
2- Short running transaction
In next training, I will talk more detail with demo for transactions
Defining Endpoints
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>
C:\WCF\GadgetDemo_WCFReturnJSON
Service Behaviors:
Instance and Concurrency
Management
03 instance modes:
Per-Session instance mode
Per-Call instance mode
Singleton Instance Mode
03 concurrent modes:
Single - default setting. Instructs the runtime to allow access
on one thread per instance of the service class. This setting
is the safest one because service operations do not need to
worry about thread safety.
Reentrant - Only one thread at a time can access the service
class, but the thread can leave the class and come back later
to continue.
Multiple - Multiple threads may access the service class
simultaneously. This setting requires the class to be built in a
thread-safe manner.
Per Session
Per Call
Singleton Instance
Service Throttling
maxConcurentInstances: controlling how many
services can be created by service. With this
setting we are defining the upper boundary of the
number of instances resides in a memory.
maxConcurentCalls: - controlling how many
concurrent calls can be active. What we can
manage here is actually the number of threads
used in our service processing.
maxConcurrentSessions: - used to control how
many active sessions we have especially when
using PerSession instance mode.
WCF Security
Authentication
Authorization
Confidentiality
Integrity
Transport and Message Security
Please look at
http://wcfsecurityguide.codeplex.com/
Workflow
ServiceHost
ServiceHost
Service
Runtime
WorkflowOperationInvok
OperationInvoker
er
OperationSelector
InstanceProvider
DurableInstanceProvider
MessageContextInspect
MessageInspector
or
ServiceDescripti
on
ServiceBehavior
WorkflowServiceBehavior
OperationBehavio
WorkflowOperationBehav
r
ior
ContextChannel
ListenerChannel
Service
Instance
Workflow
Instance
ReceiveActivity
Operation 1 1
Operation 2 2
ReceiveActivity
Workflow Runtime
WF Persistence DB
WCF Summary
Application
Error
Behavior
Service Model
Secure
Channel
Hosting
WAS
Environments
HTTP
Channel
ASP.NET
Instance
Behavior
Throttling
Behavior
Messaging
Metadata
Behavior
WPF
Text/XML
Encoder
Reliable
Channel
TCP
Channel
WinForm
Queue
Channel
Binary
Encoder
NT Service
COM+
Good References
Book Essential Windows Communication
Foundation for .NET 3.5
PluralSight video training
http://
www.pluralsight.com/main/screencasts/defaul
t.aspx
Channel 9
http://channel9.msdn.com/shows/Endpoint/
WCF Summary
WCF is good for SOA, 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