Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store global per request data in a .NET Web API project

I am working on writing a web api for my application that is currently written using ASP.NET Web Forms. I have a module that acquires some data at the beginning of the request and stores it in HttpContext.Current.Items so that it is available to all code later during the page processing.

I am trying to write my web api and it needs to do the same thing. What is the correct way to store "per request" global data to be used during processing for a web api controller (I suspect it would be the same for a regular controller as well).

Also, if there is a way to do so, is there a way to set this data in an IHttpModule that runs prior to the controller being instantiated.

Any help is appreciated!

like image 773
Leslie Hanks Avatar asked Sep 09 '13 01:09

Leslie Hanks


People also ask

What is HttpConfiguration in Web API?

The HttpConfiguration is the main class which includes following properties using which you can override the default behaviour of Web API. Property. Description. DependencyResolver. Gets or sets the dependency resolver for dependency injection.

How many requests per second can ASP.NET Core handle?

7+ Million HTTP requests per second from a single server.


1 Answers

You can use the HttpRequestMessage.Properties dictionary as per this StackOverflow question.

There are other options like HttpContext or Session, but they can't be used in a self-hosting setup, only in WebHosting mode.

like image 163
Simon C Avatar answered Oct 12 '22 18:10

Simon C