Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

c# - Programmatically determine if code is running under IIS Express

I am not sure this is even possible, but I am hoping for a clue as to determine if the code that is currently executing is running under IIS Express. My best approximation so far, which is incredibly hackish and will certainly fail/break at some point:

bool IsExpress = 
  Request.ServerVariables["SERVER_SOFTWARE"] == "Microsoft-IIS/7.5"
  && Int32.Parse(Request.ServerVariables["INSTANCE_ID"]) > 1000000000;

Certainly there has to be a better way. My examination of the Application, Server and Request objects didn't seem to reveal anything that might provide better insight. Perhaps I just need to look at some other object?

Update:

I am really curious if there is a way to detect this - it is really academic at this point I don't have a burning need to use it. The original question stands. But in the spirit of responding to the comments, specifically I am interested in answering a criticism from another question/answer on this site: How to search the server's MIME map. The criticism is that the posted answer does not work for IIS Express, only traditional IIS instances. IIS Express stores the MIME configuration in the applicationhost.config XML file and I would like to update that answer to provide a way to return that information for IIS Express as well. I could certainly just add some code that grabs the appropriate value from the XML (Yay for LINQ to XML!) but I would really like to make it smarter. To be clear, I don't need help parsing that file - just something more elegant in trying to detect if code is currently executing in the IIS Express engine.

Update 2:

IIS 8.0 Express Beta was released this week, and it further goes to show that the approach in my question is brittle and will break. While it isn't a deal breaker to target a specific version, it would be nice to account for that and try to ensure the code will work with at least the known versions today.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Would checking the current process name do the trick?

bool isExpress = 
  String.Compare(Process.GetCurrentProcess().ProcessName,"iisexpress") == 0;

Normal IIS runs under w3wp.exe from memory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...