Windows Communication Foundation Part 1 You can reuse these slides but please mention the original source if you want to do so. Speaker: Nguyễn Bá Quang & Trịnh Minh Cường
Agenda WCF Unified Model Simple demo ClientsHosting WCF Architecture Detail Endpoints ABCs and Behaviors
Overview and Architecture
Unified Model Basic Open Interop Basic Open Interop ASMX Secure Open Interop Secure Open Interop WSE Fast Secure Binary Fast Secure Binary Remoting Queued Txns Queued Txns MSMQ Fast Secure Binary Txns Fast Secure Binary Txns Enterprise Services Image from WCF presentation by Mike Taulty - Vista Developer Launch
Unified Model Benefits Programming model Learning curve Consistency Write less code when using multiple technologies FlexibilityEnvironments Productivity in development environment Simplify Automated Integration Testing Deployment options in production Design for distribution, run local
WCF Architecture ClientService Message ABCABC AddressBindingContract (Where)(How)(What) Bv Bv Bv Bv ABC Endpoint
WCF Architecture: Messaging Runtime TransportEncoderProtocol(s)TransportEncoderProtocol(s) Client Dispatcher Service Contract and Behaviors Binding Address
Service Model Layer Influences system operation based on incoming and outgoing messages. Effects of behaviors are local. Messaging Layer Moves messages back and forth and adds transfer semantics. Channels are symmetric. ServiceCode Message Inspector Formatter Behavior Instancing Behavior Security Channel TCP Transport Security Channel TCP Transport Transaction Behavior Formatter Behavior WCF Architecture: Composition & Behaviors
3 Ways to Talk One Way: Datagram-style delivery Request-Reply Immediate Reply on same logical thread Duplex Reply “later” and on backchannel (callback-style) ClientService One Way Request-Reply Duplex (Dual)
Demo #1: Simple console WCF app 01_WCFServiceLibrary Create WCF service How to test service through web browser How to consumes it 10
Client side: Add Service
Service Clients Through code or configuration Service knowledge Extract metadata SvcUtil.exe Custom tool Generates code and config Share Types Service, Data, Faults Reusing existing types on the client Set Name and Namespace GSC SSC RSC
Service Metadata OperationsTypesAddressesBindingsPolicies Described using WSDLWS-Policy Image from WCF presentation by Mike Taulty - Vista Developer Launch
Demo mex: 02_ConsoleWCFWithConfig 14
Service Hosting
Where Are We? ServiceHostDescription Behaviour EndpointAddressBindingContract Behaviour Operation Behaviour RuntimeListenersSitesExtensions
Demo: different type of hostings Quang Self Host IIS Host Windows Service Host 17
Where Are We? ServiceHostDescription Behaviour EndpointAddressBindingContract Behaviour Operation Behaviour RuntimeListenersSitesExtensions
Address
Where Are We? ServiceHostDescription Behaviour EndpointAddressBindingContract Behaviour Operation Behaviour RuntimeListenersSitesExtensions
Endpoint Address UriIdentityHeaders
Address URI SchemeHostPortPath Port Sharing via HTTP.SYS Port Sharing via HTTP.SYS Port Sharing via TCP Port Sharing Service Port Sharing via TCP Port Sharing Service
Binding
Where Are We? ServiceHostDescription Behaviour EndpointAddressBindingContract Behaviour Operation Behaviour RuntimeListenersSitesExtensions
Bindings How to talk to the service endpoint Binding information categories ProtocolsSecurity Reliable messaging TransactionsEncodingTransport
Binding Layers and Elements Bindings are constructed from an ordered set of binding elements Binding elements add channels to the channel stack in the channel factories/listeners
Binding Features BindingInteroperabilitySecurity (Default)Session (Default)TransactionsDuplex BasicHttpBindingBasic Profile 1.1None, Transport, Message,None n/a WSHttpBindingWSTransport, Message, MixedNone, Transport, Reliable SessionNone, Yesn/a WSDualHttpBindingWSMessageReliable SessionNone, YesYes WSFederationHttpBindingWS-FederationMessageNone, Reliable SessionNone, YesNo NetTcpBinding.NET 3.0Transport, MessageReliable Session, TransportNone, YesYes NetNamedPipeBinding.NET 3.0TransportNone, TransportNone, YesYes NetMsmqBinding.NET 3.0Message, Transport, BothNoneNone, YesNo NetPeerTcpBindingPeerTransportNone Yes MsmqIntegrationBindingMSMQTransportNoneNone, Yesn/a
Configure binding 28 Entirely by code Using code and configuration file Demo how to use WCF Service Configuration Editor.
Demo #2 02_ConsoleWCF02_ConsoleWCFWithConfig 29
WCF Service Configuration Editor 30
Create Your Own Binding Custom Use CustomBinding Fill with binding elements Through code or configuration Inherit Derive from system-provided bindings User-defined Derive from Binding Add your own or system-provided elements
Demo 3 Ways to Talk One Way: Datagram-style delivery. Demo 03_OneWay Request-Reply Immediate Reply on same logical thread. 04_Asynchronous Duplex Reply “later” and on backchannel (callback-style) ClientService One Way Request-Reply Duplex (Dual)
Contract
Where Are We? ServiceHostDescription Behaviour EndpointAddressBindingContract Behaviour Operation Behaviour RuntimeListenersSitesExtensions
Contract Types ServiceContractOperationContractDataContractDataMemberFaultContractMessageContractMessageHeaderMessageBodyMember
[ServiceContract] Defines the service to be published Can be applied to interfaces and classes PropertiesNameNamespaceProtectionLevelCallbackContractSessionMode
[OperationContract] Defines operations provided by the service PropertiesIsOneWay No service exceptions when set to true IsInitiatingIsTerminatingProtectionLevelAction Use “*” to catch all messages OES OEC
Data Contracts [ DataContract ] PropertiesName Namespace (Default: [ DataMember ] PropertiesNameIsRequiredOrder Can be applied to any instance property, field or event Same namespace as the DataContract PDM
Collections Collections are interchangeable All list collections of the same type are considered to have the same data contract Same applies to dictionaries of the same type Concrete classes and interfaces [CollectionDataContract] More control over collection serialisation
Sharing DataContract ApproachesSvcutil.exe Share the type Reuse an existing type (struct/class/interface) Make sure you set the Name and Namespace SDC RDC
FaultContract Exceptions should be explicitly published Security Exception to fault mapping How to raise the fault? Decorate the fault with [DataContract] Decorate the operation contract with [FaultContract] Throw FaultException Throw FaultException How to handle the fault? Catch FaultException Catch FaultException Detail property of the exception is of type TDetail FDC FOC TFE CFE
Behaviors
Where Are We? ServiceHostDescription Behaviour EndpointAddressBindingContract Behaviour Operation Behaviour RuntimeListenersSitesExtensions
Behaviours SBV
Demo (Quang) PerCallPerSession 45
Demo: SilverLight consume basic web service ADO.net data service 46
Questions?