You are on page 1of 23

Services

using WCF
Martin Kropp
University of Applied Sciences Northwestern Switzerland

Learning Targets
You
know the design goals of Windows Communication Foundation can explain the architecture of WCF can explain the ABC of Windows Communication Foundation can develop services using WCF can use services through different communication channels

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

Content

WCF Basics
Main Concepts Addresses Contracts Bindings Behaviour Practice

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

WCF Fundamentals
Windows Communication Foundations
Is a runtime for message based communication Development API
For creating systems that send messages between services and clients Unified API across different transport mechanisms

For applications that communicate with other applications


On the same computer system On external computer system In another company accessible over the internet

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

The Four Tenets of Service-Orientation


Boundaries are Explicit Developers opt-in to consuming, exposing, and defining public-facing service faade. Services and consumers are independently versioned, deployed, operated, and secured. Data never includes behavior; Objects with data and behavior are a local phenomenon. Capabilities and requirements represented by a unique public name; Used to establish service suitability.
Institute for Mobile and Distributed Systems, M. Kropp 5

Autonomous Evolution

Share schema & contract, not class

Compatibility based on policy


Windows Communication Foundation,v1.0

WCF Design Goals


A unified programming model for building service-oriented applications on the Windows platform
Unifies todays distributed technology stacks

Unification

Composable functionality Appropriate for use on-machine, in the intranet, and cross the Internet Service-oriented programming model Supports 4 tenets of service-orientation Improve developer productivity

Productive Service-Oriented Programming

Interoperability & Integration


Windows Communication Foundation,v1.0

WS-* interoperability with applications running on other platforms Interoperability with todays distributed stacks
Institute for Mobile and Distributed Systems, M. Kropp 6

Callers and Services

Caller

Service

Messages

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

Endpoints

Caller
Endpoint Endpoint Endpoint Endpoint

Service

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

The ABC of WCF

Caller
A C B A A A B B B C C C

Service

Address
Where?

Binding
How?

Contract
What?

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

WCF Architecture: Messaging Runtime

Service Client Dispatcher


Protocol(s) Protocol(s)

Contract and Behaviors

Encoder

Encoder

Binding

Transport

Transport

Address
Windows Communication Foundation,v1.0 Institute for Mobile and Distributed Systems, M. Kropp 10

Adresses
Defines where a service is located Specifies a URI where the service is located
Relative or absolute

Address consist of
Scheme
HTTP, TCP, Named pipes, MSMQ

Machine [Port] Path

Examples
http://www.mystore.com/StoreFront net.tcp://mycomputer:9000/StoreFront

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

11

Contracts
Defines what a service communicate
Service Contracts
Describe the operations a service can perform Map CLR types to WSDL

Data Contracts
Describes a data structure Maps CLR types to XSD

Message Contracts
Defines the (application specific) structure of the message on the wire Maps CLR types to SOAP messages
Windows Communication Foundation,v1.0 Institute for Mobile and Distributed Systems, M. Kropp 12

Ways to Talk
One Way

Client

Request-Reply Duplex (Dual)

Service

One Way:
Datagram-style delivery

Request-Reply
Immediate Reply on same logical thread

Duplex
Reply later and on backchannel (callback-style)
Windows Communication Foundation,v1.0 Institute for Mobile and Distributed Systems, M. Kropp 13

Service Contract
[ServiceContract] ServiceContract] public interface ICalculator { [OperationContract] OperationContract] int DoMath(int a, int b, string op); // Not exposed as part of the external contract :-) void MethodRequiredForImplementation(bool b); }

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

14

Data Contract
[DataContract] DataContract] public class ComplexNumber { DataMember] [DataMember] public double Real = 0.0D; [DataMember] DataMember] public double Imaginary = 0.0D; public ComplexNumber(double r, double i) { this.Real = r; this.Imaginary = i; } }
Windows Communication Foundation,v1.0 Institute for Mobile and Distributed Systems, M. Kropp 15

Message Contract
[MessageContract] MessageContract] public class ComplexProblem { MessageHeader(Name="Op", MustUnderstand=true)] [MessageHeader(Name="Op", MustUnderstand=true)] public string operation; [MessageBodyMember] MessageBodyMember] public ComplexNumber n1; MessageBodyMember] [MessageBodyMember] public ComplexNumber n2; MessageBodyMember] [MessageBodyMember] public ComplexNumber solution; // Constructors }

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

16

Binding
Describes how a service communicates Specifies set of binding elements
Transport; http, tcp, np, msmq Encoding format; text, binary, MTOM, ... Security requirements Reliable session requirements Transaction requirements

Set of predefined standard bindings


Can be customized

Custom binding

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

17

Behavior
Modifies or extends service or client runtime behavior Examples
Instancing; Singleton, PrivateSession, SharedSession, PerCall Concurrency; Multiple, Reentrant, Single Throttling; connections, threading Metadata customization Transactions; AutoEnlist, Isolation, AutoComplete

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

18

Bindings & Behaviors: Security

Client
A C Be B A A A B B B C C C Be

Service

Bindings Insert Claims in Messages Behaviors Implement Security Gates


Windows Communication Foundation,v1.0 Institute for Mobile and Distributed Systems, M. Kropp 19

Configuring Services: Defining Endpoints

<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service serviceType="CalculatorService"> <endpoint address="Calculator" bindingSectionName="basicProfileBinding" contractType="ICalculator" /> </service> </services> </system.serviceModel> </configuration>

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

20

Configuring WCF: Specify Bindings


<endpoint address="Calculator" bindingSectionName="basicHttpBinding" bindingConfiguration="UsernameBinding" contractType="ICalculator" />

<bindings> <basicHttpBinding> <binding name="UsernameBinding" messageEncoding="Mtom"> <security mode="Message"> <message clientCredentialType="UserName"/> </security> </binding> </basicHttpBinding> </bindings>

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

21

Features Summary
Address
http://... HTTP Transport

Binding
WS-Security Protocol

Contract
Request/ Response

Behavior
Instancing Behavior Concurrency Behavior

net.p2p://...

Peer Transport TCP Transport NamedPipe Transport WS-RM Protocol

Throttling Behavior

net.tcp://...

Metadata Behavior net.pipe://... WS-Coord Protocol One-Way Error Behavior net.msmq://... MSMQ Transport Duplex Channel Duplex xxx://... Custom Transport Custom Protocol Custom Behavior Security Behavior Transaction Behavior

Externally visible, per-endpoint


Windows Communication Foundation,v1.0 Institute for Mobile and Distributed Systems, M. Kropp

Opaque, per-service, endpoint, or operation

22

Building a WCF application


Application Design Service
1. Define Contracts 2. Implement Contracts 3. Define Endpoints 4. Host & Run Service

Client
1. Generate Proxy from Metadata 2. Implement & Run Client

Windows Communication Foundation,v1.0

Institute for Mobile and Distributed Systems, M. Kropp

23

You might also like