Exception when trying to resolve decorators

Dec 1, 2011 at 2:59 PM

Hi,

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 :

innerMost

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

outer

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

		[ImportingConstructor]
		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 :

container.GetExportedValues<DelegatingHandler>();
// or
container.GetExportedValues<DelegatingHandler>("topMost");

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 ?

Thanks,

Thomas

Coordinator
Dec 1, 2011 at 5: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.

thanks

-alok

 

Dec 2, 2011 at 7: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,

Thomas

Feb 2, 2012 at 9:05 PM

Hi Thomas,

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

 

Thanks in advance,

Gustavo

Feb 3, 2012 at 8: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 ?

 

 

Thomas

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

Any additional help will be greatly appreciated.

Thanks Thomas,

Gustavo


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 ?


Thomas
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 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
Feb 4, 2012 at 9:50 AM

Gustavo,

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 http://mookid.dk/oncode/archives/2295. 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.

Thomas