You are on page 1of 20

Connecting Applications with Windows Communication Foundation

Brian Noyes www.idesign.net

2008 Brian Noyes, IDesign Inc. All rights reserved

About Brian
n n n

n n n

Chief Architect, IDesign Inc. (www.idesign.net) Microsoft Regional Director / MVP Publishing l Developing Applications with Windows Workflow Foundation, LiveLessons training DVD, June 2007. l Smart Client Deployment with ClickOnce, Addison Wesley, January 2007 l Data Binding in Windows Forms 2.0, Addison Wesley, January 2006 l MSDN Magazine, MSDN Online, CoDe Magazine, The Server Side .NET, asp.netPRO, Visual Studio Magazine Speaking l Microsoft TechEd US, Europe, Malaysia, Visual Studio Connections, DevTeach, INETA Speakers Bureau, MSDN Webcasts Participates in Microsoft Design Reviews E-mail: brian.noyes@idesign.net Blog: http://briannoyes.net

2007 Brian Noyes, IDesign Inc All Rights Reserved

Agenda
n n n n n n n

Connected Systems Concerns WCF Overview WCF ABCs and Es Defining Services Connecting Clients Passing Data Advanced Capabilities

Connected Systems in a SO World


n n n n

Need to connect clients to back-end services Need to connect services to other services Need to be decoupled from the specific service technology / calling patterns Need support for distributed system capabilities: l Service hosting l Instance management l Reliability l Async communications l Transactions l Security

2007 Brian Noyes, IDesign Inc All Rights Reserved

SOA Tenets
n n n n

Service boundaries are explicit Services are described by policy Services expose contract and schema, not types Services are autonomous

What if Im not doing SOA?


n

If your application is composed of more than one process, you still need a remoting technology WCF is the best available remoting technology even if you are just doing client/server, point-to-point communication

2007 Brian Noyes, IDesign Inc All Rights Reserved

Agenda
n n n n n n n

Connected Systems Concerns WCF Overview WCF ABCs and Es Defining Services Connecting Clients Passing Data Advanced Capabilities

WCF Overview
n n n n n

Framework for connecting distributed applications Part of .NET 3.0 and later Enables building SOA applications Implements WS-* standards Single API that supports the capabilities of: l .NET Remoting l ASP.NET Web Services l Enterprise Services (COM+) l MSMQ

2007 Brian Noyes, IDesign Inc All Rights Reserved

WCF Overview
n

Messaging subsystem at its core l SOAP messages


Does

not necessarily mean XML/Text!

Interoperability with: l Enterprise Services l ASMX / Web Services 1.0 services l Any WS-* compliant web service l MSMQ applications

WCF Value Proposition


n

Simplicity l On par with other remoting technologies if you just need simple remote calls l Incrementally more complex as you start to take advantage of more complex features

Features which the other technologies may not have

Flexibility l You can change your mind about wire level communication protocols, security model, and many other facets without changing your source code Maintainability l One API for all forms of remote communication, dont need to learn multiple technology stacks Power l All the capabilities of other remoting APIs rolled into one l Interoperability and SOA enabled in ways the other technologies never considered

2007 Brian Noyes, IDesign Inc All Rights Reserved

Agenda
n n n n n n n

Connected Systems Concerns WCF Overview WCF ABCs and Es Defining Services Connecting Clients Passing Data Advanced Capabilities

WCF ABCs and Es


n

Clients and services communicate through Endpoints

Client

Service
Endpoint

Endpoint

Message

Endpoint

2007 Brian Noyes, IDesign Inc All Rights Reserved

WCF ABCs and Es


n

Enpoints are composed of Address, Binding, Contract

Client
A C B A B B

Service
C C

Message

Address
(Where)

Binding
(How)

Contract
(What)

WCF Addresses
n n

Location of the service URL l Protocol l Server/domain l Port l Service URI (optional) Examples: l http://myappserver:2112/ l http://my.service.domain.com/SomeServiceURI l net.tcp://myappserver:2112/SomeServiceURI

2007 Brian Noyes, IDesign Inc All Rights Reserved

WCF Bindings
n n

The brains of the communications Encapsulates what happens on the wire l Wire level protocol l Communication patterns l Message format l Security l Transactions l Reliability Can all be extracted out of code and put in config

WCF Bindings
n

n n

WCF defines several standard bindings l Basic l TCP l Peer TCP l Named Pipe l WS Many variation bindings l Context, Dual, Federation, etc Can customize standard bindings Can define custom bindings

2007 Brian Noyes, IDesign Inc All Rights Reserved

WCF Contracts
n

Service Contracts l Operations exposed by the service Data Contracts l What data is passed through the service operations Message Contracts l What the message looks like when serialized Fault Contracts l What error messages look like

Agenda
n n n n n n n

Connected Systems Concerns WCF Overview WCF ABCs and Es Defining Services Connecting Clients Passing Data Advanced Capabilities

2007 Brian Noyes, IDesign Inc All Rights Reserved

Defining Services
n

Define a Service Contract l .NET interface l [ServiceContract] attribute on the type l [OperationContract] attribute on the operations (methods)

Defining Services
n

Implement a service types l Implements service contract interface l Does not require any WCF specifics for basic communication l Add [ServiceBehavior] attribute with properties for special communication patterns
Timeouts, instancing, concurrency, transactions

2007 Brian Noyes, IDesign Inc All Rights Reserved

10

