WebApi - Autofac cannot resolve parameter HttpRequestMessage <p>I have an issue with <code>RegisterHttpRequestMessage</code> not working for me and cannot figure out what I'm doing wrong. This is specifically when I try to manually resolve a service that accepts the HttpMessageRequest as a parameter. </p> <p>I'm using modules to register components in my builder, and currently my module in the main web project looks like this:</p> <pre><code>builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterHttpRequestMessage(GlobalConfiguration.Configuration); builder.RegisterType&lt;SourceSystemViewModel&gt;().AsImplementedInterfaces().InstancePerRequest(); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) .Where(t =&gt; t.Name.EndsWith("ViewModelValidator")) .AsImplementedInterfaces() .PropertiesAutowired(); // Etc etc etc </code></pre> <p><code>SourceSystemViewModel</code> is currently quite simple and looks like this:</p> <pre><code>public interface ISourceSystemViewModel { SourceSystem Value { get; } } public class SourceSystemViewModel : ISourceSystemViewModel { public SourceSystemViewModel(HttpRequestMessage request) { Value = request.Headers.GetSourceSystem(); } public SourceSystem Value { get; } } </code></pre> <p>GetSourceSystem is just an extension method that pulls out the header value. I have tried both registering <code>SourceSystemViewModel</code> with and without <code>InstancePerRequest</code> but it doesn't make a difference. The moment autofac tries to resolve <code>ISourceSystemViewModal</code> (and ultimately <code>HttpRequestMessage</code>) it throws this:</p> <blockquote> <p>An exception of type 'Autofac.Core.Registration.ComponentNotRegisteredException' occurred in Autofac.dll but was not handled in user code</p> <p>Additional information: The requested service 'System.Net.Http.HttpRequestMessage' has not been registered. To avoid this exception, either register a component to provide the service, check for service registration using IsRegistered(), or use the ResolveOptional() method to resolve an optional dependency.</p> </blockquote> <p>Using autofac 3.5 (webapi dll is quoted as autofac.webapi2 3.4).</p> <p>Any ideas much appreciated!</p> <p><strong>Notes</strong></p> <p>I'll add any findings as I come across them...</p> <p><strong>Update 1</strong></p> <p>I took a look at how <code>RegisterHttpRequestMessage</code> works and it does indeed add a message handler called <code>CurrentRequestHandler</code> to <code>HttpConfiguration</code>. When my request comes in I can see that this message handler still exists. So the method <em>seems</em> to do what it's supposed to, it's just not resolving the request message for me...</p> <p><strong>Update 2</strong></p> <p>I have noticed that while in the context of a controller and therefore have access to the <code>HttpRequestMessage</code> I can resolve both objects. Like this:</p> <pre><code>ILifetimeScope requestLifetimeScope = Request.GetDependencyScope().GetRequestLifetimeScope(); var h = requestLifetimeScope.Resolve&lt;HttpRequestMessage&gt;(); var sourceSystemViewModel = requestLifetimeScope.Resolve&lt;ISourceSystemViewModel&gt;(); </code></pre> <p><strong>Update 3</strong></p> <p>It's important to note that I am manually trying to resolve a service that is expecting the HttpMessageRequest as an injected parameter. For example, this fails for me:</p> <pre><code>using (var httpRequestScope = IocProxy.Container.Context.BeginLifetimeScope("AutofacWebRequest")) { var sourceSystemViewModel = httpRequestScope.Resolve&lt;ISourceSystemViewModel&gt;(); } </code></pre>