Search code examples
asp.net-web-apiautofac

Autofac with Web API: inject dependencies from HttpRequestMessage?


I have a need to inject certain values from the HttpRequestMessage (mostly revolving around identity) into some of my objects.

As I understand it, using HttpSession.Current is not recommended in the Web Api framework. How do I access the HttpRequestMessage for user's Identity and other attributes such as user's IP address, UserAgent, etc. during binding of my dependencies in Autofac?


Solution

  • You can use builder.RegisterHttpRequestMessage() while you're building your dependency container. After that, anything that takes an HttpRequestMessage as a dependency will get the current message.