You are on page 1of 38

Windows Communication Foundation

Basic Concept

Copyright 2009 Trend Micro Inc.

Challenges in Building Business Applications


We are living in a distributed world Business is agile Costs must be reduced Systems have to be scalable, secure, and reliable
Python .NET JAVA

Efficient integration and interoperability are essential

Copyright 2009 Trend Micro Inc.

Service-Oriented Architecture (SOA)


SOA is based on service A service exposes functionality by accepting data (requests) and sometimes returning data (response). Services deal with data not objects.
Data in
Business Service Component

Customer Order Business Partner Internal ERP System

Data out

Copyright 2009 Trend Micro Inc.

Service-Oriented Architecture (SOA)

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 4

WCF Window Communication Foundation


A unified .NET Framework for building service-oriented applications. Introduced in .NET Framework 3.0 and extended in .NET3.5 and .NET4.0 WCF is the foundation for other Microsoft-distributed technologies
.NET Enterprise

WSE

.NET Remoting

ASMX

WCF

MSMQ

Copyright 2009 Trend Micro Inc.

WCF Objective for Service Implementation


Service development should be simple but still extensible to meet a variety of needs One API should be used for all communication protocals Service should be interoperable Service should use WS-* standards if applicable Service can support SOAP, REST, and other communication architectures

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 6

The WCF Communication Model

Message

HTTP

Endpoint Endpoint

Client

Endpoint

Service

TCP/IP Endpoint

Service communicate using messages that are exchanged between endpoints.

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 7

The ABCs of WCF


Endpoint: Address, Binding, Contract combinations

Message

HTTP

Client

TCP/IP

Service

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 8

Endpoints: Addresses, Bindings, and Contracts


An address that indicates where the endpoint can be found. A binding that specifies how a client can communicate with the endpoint. A contract that identifies the operations available. A set of behaviors that specify local implementation details of the endpoint.

Copyright 2009 Trend Micro Inc.

Address
Where s my service? The location of the service on the network where it can be reached. http://localhost/service.svc net.msmq://localhost/private/service net.tcp://localhost:6000/service Set via configuration or through code

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 10

Contract
What s the functionality? Define what data and operation will be exposed Define Contract is the first step of WCF service life cycle Information conveyed in a Contract
Operations exposed by the service In/Out Data representation Types of errors (faults) Messaging patterns

Copyright 2009 Trend Micro Inc.

Service Contract
Describe the operations exposed by the service Defined as a C# interface decorated by the ServiceContract and OperationContract attributes

[ServiceContract] public interface ICalculatoor { [OperationContract] int Add(int a, int b); Functions provided by the service [OperationContract] int Sub(int a, int b); }

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 12

ata Contract
Describes the shape of the data in the request and response messages Defined as a class decorated with the ataContract and DataMember attributes
[DataContract] public class Employee { [DataMember] public string Name { get; set; } [DataMember] public int Age { get; set; } public int Salary { get; set; } }
Classification 2/9/2012 Copyright 2009 Trend Micro Inc. 13

Serialized

Not Serailized

Message Contracts
Describes the data as a SOAP message Defined as a class decorated with the MessageContract, MessageHeader, and MessageBodyMember attributes

[MessageContract] public class Employee { Leave something unencrypted [MessageHeader] public string Name { get; set; } [MessageBodyMember] Put something sensitive public int Age { get; set; } need to be encrypted }
Classification 2/9/2012 Copyright 2009 Trend Micro Inc. 14

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/GetEmployee</Ac tion> </s:Header> <s:Body> <GetEmployee xmlns="http://tempuri.org/"> <emp xmlns:d4p1="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/XMLSchemainstance"> <Age>12</:Age> <Name>Lizzie</Name> </emp> </GetEmployee> </s:Body> </s:Envelope> <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService/GetEmployee</Ac tion> <h:Name xmlns:h="http://tempuri.org/">Lizzie</h:Name> </s:Header> <s:Body> <Employee xmlns="http://tempuri.org/"> <Age>10</Age> </Employee> </s:Body> </s:Envelope>
Classification 2/9/2012 Copyright 2009 Trend Micro Inc. 15