Agenda
n n n n n n n

Connected Systems Concerns WCF Overview WCF ABCs and Es Defining Services Connecting Clients Passing Data Advanced Capabilities

Connecting Clients
n

Need a WCF proxy on the client l Generate


svcutil.exe Add Service

Reference in VS Code gen (snippets, CodeRush, CodeSmith)


l

Dynamically create
ChannelFactory<T> class

n n

Proxy exposes service contract methods Create instance of proxy, call methods

2007 Brian Noyes, IDesign Inc All Rights Reserved

11

Agenda
n n n n n n n

Connected Systems Concerns WCF Overview WCF ABCs and Es Defining Services Connecting Clients Passing Data Advanced Capabilities

Passing Data
n n

n n n

Based on Data Contracts .NET primitives work out-of-box l Serializable Any serializable type by default XmlSerializable types if you change the formatter Best practice: l Define data contracts for custom types used in the service contract
[DataContract] attribute on [DataMember] attribute on

the entity type the properties you want to be contained in

the data transfered

2007 Brian Noyes, IDesign Inc All Rights Reserved

12

Agenda
n n n n n n n

Connected Systems Concerns WCF Overview WCF ABCs and Es Defining Services Connecting Clients Passing Data Advanced Capabilities

Advanced Capabilities
n n n n n n n n

Asynchronous calls Callbacks Sessions Security Reliability Queuing Transactions Peer to Peer

2007 Brian Noyes, IDesign Inc All Rights Reserved

13

Asynchronous calls
n

n n n

Service calls can: l Take a long time l Be one-way l Call back into client l Be re-entrant Need to make calls asynchronously Handle completion / error handling Display results in UI l Completion events/callbacks on background thread

Asynchronous Calls
n

Manual Async Calls l Delegate based l BeginXXX/EndXXX method variants


Svcutil /a switch OperationContract.AsyncPattern =
l l

true

Register callback method Gets called on background thread, must marshal results yourself to UI thread (ISynchronizeInvoke)

2007 Brian Noyes, IDesign Inc All Rights Reserved

14

Callbacks
n

Service can call client l Events Need to register client callback contract on service l ServiceContract.CallbackContract Need to obtain callback channel instance in service l OperationContext.GetCallbackChannel<T> Callbacks happen asynchronously from client perspective

Sessions
n

n n n n n

Often service calls are related to one another l Start Order l Add Order Items l Submit Order Individual service calls are just messages l Underlying protocol does not support sessions Need to relate multiple messages together Sessions are maintained through shared service instances on service side NOTE: Session is the default instancing mode NOTE: Sessions are BAD for scalability NOTE: Sessions get messy with transactions / web farms / etc.

2007 Brian Noyes, IDesign Inc All Rights Reserved

15

Security
n n n

Three kinds: Transport, Message, Mixed Must match the requirements of the service Credentials: l Windows l X.509 l Username/password l Cardspace Message integrity / confidentiality l SSL l WS-Security Authorization l .NET Role-based security l PrincipalPermission / IsInRole

Reliability
n

n n n

Reliable transports l TCP l IPC WCF reliability deals with message reliability Enables order delivery assurance Reliability is controlled in the binding l Supported/not supported l Enabled/disabled

2007 Brian Noyes, IDesign Inc All Rights Reserved

16

Reliability
n

Reliability not supported l Basic binding l Peer TCP binding l MSMQ/MSMQ integration binding Reliability always enabled l WS dual binding Reliability disabled by default but can be enabled l TCP binding l WS binding Reliable by definition while not using WCF reliability l Named pipes

Queuing
n

Message queuing technologies allow communications that are: l Asynchronous l One way l Buffered l Disconnected Great for submit types of operations where you dont need immediate feedback in the client of service side processing

2007 Brian Noyes, IDesign Inc All Rights Reserved

17

Queued Calls
MSMQ Queue Proxy
MSMQ Message

Dispatcher

Client

Service

MSMQ Channel Listener

Dispatcher

Service

Client Initiated Transactions


n n n

Indigo transactions built on top of System.Transactions + WS-* Initiate a transaction in client code Transaction is flowed to all services called that support transactions

using (TransactionScope scope = new TransactionScope()) { // Call Service1 transaction flowed to service1 // Call Service2 transaction flowed to service2 scope.Complete(); } // All commit or all rollback

2007 Brian Noyes, IDesign Inc All Rights Reserved

18

Enabling Transactions
n n

Transactions do not flow by default Requirements: l Enclose service calls in transaction scope on client l transactionFlow = true on binding l [TransactionFlow] attribute on service contract methods

Optional specify transaction scoping options TransactionFlowRequired = true TransactionAutoComplete = true

OperationBehavior attribute on service methods


Peer-to-Peer Communications
n n n n n

No true multicast in V1 Each client is a also service Must call every other (interested) client directly Can simulate with duplex channels Can use PeerChannel

2007 Brian Noyes, IDesign Inc All Rights Reserved

19

Resources
n n n n

Programming WCF, Juval Lowy, OReilly & Associates. Learning WCF, Michele Leroux Bustamante, OReilly & Associates. IDesign WCF Master Class, www.idesign.net. IDesign articles: http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=1&tabid=9 WCF Dev Site: http://msdn.microsoft.com/wcf

E-mail: brian.noyes@idesign.net Blog: http://briannoyes.net

2007 Brian Noyes, IDesign Inc All Rights Reserved

20

You might also like