Upload
chris-deweese
View
2.257
Download
0
Tags:
Embed Size (px)
DESCRIPTION
WCF provides many extension points and in this session Chris will cover how to wire up an Inversion of Control container into the WCF stack. Chris will discuss behaviors, instance providers, and the architecture of WCF in order to illustrate the best way to invert control in your services.
Citation preview
Inverting Control in WCF
Chris Deweese
photo by brentdanley via Flickr
• .NET Developer/Architect– Specialties: SOA, Xml, Distributed Messaging, Object-
Oriented Design– Primarily work on System Integration & Information
Sharing projects• 2009 Microsoft MVP, Solutions Architect• Twitter @cdeweese• http://christopherDeweese.com/• Avid Xbox player (when kids are sleeping)
About Chris Deweese
At a Glance
• Brief overview of Inversion of Control– The “container”
• Quick Overview of WCF Hosting Environment• Short Overview of WCF Extensibility• Where to invert control in WCF• Ode to Code• Review
Wikipedia: Inversion of Control
• In object-oriented programming, the dependency inversion principle refers to a specific form of decoupling where conventional dependency relationships established from high-level, policy-setting modules to low-level, dependency modules are inverted (e.g. reversed) for the purpose of rendering high-level modules independent of the low-level module implementation details. The principle states:
• A. High-level modules should not depend on low-level modules. Both should depend on abstractions.
• B. Abstractions should not depend upon details. Details should depend upon abstractions.
http://en.wikipedia.org/wiki/Dependency_inversion_principle
English […]! Do you speak it?
• Reduce coupling between modules (classes, components, subsystems, etc) by using abstractions (interfaces, base classes).
• Modules built to require dependencies be provided.• At runtime the dependencies will be provided so
the module can do it’s job.• Example: Class PersonService requires an
IPersonRepository instance in order to perform its work.
The “Container”
• Inversion of Control containers are a pattern to implement the dependency inversion principle.
• The “container” is used to locate dependencies typically by type <T> or type <T> + a unique key.
Containers for .NET
• Structure Map• Castle Windsor• Unity• Spring.Net• AutoFac• Ninject• … Others via Scott Hanselman’s Computer Zen
WCF Hosting Environment
• Service instances hosted inside a service host and service context
• Service Instance Provider responsible for managing service instance lifetimes– If service instance faults the service host will abort
and destroy the instance• Default is per call instancing, i.e., every call to
your endpoint creates new instance of your service*
*This is probably a key point.
WCF Hosting Environment
WCF Extensibility
• WCF Extensible through many means– Behaviors, Instance Providers, Hosts, Dispatchers,
and more. All done via:• Overriding Base classes• Implementing Interfaces
• For Today’s discussion we’ll focus on Service Behaviors, Instance Providers, Service Hosts, and Service Host Factories
Service Behavior?
• Controls the local execution of a service• Applied to alter the services behavior at
runtime• Service Behavior must implement
System.ServiceModel.Description.IServiceBehavior.• Service Behavior applied through code by a
custom service host or through configuration.
“Best” location to attach container?
• In WCF, for a container to serve all instances it must be hosted at the Service Host level
• The container will be available to pass dependencies to the service instances at runtime
• Created once and serves all instances of the service
The Solution
• Custom Instance Provider– Provides instances of your service class to the WCF
Runtime.• Custom Service Behavior– To substitute your instance provider for the default one.
• Custom Service Host– Controls the container.
• Custom Service Host Factory– Initializes container, creates custom service host and
passes container to custom service host.
Discussion/Questions
• Thanks for your time. Let’s talk if you have any tips or questions.