This project has moved and is read-only. For the latest updates, please go here.

Exception when trying to resolve decorators

Dec 1, 2011 at 3:59 PM


I'm trying to resolve with MEF a collection of decorated types but I'm encountering the problem that I don't know how to solve. Here's the information that I have :

Internal error occurred. Additional information: ''.

   at System.ComponentModel.Composition.Hosting.ImportEngine.InPrerequisiteLoop()
   at System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImportsStateMachine(PartManager partManager, ComposablePart part)
   at System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImports(PartManager partManager, ComposablePart part, Boolean shouldTrackImports)
   at System.ComponentModel.Composition.Hosting.ImportEngine.SatisfyImports(ComposablePart part)
   at System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition)
   at System.ComponentModel.Composition.Hosting.CatalogExportProvider.CatalogExport.GetExportedValueCore()
   at System.ComponentModel.Composition.ExportServices.GetExportedValueFromLazy[T](Export export)
   at System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValuesCore[T](String contractName)

Here's my classes :


[Export("innerMost", typeof(DelegatingHandler))]
	public class OperationAccessCheckingHandler : DelegatingHandler
		protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)


[Export("topMost", typeof(DelegatingHandler))]
	public class AuthenticatingHandler : DelegatingHandler
		public string KeyHeaderName = HttpRequestHeader.Authorization.ToString();
		private readonly IAuthenticationProvider _authenticationProvider;

		public AuthenticatingHandler([Import("innerMost", typeof(DelegatingHandler))]HttpMessageHandler innerHandler, IAuthenticationProvider authenticationProvider)
			: base(innerHandler)
			_authenticationProvider = authenticationProvider;

		protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)

when I resolve it :

// or

I get this exception.

I tried to export concrete classes but the same exception is thrown. Do you have any idea what's going bad ?



Dec 1, 2011 at 6:40 PM

Hi Thomas,

This might be a function of the fact that you have only pasted a partial code snipped, but can you confirm that you have an Export for IAuthenticationProvider.

In addition to what I can tell HttpMessageHandler is an abstract class, which you should not be able to instantiate. Again I might be missing something, so if you could clarify your example it might help with providing you an answer.




Dec 2, 2011 at 8:38 AM

Hi alok,

I'm sorry for the partial code snippet. I can confirm that I have an export for IAuthenticationProvider. I'm exporting AuthenticatingHandler and OperationAccessCheckingHandler which are concrete classes. What's strange is that while resolving in debug mode I can see that OperationAccessCheckingHandler is injected as innerHandler to the constructor of AuthenticatingHandler. But when it exit the constructor the exception is thrown from container.GetExportedValues<DelegatingHandler>("topMost");. When I try just to resolve AuthenticatingHandler without decorated OperationAccessCheckingHandler all works well.

At the end I wanted to say that I'm using the Framework 4 System.ComponentModel.Composition and not the newest preview.

Thank you,


Feb 2, 2012 at 10:05 PM

Hi Thomas,

How do you solve the issue?, we are facing it once in a while.


Thanks in advance,


Feb 3, 2012 at 9:16 PM

Hi Gustavo,


My problem was related to wcf web api and the fact that message handlers doesn't support decorators. Nothing to do with med in fact.


Do you want more info on it ?




Feb 3, 2012 at 9:24 PM
Yes please,

Any additional help will be greatly appreciated.

Thanks Thomas,


From: [email removed]
To: [email removed]
Date: Fri, 3 Feb 2012 13:16:33 -0800
Subject: Re: Exception when trying to resolve decorators [MEF:281475]

From: tjaskula
Hi Gustavo,

My problem was related to wcf web api and the fact that message handlers doesn't support decorators. Nothing to do with med in fact.

Do you want more info on it ?

Read the full discussion online.
To add a post to this discussion, reply to this email (
To start a new discussion for this project, email
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at
Feb 4, 2012 at 10:50 AM


The problem in WCF Web API is that you can't just hand over to the framework decorated message handlers because you'll end up with the error I posted here. So it's not related to MEF. The same happens if you use another IoC framewok like Castle or StructureMap. For WCF Web API it's not to you and the underlying IoC framework to wire up the decoration of your instances. So you just can't use the costructor injection in the message handlers to achieve this. You have just to pass while registering a message handlers an ordered collection and the WCF Web API will wire up the decorators itself. I don't know how to resolve instances from MEF in a ordered way (because I want a special order in which the decorators should be wired) so I gave up with MEF and implemented it with Castle and a custome handler filter to achieve it I think it's a design flaw in WCF Web API to not allow you to wire up the decorators upfront. It's easier to do it then to implement ordered collection resolution.

I hope it helps. Let me know if you want more information.