以编程方式确定是否代码是在IIS运行快车 [英] Programmatically determine if code is running under IIS Express

查看:104
本文介绍了以编程方式确定是否代码是在IIS运行快车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这甚至有可能,但我希望有线索,以确定是否当前正在执行的代码是在IIS快递运转。我最好的逼近,到目前为止,这是令人难以置信的hackish,肯定会失败/中断在一些点:

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?

更新:

我如果有检测到这种方式真的很好奇 - 这是在这一点上确实学术我没有燃烧需要使用它。原来的问题的立场。但在应对意见的精神,特别是我感兴趣的是另一个问题/答案在本网站回答批评:的如何搜索服务器的MIME地图。有人批评说,张贴答案不会为IIS Express中,只有传统的IIS实例工作。 IIS快速存储对ApplicationHost.config XML文件中的MIME配置,我想以更新的答案提供了一个方法,返回信息为IIS快递为好。我当然可以只添加一些代码,从XML抓住适当的值(耶对LINQ到XML!),但我真的想使它更聪明。需要明确的是,我不需要帮助解析该文件 - 只是一些试图如果代码当前在IIS Express引擎执行的检测更优雅。

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.

更新2:

IIS 8.0快递测试版的released本周,它进一步表明一点,在我的问题的做法是脆,将打破。虽然它不是一个大忌瞄准特定的版本,这将是很好考虑到这一点,尽量保证代码将与至少已知版本今天的工作。

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.

推荐答案

应该检查当前进程名做的伎俩?

Would checking the current process name do the trick?

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



普通IIS下的w3wp.exe 从运行内存。

这篇关于以编程方式确定是否代码是在IIS运行快车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