Yet Another WCF Service Instance Provider

So here's a lightweight handy WCF service instance provider that takes a slightly different approach than most of what we've seen so far. Usually most people want to use an IInstanceProvider implementation to work with their DI container of choice. Common examples would be the integration of Castle, StructureMap, or Unity with WCF to allow container based instantiation of service implementations at runtime. Thinking to myself however that this required the use of the configuration file to add or detach the required behaviors limited us to not using Service Behavior Attributes, or if we went down that path, it would require us to take hard dependencies on the container type, albeit indirectly. Coupled with the fact that your IInstanceProvider instances themselves require the use of a default contructor (afterall, who wants to be in the IInstanceProviderProvider insanity, right +D) this means that you are in the situation of requiring a Singleton container reference, ala the Common Service Locator "ambient container"  or more likely a static container api that can talk to some initialized container instance for you under the covers.

All this got me thinking however about turning this upside down. Instead, wouldn't we rather be in a world were we could use one IInstanceProvider regardless of container implementation, or even WITHOUT A CONTAINER? Shouldn't instead we be able to leverage something that can assist us in leveraging ANY resolution methodology we desire from the host? So if that was the goal then perhaps this approach satisfies the need. Instead of leveraging ambient containers I thought of leveraging static events into the pattern that can supply the requested instance. How you decided to handle the event is completely up to you, the hosting application. You may decide to use your choice of container, pull from a factory, or instead use the Activator.CreateInstance() method and assume the presence of a default constructor (replicating the behavior of the default WCF approach).

Try out the attached code and see if it's an approach you like.

Comments (0)

Skip to main content