Connecting Applications With Wcf
Transcript of Connecting Applications With Wcf
-
7/28/2019 Connecting Applications With Wcf
1/20
12007 Brian Noyes, IDesign Inc All Rights Reserved
2008 Brian Noyes, IDesign Inc. All rights reserved
Brian Noyes
www.idesign.net
Connecting Applications with Windows
Communication Foundation
About Brian
n Chief Architect, IDesign Inc. (www.idesign.net)
n Microsoft Regional Director / MVP
n 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
n Speaking
l Microsoft TechEd US, Europe, Malaysia, Visual Studio Connections,DevTeach, INETA Speakers Bureau, MSDN Webcasts
n Participates in Microsoft Design Reviews
n E-mail:[email protected]
n Blog: http://briannoyes.net
http://www.idesign.net/http://www.idesign.net/mailto:[email protected]://briannoyes.net/http://briannoyes.net/mailto:[email protected]://www.idesign.net/http://www.idesign.net/ -
7/28/2019 Connecting Applications With Wcf
2/20
22007 Brian Noyes, IDesign Inc All Rights Reserved
Agenda
n Connected Systems Concerns
n WCF Overview
n WCF ABCs and Es
n Defining Services
n Connecting Clients
n Passing Data
n Advanced Capabilities
Connected Systems in a SO World
n Need to connect clients to back-end services
n Need to connect services to other services
n Need to be decoupled from the specific service technology / calling patterns
n Need support for distributed system capabilities:
l Service hosting
l Instance management
l Reliability
l Async communications
l Transactions
l Security
-
7/28/2019 Connecting Applications With Wcf
3/20
32007 Brian Noyes, IDesign Inc All Rights Reserved
SOA Tenets
n Service boundaries are explicit
n Services are described by policy
n Services expose contract and schema, not types
n 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
n WCF is the best available remoting technology even if you are
just doing client/server, point-to-point communication
-
7/28/2019 Connecting Applications With Wcf
4/20
42007 Brian Noyes, IDesign Inc All Rights Reserved
Agenda
n Connected Systems Concerns
n WCF Overview
n WCF ABCs and Es
n Defining Services
n Connecting Clients
n Passing Data
n Advanced Capabilities
WCF Overview
n Framework for connecting distributed applications
n Part of .NET 3.0 and later
n Enables building SOA applications
n Implements WS-* standards
n Single API that supports the capabilities of:
l .NET Remoting
l ASP.NET Web Services
l Enterprise Services (COM+)
l MSMQ
-
7/28/2019 Connecting Applications With Wcf
5/20
52007 Brian Noyes, IDesign Inc All Rights Reserved
WCF Overview
n Messaging subsystem at its core
l SOAP messages
Does not necessarily mean XML/Text!
n 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
n Flexibility
l You can change your mind about wire level communication protocols, security
model, and many other facets without changing your source code
n Maintainability
l One API for all forms of remote communication, dont need to learn multiple
technology stacks
n Power
l All the capabilities of other remoting APIs rolled into one
l Interoperability and SOA enabled in ways the other technologies never considered
-
7/28/2019 Connecting Applications With Wcf
6/20
62007 Brian Noyes, IDesign Inc All Rights Reserved
Agenda
n Connected Systems Concerns
n WCF Overview
n WCF ABCs and Es
n Defining Services
n Connecting Clients
n Passing Data
n Advanced Capabilities
WCF ABCs and Es
n Clients and services communicate through Endpoints
ClientClient ServiceService
MessageMessageEndpointEndpoint EndpointEndpoint
EndpointEndpoint
-
7/28/2019 Connecting Applications With Wcf
7/20
72007 Brian Noyes, IDesign Inc All Rights Reserved
WCF ABCs and Es
n Enpoints are composed of Address, Binding, Contract
ClientClient ServiceService
MessageMessageAABBCC AA BB CC
AA BB CC
AddressAddress BindingBinding ContractContract
(Where)(Where) (How)(How) (What)(What)
WCF Addresses
n Location of the service
n URL
l Protocol
l Server/domain
l Port
l Service URI (optional)
n Examples:
l http://myappserver:2112/
l http://my.service.domain.com/SomeServiceURI
l net.tcp://myappserver:2112/SomeServiceURI
http://my.service.domain.com/SomeServiceURIhttp://my.service.domain.com/SomeServiceURI -
7/28/2019 Connecting Applications With Wcf
8/20
82007 Brian Noyes, IDesign Inc All Rights Reserved
WCF Bindings
n The brains of the communications
n Encapsulates what happens on the wire
l Wire level protocol
l Communication patterns
l Message format
l Security
l Transactions
l
Reliabilityn Can all be extracted out of code and put in config
WCF Bindings
n WCF defines several standard bindings
l Basic
l TCP
l Peer TCP
l Named Pipe
l WS
n Many variation bindings
l Context, Dual, Federation, etc
n Can customize standard bindings
n Can define custom bindings
-
7/28/2019 Connecting Applications With Wcf
9/20
92007 Brian Noyes, IDesign Inc All Rights Reserved
WCF Contracts
n Service Contracts
l Operations exposed by the service
n Data Contracts
l What data is passed through the service operations
n Message Contracts
l What the message looks like when serialized
n Fault Contracts
l
What error messages look like
Agenda
n Connected Systems Concerns
n WCF Overview
n WCF ABCs and Es
n Defining Services
n Connecting Clients
n Passing Data
n Advanced Capabilities
-
7/28/2019 Connecting Applications With Wcf
10/20
102007 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
-
7/28/2019 Connecting Applications With Wcf
11/20
112007 Brian Noyes, IDesign Inc All Rights Reserved
Agenda
n Connected Systems Concerns
n WCF Overview
n WCF ABCs and Es
n Defining Services
n Connecting Clients
n Passing Data
n 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 class
n Proxy exposes service contract methods
n Create instance of proxy, call methods
-
7/28/2019 Connecting Applications With Wcf
12/20
122007 Brian Noyes, IDesign Inc All Rights Reserved
Agenda
n Connected Systems Concerns
n WCF Overview
n WCF ABCs and Es
n Defining Services
n Connecting Clients
n Passing Data
n Advanced Capabilities
Passing Data
n Based on Data Contracts
n .NET primitives work out-of-box
l Serializable
n Any serializable type by default
n XmlSerializable types if you change the formatter
n Best practice:
l Define data contracts for custom types used in the service
contract [DataContract] attribute on the entity type
[DataMember] attribute on the properties you want to be contained in
the data transfered
-
7/28/2019 Connecting Applications With Wcf
13/20
132007 Brian Noyes, IDesign Inc All Rights Reserved
Agenda
n Connected Systems Concerns
n WCF Overview
n WCF ABCs and Es
n Defining Services
n Connecting Clients
n Passing Data
n Advanced Capabilities
Advanced Capabilities
n Asynchronous calls
n Callbacks
n Sessions
n Security
n Reliability
n Queuing
n Transactions
n Peer to Peer
-
7/28/2019 Connecting Applications With Wcf
14/20
142007 Brian Noyes, IDesign Inc All Rights Reserved
Asynchronous calls
n Service calls can:
l Take a long time
l Be one-way
l Call back into client
l Be re-entrant
n Need to make calls asynchronously
n Handle completion / error handling
n 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 = true
l Register callback method
l Gets called on background thread, must marshal results
yourself to UI thread (ISynchronizeInvoke)
-
7/28/2019 Connecting Applications With Wcf
15/20
152007 Brian Noyes, IDesign Inc All Rights Reserved
Callbacks
n Service can call client
l Events
n Need to register client callback contract on service
l ServiceContract.CallbackContract
n Need to obtain callback channel instance in service
l OperationContext.GetCallbackChannel
n Callbacks happen asynchronously from client perspective
Sessions
n Often service calls are related to one another
l Start Order
l Add Order Items
l Submit Order
n Individual service calls are just messages
l Underlying protocol does not support sessions
n Need to relate multiple messages together
n Sessions are maintained through shared service instances on service side
n
NOTE: Session is the default instancing moden NOTE: Sessions are BAD for scalability
n NOTE: Sessions get messy with transactions / web farms / etc.
-
7/28/2019 Connecting Applications With Wcf
16/20
162007 Brian Noyes, IDesign Inc All Rights Reserved
Security
n Three kinds: Transport, Message, Mixed
n Must match the requirements of the service
n Credentials:
l Windows
l X.509
l Username/password
l Cardspace
n Message integrity / confidentiality
l SSL
l WS-Security
n Authorization
l .NET Role-based securityl PrincipalPermission / IsInRole
Reliability
n Reliable transports
l TCP
l IPC
n WCF reliability deals with message reliability
n Enables order delivery assurance
n Reliability is controlled in the binding
l Supported/not supported
l Enabled/disabled
-
7/28/2019 Connecting Applications With Wcf
17/20
172007 Brian Noyes, IDesign Inc All Rights Reserved
Reliability
n Reliability not supported
l Basic binding
l Peer TCP binding
l MSMQ/MSMQ integration binding
n Reliability always enabled
l WS dual binding
n Reliability disabled by default but can be enabled
l
TCP bindingl WS binding
n 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
n Great for submit types of operations where you dont need
immediate feedback in the client of service side processing
-
7/28/2019 Connecting Applications With Wcf
18/20
182007 Brian Noyes, IDesign Inc All Rights Reserved
Queued Calls
ClientProxy
DispatcherMSMQ
Queue
MSMQ
Message
Service
MSMQ
Channel
Listener
Dispatcher Service
Client Initiated Transactions
n Indigo transactions built on top of System.Transactions + WS-*
n Initiate a transaction in client code
n 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 service2scope.Complete();
} // All commit or all rollback
-
7/28/2019 Connecting Applications With Wcf
19/20
192007 Brian Noyes, IDesign Inc All Rights Reserved
Enabling Transactions
n Transactions do not flow by default
n 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
l OperationBehavior attribute on service methods
TransactionFlowRequired = true
TransactionAutoComplete = true
Peer-to-Peer Communications
n No true multicast in V1
n Each client is a also service
n Must call every other (interested) client directly
n Can simulate with duplex channels
n Can use PeerChannel
-
7/28/2019 Connecting Applications With Wcf
20/20
202007 Brian Noyes IDesign Inc All Rights Reserved
Resources
n Programming WCF, Juval Lowy, OReilly & Associates.
n Learning WCF, Michele Leroux Bustamante, OReilly &Associates.
n IDesign WCF Master Class, www.idesign.net.
n IDesign articles:http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=-1&tabid=9
n WCF Dev Site: http://msdn.microsoft.com/wcf
E-mail: [email protected]
Blog: http://briannoyes.net
http://www.idesign.net/http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=http://msdn.microsoft.com/wcfmailto:[email protected]://briannoyes.net/http://briannoyes.net/mailto:[email protected]://msdn.microsoft.com/wcfhttp://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=http://www.idesign.net/