Igorek
Igorek

Reputation: 15850

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?

Upvotes: 4

Views: 1444

Answers (2)

Luis Ferreira
Luis Ferreira

Reputation: 23

Adding to the accepted answer of @Travis Illig, the full call should be something like this:

builder.RegisterHttpRequestMessage(System.Web.Http.GlobalConfiguration.Configuration);

Upvotes: 0

Travis Illig
Travis Illig

Reputation: 23894

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.

Upvotes: 7

Related Questions