【问题标题】:Executing Windows Service (*.exe) from Asp.net Web Application从 Asp.net Web 应用程序执行 Windows 服务 (*.exe)
【发布时间】:2019-01-22 15:50:23
【问题描述】:

我已经在我的系统中创建了 Windows 服务并安装了 .exe 文件。我可以在 Windows 10 上通过Service 控件运行此服务。

我正在从 ASP.NET Web 应用程序运行相同的服务。服务已启动,但我想在从 Web 应用程序调用时调用 protected override void OnCustomCommand(int command) 方法。

我收到错误

An exception of type 'System.InvalidOperationException' occurred in System.ServiceProcess.dll but was not handled in user code

Additional information: Cannot control ServiceName service on computer '.'.

网页上调用windows服务的代码如下-

            ServiceController sc = new ServiceController("ServiceName");
            if (sc.Status != ServiceControllerStatus.Running && sc.Status != ServiceControllerStatus.StopPending)
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(60000);
                sc.Start();
                sc.ExecuteCommand(CommandId);
                sc.WaitForStatus(ServiceControllerStatus.Running, timeout);
            }

谁能帮忙找出这个问题的原因?

【问题讨论】:

  • 您在该服务调用中的计算机名称似乎有问题,或者可能是权限问题。
  • 你检查过these things吗?
  • @RobertHarvey 是的,看起来是这样。但我不明白如何给出计算机名称或如何授予权限。另一件事是,如果是权限问题,那么如何在没有任何权限的情况下启动服务。
  • 看起来你真的应该使用总线消息或 Web API 或 Nancy 或其他东西与你的服务进行通信,而不是通过 ServiceController。

标签: c# asp.net windows-services


【解决方案1】:

运行网页的代码没有管理员权限来控制该机器上的服务。

您必须将应用程序池配置为在管理帐户下运行。从安全的角度来看,这可能不是一个好主意。

如果您不想以管理权限运行您的网络应用程序,您可以考虑在您的 Windows 服务中托管您自己的侦听器,该侦听器只需要用户名/密码,或者可能根本不需要身份验证,因为您选择的变体仅可用于本地主机。 WCF 可能是一个不错的选择,如果您更熟悉的话,可能是 ASP.NET。

【讨论】:

  • 我不推荐 WCF,除非您使用 SOAP(这实际上可能是一种合理的方法)。
  • 我在想命名管道,因为它只是本地机器。但是任何连接两个进程的技术都应该有效。
猜你喜欢
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-01
相关资源
最近更新 更多