views:

86

answers:

2

Hello,

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!

+1  A: 

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
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
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
Alternatively, you could check the machine name that the application is running under and load appropriate configuration depending on this?
Owen
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
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
@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
@Owen, I agree, but that's not what he's asking for.
dave thieben
A: 

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
No - see my question and comments. I want the configuration updated executed exactly once when the application is started up.
Alex