WCF PDF
WCF PDF
com
WCF
WINDOWS COMMUNICATION
FOUNDATION
OVERVIEW OF WCF, MICROSOFTS UNIFIED
COMMUNICATION FRAMEWORK FOR .NET APPLICATIONS
Peter R. Egli
© Peter R. Egli 2015
INDIGOO.COM 1/24
Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
Contents
1. What is WCF?
2. WCF‘s ABC
3. WCF model
4. The 4 layers of the WCF architecture
5. WCF programming model
6. WCF address (the 'A' in 'ABC')
7. WCF binding (the 'B' in 'ABC')
8. WCF contract (the 'C' in 'ABC')
9. WCF service hosting
10. Steps for creating and running a WCF service
11. WCF configuration versus programming
12. WCF and SOAP messages
2/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
1. What is WCF (1/3)?
WCF is a unified communication framework for distributed applications.
WCF defines a common programming model and unified API for clients and services to send
messages between each other.
WCF is the current and future standard for distributed .Net applications.
Extensibility:
WCF client / server can be configured to interoperate with REST, ATOM-feeds, plain XML
or JSON messages (extensions for interfacing to any message-based service).
3/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
1. What is WCF (2/3)?
It provides a unified API to use various types of communication from an application.
WCF sits on top of the .Net Framework.
.Net FW .Net FW
MSMQ DCOM WS MSMQ DCOM WS
TCP/IP HTTP TCP/IP HTTP
Client Server
Client and server communice asynchronously
WCF WCF over an MSMQ (message queueing) infrastructure.
WCF WCF Web service client and service use WCF for
communication. WCF provides interoperability with
HTTP HTTP non-WCF web service clients / services.
Pipe Pipe
5/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
2. WCF‘s ABC
The core concept of WCF is a service that is provided on an endpoint and accessible over the
network through a transport protocol.
Thus a WCF service is defined by ABC:
A = Address:
Where is the service available (the endpoint's URI in case of a web service).
B = Binding:
How can the service be accessed (what transport protocol is used).
C = Contract:
What does the service interface look like (operations, data-types).
6/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
3. WCF model (1/4)
WCF defines a (consistent) service model with entities and relationships.
hosted in • Self-hosted
Host • Service host
Application
has Instancing • Single
• Session
has has model
• Per call
Message
contract
implements
Service contains Operation
WCF client WCF service
contract contract
1 1 uses
has has Appl. corresponds to
* * Data
endpoint
uses contract
Endpoint Endpoint
1 1 Infrastructure Provides service
endpoint metadata
Message has has
has Address
as URI
* *
Communication stack
Security
Channel
protocol
Text/ ABC elements (blue)
XML
MTOM Binary WSDL are described by
has Message
Message exchange a WSDL file.
Channel
encoding pattern
Transport
Channel
protocol
HTTP TCP MSMQ Request One-
Binding elements Duplex
reply way
7/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
3. WCF model (2/4)
Application:
An application has a WCF client and / or WCF service.
WCF client:
Component with a WCF endpoint for communicating with a WCF service through messages.
WCF service:
Counterpart to WCF client. Runs in its own process (self-hosted) or in a specific service
hosting process (IIS, Windows Activation Service, Windows Service).
Endpoint:
WCF client and service use an endpoint to connect to each other.
Application endpoint: Endpoint on which an application service is exposed / offered.
Infrastructure endpoint: Part of WCF system to offer metadata of an application service.
Message:
Unit of data exchange between WCF client and service. WCF is strictly message-based.
Address:
Physical address comprising hostname, port number and service name (=URI). The application
service is accessible under such an address.
Example.: http://localhost:8000/HSZ-TWSMW/DateTimeService
8/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
3. WCF model (3/4)
Service contract:
Set of operations which define the application service. The service contract is implemented
by an interface in a .Net language (e.g. C# interface).
Operation contract:
Defines the signature (parameters and return type) of an individual operation of a service.
Instancing model:
Defines how the service is instantiated: Singleton, Session, Single-Call.
Binding (element):
Defines how an endpoint communicates with its peer endpoint. Binding elements define
individual aspects of the communication, basically the transport protocol (raw TCP, HTTP),
message encoding (text, binary) and the security.
Channel:
Concrete implementation of a binding element (binding element = configuration, channel =
implementation).
9/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
3. WCF model (4/4)
Transport protocol (transport binding):
Defines the protocol used to transfer messages “over the wire” (wire protocol). A common
transport protocol is HTTP (HTTP over TCP).
Communication stack:
Comprises different binding elements, at a minimum a transport binding element and message
encoding binding element.
Host:
Runtime environment for a service. Either self-hosted (specific process for service) or a general
hosting process, e.g. IIS, WAS or Windows service.
10/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
4. The 4 layers of the WCF architecture
Application
Contracts define the service (as XSD):
Contracts Data contract Definition of complex types (structures, classes, generics).
Policy Service contract (C) Definition of operation signatures.
Data Message Service
Contract Contract Contract
and Message contract (C) Layout of (SOAP) messages.
Binding Policies and binding (B) Definition of transport protocol (TCP, HTTP) and
message encoding (text, binary); security functions used (if any).
Service Runtime
Throttling Error Metadata Instance Message
Behavior Behavior Behavior Behavior Inspection
Service runtime contains runtime behavior (functions of service host):
Examples: Error behavior, throttling message reception, delivery of service
Transaction Dispatch Concurrency Parameter
Behavior Behavior Behavior Filtering
metadata to the outside world.
Messaging
WS Reliable Encoders: Messaging layer with channels (channel = implementation of binding element):
WS Security
Channel
Messaging Binary/MTOM - Security (authentication & encryption) & reliability (reliable messaging) channels.
Channel /Text/XML
- Message encoding channels (text, binary, MTOT, XML).
Transaction
- Transport channels (TCP, HTTP).
HTTP TCP NamedPipe MSMQ
Channel Channel
Flow
Channel Channel
All used channels comprise the channel stack.
Channel
Activation and Hosting Channels, service runtime behaviors and service itself (modules implementing
Windows .exe
Windows
the contracts) are run in a host process. Services can be:
Activation (self- COM+ a. Self-hosted (run in their own process).
Service Services
hosting)
b. Hosted by an external agent (WAS, IIS, Windows services).
11/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
5. WCF programming model (classes and interfaces)
The ABC elements of WCF are offered as specific class libraries.
12/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
6. WCF address (the 'A' in 'ABC')
The WCF address defines where a web service (endpoint) is accessible.
WCF models an address as an endpoint reference (EPR) as per the WS-Addressing standard.
WS-Addressing EPR schema:
<wsa:EndpointReference>
<wsa:Address>xs:anyURI</wsa:Address> URI
<wsa:ReferenceProperties>... </wsa:ReferenceProperties> ? Properties to identify the endpoint
<wsa:ReferenceParameters>... </wsa:ReferenceParameters> ? Parameters associated with an endpoint
<wsa:PortType>xs:QName</wsa:PortType> ? Endpoint type (WSDL 1.0 portType, WSDL 2.0 interface)
<wsa:ServiceName PortName="xs:NCName"?>xs:QName</wsa:ServiceName> ? Link to WSDL service element containing the endpoint descr.
<wsp:Policy> ... </wsp:Policy>* Security settings of endpoint
</wsa:EndpointReference>
WS-addressing EPR see http://www.w3.org/Submission/ws-addressing/#_Toc77464317
http://localhost:8000/HSZ-TWSMW/DateTimeService
13/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
7. WCF binding (the 'B' in 'ABC') (1/2)
The binding defines how a web service endpoint is accessed.
A binding contains the following elements:
1. Transport protocol:
Underlying transport protocol to use when interacting with the web service.
Examples: TCP, HTTP, MSMQ.
2. Message encoding:
Definition of the message encoding.
Examples: Text/XML (SOAP), binary, MTOM (Message Transfer Optimized Mechanism).
Encoder + Encoder +
Security Security
Encoding and securing of Transport connection and protocol Encoding and securing of
application messages. application messages.
14/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
7. WCF binding (the 'B' in 'ABC') (2/2)
WCF provides the following bindings:
WSDL / .Net C#
XSD SvcUtil.exe code
16/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
9. WCF service hosting
A service host is the process that runs (executes) the service(s).
WCF provides different possibilities to host a service.
A. Self-hosted service:
The service runs in an application process that the developer created.
The lifecycle (when is service opened and closed) is controlled by the service itself.
c. Windows services:
Standard Windows execution with security etc.
Lifecycle of service controlled by service startup configuration.
18/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
10. Steps for creating and running a WCF service (2/4)
The following steps outline how to create a web service in Visual Studio 2010.
19/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
10. Steps for creating and running a WCF service (3/4)
2. Interface definition (service contract as C# interface or C# class):
Define the interface of the web service with a .Net interface.
Example interface contract definition in C# (containing operation contracts):
[ServiceContract(Namespace = "http://indigoo.WSMW")]
public interface IMyInterface
{
[OperationContract]
string MyFunction();
[OperationContract]
int MyOtherFunction();
}
20/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
10. Steps for creating and running a WCF service (4/4)
4. Deploy and run the service:
The final step is to deploy the service assembly (DLL) into the hosting environment or to start
it in case of self-hosting.
For self-hosting services, the following code needs to be added to the service (definition of
endpoint with address and binding):
selfHost.AddServiceEndpoint(typeof(MyService), new BasicHttpBinding(), "MyService");
Finally the service can be started in the WCF self-hosting environment as follows:
selfHost.Open();
21/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
11. WCF configuration versus programming (1/2)
WCF client and service can be defined by configuration (XML file, declarative) or
programmatically (.Net classes).
Good practice:
For improved reusability of web services in different environments, contract ('C') and address
('A') as well as transport binding ('B') should be separated.
Define interface and implementation programmatically (C#) without transport and message
encodings.
Definition of transport binding, address and message encoding in configuration file.
SvcConfigEditor.exe can also be launched through the App.config / Web.config files' context
menu:
23/24
© Peter R. Egli 2015 Rev. 2.20
WCF – Windows Communication Foundation indigoo.com
12. WCF and SOAP messages
Usually WCF hides the mapping of operations (operation contracts, data contracts) to SOAP
messages (WCF bindings that use SOAP messages automatically map operations into SOAP
messages).
If necessary, WCF MessageContracts provide complete control over the structure of the
exchanged SOAP messages (similar concept to JAX-WS @WebServiceProvider annotations).
24/24
© Peter R. Egli 2015 Rev. 2.20