如何将 IIS Express 作为通过 Windows 服务启动的进程来运行
我正在尝试将 IIS Express 与我的应用程序一起分发。 IIS Express 将在端口 80 上为外部 Web 请求提供服务。
我在运行 IIS Express 以及为外部请求提供服务时没有任何问题,但是 Microsoft 以其无限的智慧决定从控制台窗口以及系统托盘项运行 IIS Express。您可以通过命令行参数禁用托盘项,但不能通过控制台窗口禁用。
我想在不显示控制台窗口的情况下运行 IIS Express。我还想从 Windows 服务运行 IIS Express。
从我的应用程序中运行以下代码正是我想要的:
Directory.SetCurrentDirectory(string.Format("{0}\\IIS Express", iisProgramDirectory));
process.EnableRaisingEvents = true;
//process.Exited += new EventHandler(process_Exited);
process.StartInfo.FileName = "iisexpress.exe";
process.StartInfo.Arguments = string.Format("\"/config:{0}webservice\\config\\applicationhost.config\"", dataDirectory);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//process.StartInfo.UserName = "Administrator";
//System.Security.SecureString securePwd = new System.Security.SecureString();
//string password = "**********";
//char[] pword = password.ToCharArray();
//for (int i = 0; i < pword.Length; i++)
//{
// securePwd.AppendChar(pword[i]);
//}
//process.StartInfo.Password = securePwd;
process.Start();
显然我正在以管理员身份运行。 IIS Express 显然需要以管理员权限运行才能服务外部请求并侦听端口 80。
我的 Windows 服务在 Windows 服务帐户下运行,我相信该帐户具有完全权限,但 IIS Express 进程只是正常退出,错误代码为 0当我尝试从 Windows 服务运行它时。
我已经尝试了多种方案(正如您从代码片段中看到的那样),但似乎无法使用我的 Windows 服务运行 IIS Express 并隐藏该死的控制台窗口。
任何建议将不胜感激。
I am trying to distribute IIS Express with my application. IIS Express will serve external web requests on port 80.
I have no problems running IIS Express as well as serving external requests however Microsoft in their infinite wisdom decided to run IIS Express from a console window as well as a system tray item. You can disable the tray item by a command line argument but not the console window.
I want to run IIS Express without the console window being displayed. I also want to run IIS Express from a windows service.
Running the following code from within my application does exactly what I want:
Directory.SetCurrentDirectory(string.Format("{0}\\IIS Express", iisProgramDirectory));
process.EnableRaisingEvents = true;
//process.Exited += new EventHandler(process_Exited);
process.StartInfo.FileName = "iisexpress.exe";
process.StartInfo.Arguments = string.Format("\"/config:{0}webservice\\config\\applicationhost.config\"", dataDirectory);
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
//process.StartInfo.UserName = "Administrator";
//System.Security.SecureString securePwd = new System.Security.SecureString();
//string password = "**********";
//char[] pword = password.ToCharArray();
//for (int i = 0; i < pword.Length; i++)
//{
// securePwd.AppendChar(pword[i]);
//}
//process.StartInfo.Password = securePwd;
process.Start();
Obviously I am running as Administrator. IIS Express apparently needs to run with Administrator privileges to serve external requests as well as listen on port 80.
My windows service runs under the Windows Service account which I believe has full privileges but the IIS Express process just gracefully exits with an error code of 0 when I try to run it from the windows service.
I have tried a number of scenarios (as you can see from the code snippet) but there seems to be no way I can get IIS Express running using my windows service AND hide the darn console window.
Any suggestions will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
答案如:
字符串 IIS_EXPRESS = @"C:\Program Files\IIS Express\iisexpress.exe";
应该可以,但技巧是您需要为服务的身份授予 ACL,以便它可以获取端口 80 的所有权。
换句话说,在安装程序期间(假设您有一个将运行提升的 MSI),使其运行如下命令行:
netsh http add urlacl url=http://WhateverMachineName:80/ user=everyone
您可以在其中将“所有人”限制为运行服务的特定帐户。当您这样做时,IIS Express 应该能够正常启动,而无需管理员权限。
The answer like:
string IIS_EXPRESS = @"C:\Program Files\IIS Express\iisexpress.exe";
Should work, however the trick is that you need to grant ACLs for the the identity of the service so that it can take ownership of port 80.
In other words, during your setup program (assuming you have an MSI that will run elevated), make it run a command line like:
netsh http add urlacl url=http://WhateverMachineName:80/ user=everyone
where you can limit "everyone" to instead just a specific account under which your service will be running. When you do that, then IIS express should be able to start just fine without requiring administrator privileges.
要以管理员身份运行 IIS 7.5,只需将代码稍微更改为:
这也将使您能够在端口 80 上运行站点。
To run IIS 7.5 as administrator, just change your code slightly to:
This will also enable you to run your site on port 80.
试试这个。我们遇到了同样的情况,这很有效。这可能对你有帮助。
这是使用 IIS Express 7.5,不需要管理员权限。
Try this. We had the same situation and this worked. This may help you.
This is with IIS Express 7.5 which does not need Administrator rights.
在 iis.net 论坛上也有类似的问题(在没有控制台窗口的情况下运行 iisexpress)。
请查看 http://forums.iis.net/p/1175262/ 1970513.aspx#1970513
There was a similar question (running iisexpress without console window) on iis.net forums.
Please take a look at http://forums.iis.net/p/1175262/1970513.aspx#1970513
我知道这是一篇旧文章,但是您考虑过 Microsoft 的 SRVANY Service Wrapper 吗?
它作为 Windows 服务安装和运行(在任何凭据下),并在无 Windows 进程中启动您的进程。
任何可以从命令行(或“开始/运行”窗口)运行的东西,都可以通过 SRVANY 作为服务运行:
很好的写在:
http://www.tacktech.com/display.cfm?ttid=197
I know this is an old post, but have you considered Microsoft's SRVANY Service Wrapper?
It installs and runs as a Windows Service (under any credentials), and launches your process in a windowsless process.
Anything you can run from a command line (or Start/Run window), you can run as a service via SRVANY:
Nice write-up at:
http://www.tacktech.com/display.cfm?ttid=197
如果你还想使用80端口就不行了。
It can't be done if you also want to use Port 80.