MessageHandlerFactory broken for decorated handlers ?

Topics: Web Api
Dec 2, 2011 at 12:31 PM

Hi,

I'm facing the following problem when resolving decorated message handlers. The code snippet below is simplified (poor man DI) but the same error occures using Windsor decorators registration or MEF: 

config.MessageHandlerFactory = () =>
{						var list = new List<DelegatingHandler>();
			                               		list.Add(new AuthenticatingHandler(new OperationAccessCheckingHandler(), new DefaultAuthenticationProvider()));
			                               		return list;
};

in fact on the line return list I have the following error :

Error 500

"The server encountered an error processing the request. See server logs for more details."

I have only two MessageHandlers which are below :

public class AuthenticatingHandler : DelegatingHandler
	{
		public string KeyHeaderName = HttpRequestHeader.Authorization.ToString();
		private readonly IAuthenticationProvider _authenticationProvider;

		public AuthenticatingHandler(
										HttpMessageHandler innerHandler, 
						IAuthenticationProvider authenticationProvider)
									: base(innerHandler)
		{
			_authenticationProvider = authenticationProvider;
		}

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

and

public class OperationAccessCheckingHandler : DelegatingHandler
	{
		// do nothing for the moment, just for test
	}

Any ideas ?

Thanks,

Thomas

 

Coordinator
Dec 2, 2011 at 7:30 PM

What exception do you see getting thrown and what is the error message?

Daniel Roth

Dec 3, 2011 at 10:23 PM

Hi Daniel,

No errors, just stoping debugging and throwing error 500 in fiddler. But I think it's because I'm passing an inner handler that obviously I shouldn't do.

According to Glenn from this thread : http://wcf.codeplex.com/discussions/281439


You shouldn't use the ctor that accepts a message handler if you are using IoC. Handlers expose an inner handler property. The message handler factory will automatically set the inner handler appropriately if you pass it existing handler instances. Thus you can return a collection of handlers and they will automatically get wired. The handlers themselves should provide a constructor that does not expect the inner handler as a constructor param. If you look at the ContactManager_Advanced example you will see that several handlers get wired up and each uses a constructor similar to your second example above i.e. it without an inner handler.

Coordinator
Dec 3, 2011 at 11:03 PM

The factory validates if you set any handlers and will throw an exception. If you put on break on all errors, or use the service trace viewer you will see the error.

Dec 5, 2011 at 8:39 AM

Glenn, I think the problem comes from the fact that I'm using the decorated services with the innerHandler argument. You explain this in my other question : http://wcf.codeplex.com/discussions/281439. I didn't trace it for the moment but will check it out to be sure.

Thomas

Dec 5, 2011 at 9:37 AM
Edited Dec 5, 2011 at 9:37 AM

Glenn, I enabled the trace and, nothing at all. Request stops with error 500, debugging stops (no exception is shown), nothing in the windows event viewer and nothinf in WCF trace viewer. I'm pretty sure that the problem comes from the fact that Windsor castle registers MessageHandlers as decorators, passing the next message handler as argument (innerHandler) to the previous message handler. What do you think ?

Thomas

Dec 6, 2011 at 8:31 AM

The answer is, it's not broken, by design I should not wire up decorators. Read the full story here : http://wcf.codeplex.com/discussions/281439