Fault Contract
Describes error information for the fault section of the SOAP message Defined using a standard data contract and the FaultContract attribute decorating the operation
[OperationContract] [FaultContract(typeof(ErrorInfo))] int Add(int a, int b); *Turn on includeExceptionDetailsInFaults in service behavior

[DataContract] public class ErrorInfo { [DataMember] public string ErrorMsg{ get; set; } }

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 16

Binding
How do I connect to the service? Indicates the connectivity requirements like protocol and policies used to connect to the service at it s address.
Message should be sent in binary representation or in textual representation? Should the message be encrypted or not

WCF Provided Bindings


Http(BasicHttp, WsHttp, WsDualHttp, WsFederation) NetMsmq MsmqIntegration NetNamedPipe NetTcp NetPeerTcp ..

Set via configuration or through code


Classification 2/9/2012 Copyright 2009 Trend Micro Inc. 17

Responsibilities of a Binding
Defines the transport technology (ex: HTTP, TCP) Defines the message encoding Defines protocol and standards (ex: security, session) Define the communication channel and messaging properties (ex: timeout, maximum message size)

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 18

Message Pipeline
Reliable Messaging Security Encoding Transport Reliable Messaging Security Encoding Transport

Message move through message pipeline A binding defines which elements will be in the pipeline

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 19

Binding Element

Each binding in WCF is a combination of a transport element, an encoder element, and zero or more protocol elements.

Binding
HTTP Text Security Reliability Transaction

Transport
TCP HTTP

Encoders
Text Binary

Protocol
Security Reliability

MSMQ Custom

IPC

Transaction Custom

.NET

Custom

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 20

Channel Stack
Channel is a concrete implementation of a binding element. Channel Stack: Channels stack on top of each other to create the concrete implementation of the binding.
Reliable Messaging Security Encoding Transport Protocol Protocol Encoder Transport Channel Channel Channel Channel

Binding elements
Classification 2/9/2012

Channel stack

Copyright 2009 Trend Micro Inc. 21

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 22

Dispatchers and the Channel Stack


The channel stack needs to be extended to allow configuration of additional aspects After the channel pipeline there are several dispatchers that process the messages These dispatchers can be used to adjust the service behavior in aspects like:
Instancing Concurrency Throttling Security Serialization

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 23

Using Behavior to Configure WCF Dispatchers


The mechanism to configure the dispatchers is called behaviors

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 24

WCF Architecture
Contracts Service Runtime Messaging Hosting

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 25

Hosting WCF Service


In Process Windows Service Web Service (IIS) Windows Activation Services WAS allows you to host a service on any binding

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 26

Security
Encrypt message User authentication

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 27

Consuming WCF Service


The proxy pattern

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 28

Demo
Build a WCF service using the proxy pattern Consume the WCF service Build an endpoint for RESTFul service Invoke service with HTTP GET

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 29

And
Message Pattern Security Error handling Hosting Distributed Transaction Instancing and Concurrency Reliability Logging/Tracing/Monitoring

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 30

DISCUSSION

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 31

Services Use SOAP


SOAP is an XML-based standard SOAP carries messages for Web services messages are created according to the service contract
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:key="http://pr.trendmicro.com/keypool/"> <soapenv:Header/> <soapenv:Body> <key:QueryAC> <key:orderID>Lizzie</key:orderID> <key:buCode>us</key:buCode> <key:actCode>BU-7B4H-WWZ</key:actCode> </key:QueryAC> </soapenv:Body> </soapenv:Envelope>

Header

Body

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 32

RESTful services
Representational State Transfer (REST) is an architectural style for service REST utilizes the HTTP protocol Contract is based on HTTP verbs, input data is written in URIs
GET, POST, PUT, DELETE

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 33

WS-* Standards Integrated Web Services


WS-* is a group of XML-based standards relevant for Web services WS-* defines how security, transaction, addressing, and reliability information is transmitted WS-* forms the basis for Web service interoperability

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 34

Binding
The Address of the endpoint is affected by the endpoints binding
HTTP: http://xxx TCP: net:tcp://xxx

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 35

About the ABC s


A service can have many endpoints
Two endpoints for the same contract, each with a different binding and address Three endpoints for a different contract and with a different address, but use the same binding

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 36

Contract Design Best Practice

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 37

KnownType Attrbute
P4-53

Classification 2/9/2012

Copyright 2009 Trend Micro Inc. 38

You might also like