如何将 IIS Express 作为通过 Windows 服务启动的进程来运行

发布于 2024-10-16 12:47:15 字数 1609 浏览 3 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

深府石板幽径 2024-10-23 12:47:15

答案如:
字符串 IIS_EXPRESS = @"C:\Program Files\IIS Express\iisexpress.exe";

    StringBuilder arguments = new StringBuilder();
    arguments.Append(@"/path:");
    arguments.Append(@"C:\Inetpub\wwwroot\ClientSyncService");
    arguments.Append(@" /Port:2000");
    Process process = Process.Start(new ProcessStartInfo()
        {
            FileName = IIS_EXPRESS,
            Arguments = arguments.ToString(),
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        });

应该可以,但技巧是您需要为服务的身份授予 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";

    StringBuilder arguments = new StringBuilder();
    arguments.Append(@"/path:");
    arguments.Append(@"C:\Inetpub\wwwroot\ClientSyncService");
    arguments.Append(@" /Port:2000");
    Process process = Process.Start(new ProcessStartInfo()
        {
            FileName = IIS_EXPRESS,
            Arguments = arguments.ToString(),
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        });

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.

纸短情长 2024-10-23 12:47:15

要以管理员身份运行 IIS 7.5,只需将代码稍微更改为:

Process process = Process.Start(new ProcessStartInfo()
{
    FileName = IIS_EXPRESS,
    Arguments = arguments.ToString(),
    RedirectStandardOutput = true,
    UseShellExecute = true,
    CreateNoWindow = true,
    Verb = "runas"
});

这也将使您能够在端口 80 上运行站点。

To run IIS 7.5 as administrator, just change your code slightly to:

Process process = Process.Start(new ProcessStartInfo()
{
    FileName = IIS_EXPRESS,
    Arguments = arguments.ToString(),
    RedirectStandardOutput = true,
    UseShellExecute = true,
    CreateNoWindow = true,
    Verb = "runas"
});

This will also enable you to run your site on port 80.

荒岛晴空 2024-10-23 12:47:15

试试这个。我们遇到了同样的情况,这很有效。这可能对你有帮助。

这是使用 IIS Express 7.5,不需要管理员权限。

string IIS_EXPRESS = @"C:\Program Files\IIS Express\iisexpress.exe";

StringBuilder arguments = new StringBuilder();
arguments.Append(@"/path:");
arguments.Append(@"C:\Inetpub\wwwroot\ClientSyncService");
arguments.Append(@" /Port:2000");
Process process = Process.Start(new ProcessStartInfo()
{
    FileName = IIS_EXPRESS,
    Arguments = arguments.ToString(),
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true
});

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.

string IIS_EXPRESS = @"C:\Program Files\IIS Express\iisexpress.exe";

StringBuilder arguments = new StringBuilder();
arguments.Append(@"/path:");
arguments.Append(@"C:\Inetpub\wwwroot\ClientSyncService");
arguments.Append(@" /Port:2000");
Process process = Process.Start(new ProcessStartInfo()
{
    FileName = IIS_EXPRESS,
    Arguments = arguments.ToString(),
    RedirectStandardOutput = true,
    UseShellExecute = false,
    CreateNoWindow = true
});
强辩 2024-10-23 12:47:15

在 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

满地尘埃落定 2024-10-23 12:47:15

我知道这是一篇旧文章,但是您考虑过 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

情独悲 2024-10-23 12:47:15

如果你还想使用80端口就不行了。

It can't be done if you also want to use Port 80.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文