【发布时间】: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