15
Inverting Control in WCF Chris Deweese photo by brentdanley via Flic

Inverting Control in WCF

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

Page 1: Inverting Control in WCF

Inverting Control in WCF

Chris Deweese

photo by brentdanley via Flickr

Page 2: Inverting Control in WCF

• .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

Page 3: Inverting Control in WCF

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

Page 4: Inverting Control in WCF

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

Page 5: Inverting Control in WCF

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.

Page 6: Inverting Control in WCF

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.

Page 8: Inverting Control in WCF

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.

Page 9: Inverting Control in WCF

WCF Hosting Environment

Page 10: Inverting Control in WCF

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

Page 11: Inverting Control in WCF

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.

Page 12: Inverting Control in WCF

“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

Page 13: Inverting Control in WCF

Ode to code.

photo by Premshree via Flickr

Page 14: Inverting Control in WCF

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.

Page 15: Inverting Control in WCF

Discussion/Questions

• Thanks for your time. Let’s talk if you have any tips or questions.