Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Inverting Control in WCF


Published on

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.

Published in: Technology

Inverting Control in WCF

  1. 1. Inverting Control in WCF<br />Chris Deweese<br />photo by brentdanley via Flickr<br />
  2. 2. About Chris Deweese<br />.NET Developer/Architect<br />Specialties: SOA, Xml, Distributed Messaging, Object-Oriented Design<br />Primarily work on System Integration & Information Sharing projects<br />2009 Microsoft MVP, Solutions Architect<br />Twitter @cdeweese<br /><br />Avid Xbox player (when kids are sleeping)<br />
  3. 3. At a Glance<br />Brief overview of Inversion of Control<br />The “container”<br />Quick Overview of WCF Hosting Environment<br />Short Overview of WCF Extensibility<br />Where to invert control in WCF<br />Ode to Code<br />Review<br />
  4. 4. Wikipedia: Inversion of Control<br />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:<br />A. High-level modules should not depend on low-level modules. Both should depend on abstractions.<br />B. Abstractions should not depend upon details. Details should depend upon abstractions.<br /><br />
  5. 5. English […]! Do you speak it?<br />Reduce coupling between modules (classes, components, subsystems, etc) by using abstractions (interfaces, base classes).<br />Modules built to require dependencies be provided.<br />At runtime the dependencies will be provided so the module can do it’s job.<br />Example: Class PersonService requires an IPersonRepository instance in order to perform its work.<br />
  6. 6. The “Container”<br />Inversion of Control containers are a pattern to implement the dependency inversion principle.<br />The “container” is used to locate dependencies typically by type &lt;T&gt; or type &lt;T&gt; + a unique key.<br />
  7. 7. Containers for .NET<br />Structure Map<br />Castle Windsor<br />Unity<br />Spring.Net<br />AutoFac<br />Ninject<br />… Others via Scott Hanselman’s Computer Zen<br />
  8. 8. WCF Hosting Environment<br />Service instances hosted inside a service host and service context<br />Service Instance Provider responsible for managing service instance lifetimes<br />If service instance faults the service host will abort and destroy the instance<br />Default is per call instancing, i.e., every call to your endpoint creates new instance of your service*<br />*This is probably a key point.<br />
  9. 9. WCF Hosting Environment<br />
  10. 10. WCF Extensibility<br />WCF Extensible through many means<br />Behaviors, Instance Providers, Hosts, Dispatchers, and more. All done via:<br />Overriding Base classes<br />Implementing Interfaces<br />For Today’s discussion we’ll focus on Service Behaviors, Instance Providers, Service Hosts, and Service Host Factories<br />
  11. 11. Service Behavior?<br />Controls the local execution of a service<br />Applied to alter the services behavior at runtime<br />Service Behavior must implement System.ServiceModel.Description.IServiceBehavior.<br />Service Behavior applied through code by a custom service host or through configuration.<br />
  12. 12. “Best” location to attach container?<br />In WCF, for a container to serve all instances it must be hosted at the Service Host level<br />The container will be available to pass dependencies to the service instances at runtime<br />Created once and serves all instances of the service<br />
  13. 13. Ode to code.<br />photo by Premshree via Flickr<br />
  14. 14. The Solution<br />Custom Instance Provider<br />Provides instances of your service class to the WCF Runtime.<br />Custom Service Behavior <br />To substitute your instance provider for the default one.<br />Custom Service Host<br />Controls the container.<br />Custom Service Host Factory<br />Initializes container, creates custom service host and passes container to custom service host.<br />
  15. 15. Discussion/Questions<br />Thanks for your time. Let’s talk if you have any tips or questions.<br />