8

HttpContext.Current.Request.IsLocal is not available in Global.Asax/Application_Start (Request is not available in the context).

How else could I safely determine if my ASP.NET MVC application is started locally or not?

This is to rewrite my web.config conditionally (depending on whether the application is deployed (remote) or in testing (local)).

Thank you!

Alex
  • 75,813
  • 86
  • 255
  • 348
  • 1
    Which function in the Global.asax? I believe that different items are available based on the context. I am fairly sure that the HttpContext.Current.Request is available in the Application_Error, but not in others... – David May 21 '10 at 22:02
  • Can you work with it in the Application.BeginRequest? It's availabe there.... – David May 21 '10 at 22:06
  • 1
    No. This is for a one-time operation on a config file. Application_Start gets executed once (as desired) on application startup, BeginRequest with every request to the application (very bad place to put a one-time operation). – Alex May 21 '10 at 22:07
  • Good point. Just thinking off the top of my head. – David May 21 '10 at 22:13
  • Interestingly enough, HttpContext.Current.Request.IsLocal works in Global.Asax when I execute the application locally through VS, but doesn't work when it's deployed on a remote server. – Alex May 21 '10 at 22:20
  • Weird, I guess this maybe to do with the fact that Cassini (or whatever its called now) starts when you request to view a file in the browser(via F5). I assume that as it starts with all the information required to build the request it does this at before triggering Application_Start. Just a guess though :-) – Owen May 21 '10 at 23:15
  • If by "rewriting ```web.config```" you mean that you would modify ```web.config``` on filesystem, that's really not good idea, because that will trigger application pool restart and you'd be doing that infinitely. – the berserker Oct 30 '15 at 17:06
  • I face a similar problem in bootstrapping a background library (Hangfire) that has to be done once before any requests happen. I don't know what the answer is, but for this case where you're rewriting the web.config I would not put that in the application. That's a deploy type of things (just like any web.config transform) and should be part of your deployment process. You mention elsewhere it should be automated and that's fine so use TeamCity or Octopus to do this. Rewriting the web.config inside the app might cause an recursive overflow. – Bil Simser Feb 14 '17 at 13:59

2 Answers2

1

The Application_Start event will be fired when IIS/cassini/whatever loads up your app (way before any HTTP requests have been made).

Reading your comments you want this to be a "one time operation" which really makes no sense. Your application is not so much "started locally" but it may be requested locally and/or remotely several times throughout its life cycle. With this in mind you need to check on each request as David commented.

Maybe, it would be better if you explained a little more what you are trying to achieve?

Owen
  • 6,992
  • 7
  • 44
  • 77
  • 1
    Owen - I added an explanation in my comments (one-time operation on a config file). I'm rewriting parts of the web.config conditionally if the application is starting remotely (production) or locally. So it absolutely "makes sense". BeginRequest is certainly the wrong place for this. – Alex May 21 '10 at 22:24
  • For things like this, you're better using a build script tool like nant, or rake to that pops in the correct values in your web config at build time. But this would depend on the complexity of your deployment. – Owen May 21 '10 at 22:48
  • Alternatively, you could check the machine name that the application is running under and load appropriate configuration depending on this? – Owen May 21 '10 at 22:50
  • I don't like this either as I'm trying to automate this fully. Once I start checking against a hardcoded value (e.g. computer name, flag etc.) then I lose that advantage. – Alex May 22 '10 at 07:57
  • 1
    Alex - when the app starts, there is no concept of local or remote as there is no request yet. It sounds strange, but you could have a static bool _isInitialized, and on BeginRequest, check the flag and perform your config rewrite and set the flag, so it doesn't happen anymore. However after this first request, if it's a local request, any remote requests to the server will be operating in "local" mode. – Dave Thieben Jun 30 '10 at 19:59
  • @dave thebin, This could cause a load of issues, if you deploy your app and remote in and hit it via localhost then it will be set to 'local mode'. Only real options are check per request, automate build before deploy, or check machine name or other fairly constant env value. Personally, I would make this a deployment step, but thats just me. – Owen Jul 03 '10 at 12:39
  • @Owen, I agree, but that's not what he's asking for. – Dave Thieben Jul 03 '10 at 15:37
0

It might be more appropriate to check this in the BeginRequest method instead of the Application_Start because the first request might be local but later you could call the application on some other domain and it will no longer be local.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • No - see my question and comments. I want the configuration updated executed exactly once when the application is started up. – Alex May 22 '10 at 17:42