【发布时间】:2017-08-08 20:25:54
【问题描述】:
当我调试我的代码时,它会弹出一个对话框,提示 Windows 服务启动失败 - 无法从命令行和调试器启动服务。必须先安装 Windows 服务(使用 installutil.exe),然后使用服务器资源管理器、Windows 服务管理工具或 net start 命令启动。
但是我已经安装了我的服务。以下是具有管理员权限的 cmd 脚本。
C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsSe
rvice1\bin\Debug>installutil WindowsService1.exe
Microsoft (R) .NET Framework Installation utility Version 4.0.30319.33440
Copyright (C) Microsoft Corporation. All rights reserved.
Running a transacted installation.
Beginning the Install phase of the installation.
See the contents of the log file for the C:\Users\Sapuser\Documents\Visual Studi
o 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe as
sembly's progress.
The file is located at C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Wi
ndowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog.
Installing assembly 'C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Wind
owsService1\WindowsService1\bin\Debug\WindowsService1.exe'.
Affected parameters are:
logtoconsole =
logfile = C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\WindowsServi
ce1\WindowsService1\bin\Debug\WindowsService1.InstallLog
assemblypath = C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Windows
Service1\WindowsService1\bin\Debug\WindowsService1.exe
Installing service MyTestWinService...
Service MyTestWinService has been successfully installed.
Creating EventLog source MyTestWinService in log Application...
The Install phase completed successfully, and the Commit phase is beginning.
See the contents of the log file for the C:\Users\Sapuser\Documents\Visual Studi
o 2013\Projects\WindowsService1\WindowsService1\bin\Debug\WindowsService1.exe as
sembly's progress.
The file is located at C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Wi
ndowsService1\WindowsService1\bin\Debug\WindowsService1.InstallLog.
Committing assembly 'C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Wind
owsService1\WindowsService1\bin\Debug\WindowsService1.exe'.
Affected parameters are:
logtoconsole =
logfile = C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\WindowsServi
ce1\WindowsService1\bin\Debug\WindowsService1.InstallLog
assemblypath = C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\Windows
Service1\WindowsService1\bin\Debug\WindowsService1.exe
The Commit phase completed successfully.
The transacted install has completed.
C:\Users\Sapuser\Documents\Visual Studio 2013\Projects\WindowsService1\WindowsSe
rvice1\bin\Debug>net start MyTestWinService
The MyTestWinService service is starting.
The MyTestWinService service was started successfully.
我附上我的参考代码:
public partial class Service1 : ServiceBase
{
private Timer timer1 = null;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer1 = new Timer();
this.timer1.Interval = 60000; //60 sec
this.timer1.Elapsed +=new System.Timers.ElapsedEventHandler(this.timer1_Tick);
timer1.Enabled=true;
Library.WriteErrorLog("test windows service started");
}
protected override void OnStop()
{
timer1.Enabled = false;
Library.WriteErrorLog("Test Service ended");
}
public void timer1_Tick(object sender, ElapsedEventArgs e)
{
//job
var result = RunProcess(@"c:\", "webupknvp.Bat", "", false);
if (result == 0)
{
// success
Console.WriteLine("Sucess");
}
else
{
// failed ErrorLevel / app ExitCode
Console.WriteLine("failed try again");
}
}
public int RunProcess(string workDir, string appName, string args, bool hide = false)
{
Process proc = null;
proc = new Process();
string batrun = string.Format("cmd.exe", "/c" + @"C:\Abhay\batfile"); // or @"C:\Abhay\batfile" in the end ("cmd.exe", "/c" + @"C:\Abhay\batfile")
proc.StartInfo.UseShellExecute = false; //addition
proc.StartInfo.WorkingDirectory = workDir;//batrun
proc.StartInfo.FileName = appName;
proc.StartInfo.Arguments = args;
proc.StartInfo.CreateNoWindow = hide;
proc.Start();
proc.WaitForExit();
return proc.ExitCode;
}
}
}
库类
public static void WriteErrorLog(Exception ex)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\ Logfile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + ":" + ex.Source.ToString().Trim() + ";" + ex.Message.ToString().Trim());
sw.Flush();
sw.Close();
}
catch
{
}
}
public static void WriteErrorLog(string Message)
{
StreamWriter sw = null;
try
{
sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\ Logfile.txt", true);
sw.WriteLine(DateTime.Now.ToString() + ":" + Message);
sw.Flush();
sw.Close();
}
catch
{
}
}
}
}
Program.cs
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
我应该改变什么?
【问题讨论】:
-
“调试我的代码”是指来自 Visual Studios 的吗?您无法从 Visual Studios 运行服务来调试它。根据您发布的内容,您确实安装了该服务并且它正在运行(您应该在“服务”窗口中看到它)。但是,那是安装的版本; VS 仍然无法通过 IDE 运行服务。
-
@Lithium 是的,我可以在服务窗口中看到该服务,并且它也可以通过 admin cmd 的 net start 启动。另一方面,即使服务是从 net start 开始的,但该服务并未完成作业,即运行批处理文件。
标签: c# windows-services