3

given the nature of the project, I need to store a simple object (with 3/4 properties) in TempData. It is a read once write once so that's fine but does need to be passed between a few core methods/actions.

question is: How can I make it work with webfarms? What things are needed to be configured to allow TempData to work with a webfarm?

using MVC 4 Razor.

thank you

Wbmstrmjb
  • 105
  • 14
Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72

2 Answers2

1

By default, TempData is implemented using Sessions, so this would be a problem on a farm.

The easiest solution would be to use the CookieTempDataProvider

Andy T
  • 10,223
  • 5
  • 53
  • 95
  • Thanks. Sure, it is one option but what if the cookie is disabled for the user accessing the site? Plus it would have a bit of a perf hit I believe so want to try to avoid this. I was under the impression that you can make it work by making sure the machine keys are all the same on the webfarm (since this is a trick used with ASP.NET and session state) – Ahmed ilyas Sep 05 '13 at 17:42
  • If you are already using sessions out of process in some centrally located Session store, then there will be no problem. The reason to use cookies instead is if you do not want to use sessions. – Andy T Sep 05 '13 at 17:44
  • ok... so there isn't another way without using the cookie to store tempdata and make it work across a web farm? – Ahmed ilyas Sep 05 '13 at 17:48
  • It's still not clear from your response if it TempData will work when changing the Application Id to match across all web servers in the farm in IIS and also making sure the machine key is the same in all the web.config's? – Ahmed ilyas Sep 06 '13 at 08:50
1

TempData is stored in the session. This means that the only reliable way to use it in a web farm would be to have a state server of some sort.

Changing the ApplicationId (MachineKey) on all the servers to make them match does nothing for session. That only means that each server can decode the cookies left by the others. Session lives on the individual web server in memory.

If you don't have sticky sessions on your load balancer, the request that populates TempData on server 1, will likely redirect to a server different than itself and TempData will not be populated (or not with the same data that was just put in on server 1).

Wbmstrmjb
  • 105
  • 14