WCF Web Api MessageHandler Gotcha

If you find yourself getting this error message when you change your URL in the test client between you different APIs …

The HttpClient instance already started one or more requests. Properties can only be modified before sending the first request.

You have fallen foul of a little WCF Web Api gotcha. Message handlers need to be created each time they are required. I was using Castle Windsor to handle the lifetime of my message handlers

public class MessageHandlerInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        var path = Assembly.GetExecutingAssembly().GetDirectoryName();
        var filter = new AssemblyFilter(path, "ByBox*.dll");

        container.Register(
            AllTypes
                .FromAssemblyInDirectory(filter)
                .Where(t => t.IsSubclassOf(typeof (DelegatingHandler)))
                .Configure(c => c.LifeStyle.Custom(InstallerContext.LifestyleManager))
            );
    }
}

So, I’m using my InstallerContext’s lifestyle, but what I actually needed was this …

.Configure(c => c.LifeStyle.Transient)

I found this in a discussion on the wcf codeplex site

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s