MEF + WCF +IIS?

Dec 29, 2008 at 6:22 PM
I'm wanting to use MEF in my WCF application (hosted in IIS thus complicating things) and I'm wondering what the prescribed approach is.

What I've been doing is the method any other IoC container (Autofac, StructureMap, Windsor) does for WCF, which is providing a custom ServiceHostFactory, ServiceHost, IInstanceProvider, etc...

Is this the way to do it? Or is there a more transparent way of doing this that I'm missing?
Dec 29, 2008 at 6:37 PM

Hi @Jblosser.

Matt Hawley has a nice blog post on doing this through an ExportProvider or through a custom catalog.

http://weblogs.asp.net/mhawley/archive/2008/11/29/mef-factories-using-an-export-provider.aspx

Basically in the post he shows you how to register custom factories that will handle creating services. In his example though, the services themselves are not getting injected with further dependencies. If you want to do that, it would require creating a custom programming model (implementing your own ComposablePart and ComposablePartDefinition).

Glenn

From: jblosser [mailto:notifications@codeplex.com]
Sent: Monday, December 29, 2008 11:23 AM
To: Glenn Block
Subject: MEF + WCF +IIS? [MEF:43139]

From: jblosser

I'm wanting to use MEF in my WCF application (hosted in IIS thus complicating things) and I'm wondering what the prescribed approach is.

What I've been doing is the method any other IoC container (Autofac, StructureMap, Windsor) does for WCF, which is providing a custom ServiceHostFactory, ServiceHost, IInstanceProvider, etc...

Is this the way to do it? Or is there a more transparent way of doing this that I'm missing?

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Dec 30, 2008 at 1:22 PM
Hi Glenn,

In order to get my WCF services "inside" the MEF container (so marking them [Export] and the [Import]s get filled by the container), I need to either:
a) hack it in the constructor of the WCF service (adding itself to the container)
b) create a custom WCF ServiceHostFactory that allows me to call CompositionContainer.GetExportedObject()

If you look at IoC containers like StructureMap/Windsor/Autofac, they will follow option B (which I have done for MEF as well).

I can see from Matt's posts where using a custom ExportProvider enables my to substitute my own factory, however I would be using that code inside the custom ServiceHostFactory still correct? I still need to resolve my WCF service via the CompositionContainer unless MEF does some magic that I'm unaware of...

One drawback that I have found is that the WCF factory logic could to break things. For example:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
will apparently bypass the custom ServiceHostFactory and so the service never gets resolved from the CompositionContainer.

So, I guess my question boils down to which method is better:
  Method A -- Add the WCF Service to the container when it's constructor is called
    - Less framework code
    - WCF ServiceBehaviors work
    - Less "pure" from an IoC standpoint
    - Added logic in the constructor (could be abstracted to a base class though)

  Methob B - Use a custom ServiceHostFactory to resolve the WCF Service from the CompositionContainer
    - A lot more framework code to support the ServiceHostFactory
    - WCF ServiceBehaviors might break it (this could be due to me not grokking WCF factories though)
    - More "pure" from an IoC standpoint
    - No need for special logic in the WCF service code

  Method C - Some other method I am unaware of

If I were self-hosting WCF, I don't think this would be as much of an issue. But hosting WCF inside of IIS seems to be insanely more complicated.