Professional Documents
Culture Documents
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
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
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
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
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
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
WCF Overview
n
Interoperability with: l Enterprise Services l ASMX / Web Services 1.0 services l Any WS-* compliant web service l MSMQ applications
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
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
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
Client
Service
Endpoint
Endpoint
Message
Endpoint
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
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
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
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
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
Dynamically create
ChannelFactory<T> class
n n
Proxy exposes service contract methods Create instance of proxy, call methods
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
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
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
true
Register callback method Gets called on background thread, must marshal results yourself to UI thread (ISynchronizeInvoke)
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.
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
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
17
Queued Calls
MSMQ Queue Proxy
MSMQ Message
Dispatcher
Client
Service
Dispatcher
Service
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
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
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
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
20