Service Extension Model
Many of the WCF runtime classes that support adding new state do so through a common extension model. The extension model has three pieces: an extensible runtime object whose behavior can be modified by attaching state exceptions, extensions that modify the runtime object, and a collection that holds all of the extensions being used with a particular instance of the runtime object.
An extensible runtime object implements the IExtensibleObject interface, which provides a place for the extensions to be attached. The unusual generic type is used to make sure that only extensions designed for the extensible object can be added.
public interface IExtensibleObject<T> where T : IExtensibleObject<T>
{
IExtensionCollection<T> Extensions { get; }
}
An extension collection works like a standard collection, with the additional ability to find an extension in the collection based on its type. You'll notice that the same generic type signature carries through all of the extension model classes.
public interface IExtensionCollection<T> : ICollection<IExtension<T>>, IEnumerable<IExtension<T>>, IEnumerable where T : IExtensibleObject<T>
{
E Find<E>();
Collection<E> FindAll<E>();
}
Finally, the extensions that go in the extension collection have two methods that they implement.
public interface IExtension<T> where T : IExtensibleObject<T>
{
void Attach(T owner);
void Detach(T owner);
}
Attach is called when the extension is added to an extension collection. Detach is called when the extension is removed from the collection. The extension is only associated with a particular owner extensible object between the calls to Attach and Detach.
Extensible objects appear in a few places: service hosts, instances, operation invocations, and client proxies. These correspond to the extensible types ServiceHostBase, InstanceContext, OperationContext, and IContextChannel. Attaching an extension to one of these objects allows you to share state within the lifetime of that scope.
Here's a past example demonstrating using the extension model to attach state to a client proxy.
Comments
Anonymous
December 16, 2008
Hello, Could you provide a more simple sample where I can use this approach? Is this only for WCF or any other app? CheersAnonymous
December 16, 2008
PingBack from http://itknowledgeexchange.techtarget.com/serviceendpoint/wmoc-sharp-40-zen-of-results-ebook/Anonymous
December 17, 2008
Hi Steven, This is only a pattern used in WCF. Someone else could use the extension object classes we provide but it's not intended to be the official extension mechanism of the framework.