我正在创建一个我正在创建的应用程序的问题.我试图通过我的C#应用程序启动Windows服务.当我单击我的开始按钮时,看起来一切都会通过但是当我登录到服务器时,该服务仍然没有显示它正在运行.但是,第二次运行它时,我得到一个异常,说该服务的实例已在运行.当我登录服务器时,服务似乎停止了.有没有人见过这个?
这是我的代码.
try
{
while (reader.Read())
{
int timeoutMilliseconds = 1000;
string serviceName = reader["ServiceName"].ToString();
string permission = reader["Permission"].ToString();
if (permission == "E")
{
lblServStartSuccess.Visible = true;
ServiceController service = new ServiceController(serviceName);
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
else
{
lblServErrorStart.Visible = true;
}
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
Run Code Online (Sandbox Code Playgroud)
编辑:以下是我在一项服务上收到的例外情况:
System.InvalidOperationException:在计算机'.'上找不到服务逻辑磁盘管理器管理服务.---> System.ComponentModel.Win32Exception:指定的服务不作为已安装的服务存在---内部异常堆栈跟踪结束
我知道服务存在.我是否需要在服务前添加一些内容来告诉它要查看哪个服务器?
这是我的Windows/.NET安全堆栈:
在我的默认VS2008 DEV环境中,我有这个方法,它从ASP.NET应用程序调用,它工作正常:
private static void StopStartReminderService() {
ServiceController svcController = new ServiceController("eTimeSheetReminderService");
if (svcController != null) {
try {
svcController.Stop();
svcController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(10));
svcController.Start();
} catch (Exception ex) {
General.ErrorHandling.LogError(ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我在生产服务器上运行它时,我从ServiceController收到以下错误:
源:System.ServiceProcess - > System.ServiceProcess.ServiceController - > IntPtr GetServiceHandle(Int32) - > System.InvalidOperationException消息:无法在计算机'.'上打开eTimeSheetReminderService服务.
为什么会发生这种情况,我该如何解决?
编辑:
答案如下,主要是评论,但澄清:
注意:我没有通过web.config模拟,我在代码中执行.请参阅上面的MS KB文章.
我有一个由服务和可执行文件组成的应用程序.本质上,它是一个表单应用程序,负责在特定情况下启动和停止服务.
在Windows XP上,应用程序使用以下代码管理此项:
ServiceController controller = new ServiceController();
controller.MachineName = ".";
controller.ServiceName = "XXXXXXXXXX";
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 10));
controller.Start();
Run Code Online (Sandbox Code Playgroud)
但是在Windows 7上,即使我以管理员身份启动了应用程序,我也会遇到以下异常:
System.InvalidOperationException: Cannot open XXXXXXXXXXXXX service on computer '.'. ---> System.ComponentModel.Win32Exception: Access is denied
--- End of inner exception stack trace ---
at System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)
at System.ServiceProcess.ServiceController.Start(String[] args)
at System.ServiceProcess.ServiceController.Start()
Run Code Online (Sandbox Code Playgroud)
有什么我可以通过编程方式解决这个问题吗?
我正在尝试将已安装的 Windows服务设置为C#中的自动延迟启动.如何设置Windows服务
Automatic (Delayed Start)
Run Code Online (Sandbox Code Playgroud)
无法在ServiceStartMode枚举中找到该值.
编辑:1
public class ServiceAutoStartInfo
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private struct SERVICE_DELAYED_AUTO_START_INFO
{
public bool fDelayedAutostart;
}
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ChangeServiceConfig2(IntPtr hService, int dwInfoLevel, IntPtr lpInfo);
// Service configuration parameter
const int SERVICE_CONFIG_DELAYED_AUTO_START_INFO = 3;
public bool ChangeDelayedAutoStart(IntPtr hService, bool delayed)
{
// Validate service handle
if (hService != IntPtr.Zero)
{
// Create
SERVICE_DELAYED_AUTO_START_INFO info = new SERVICE_DELAYED_AUTO_START_INFO();
// Set the DelayedAutostart property …
Run Code Online (Sandbox Code Playgroud) 类似于/sf/answers/32729581/(该示例显示了如何在远程计算机上启动和停止现有Windows服务),我正在尝试在远程计算机上安装Windows Service。我可以很好地启动和停止远程计算机上的服务,但是我无法找到如何远程安装新服务的方法,而无需诉诸sc.exe进程(/sf/answers/81134161/),如果可能的话,我想避免这种情况。
我知道有一个System.Configuration.Install.ManagedInstallerClass.InstallHelper方法,但是我无法将此方法安装到远程计算机上。而且,我无法确定是否可以使用System.ServiceProcess.ServiceInstaller或System.ServiceProcess.ServiceProcessInstaller。
我正在尝试使用C#启动服务但是却抛出以下错误:
ServiceController service = new ServiceController(sServiceName);
service.MachineName = localComputerName;
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running);
Run Code Online (Sandbox Code Playgroud)
System.InvalidOperationException:无法在计算机上打开服务System.ComponentModel.Win32Exception:在System.ServiceProcess.ServiceController.Stop()的System.ServiceProcess.ServiceController.GetServiceHandle(Int32 desiredAccess)中拒绝访问