Я получаю ошибку, которая не может запустить службу из командной строки или debbuger
windows service should be first install and then started with server explorer,administrative tools or the net start command
это ошибка, с которой я столкнулся. Я попытался запустить службу из computer management-service и application, запустив cmd через права администратора.
ниже я прикрепляю свой код.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; using System.Timers; using System.Windows; namespace WindowsService1 { 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; } } }
библиотечные классы
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace WindowsService1 { public static class Library { 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 { } } } }
программы.в CS
using System; using System.Collections.Generic; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace WindowsService1 { static class Program { /// <summary> /// The main entry point for the application. /// </summary> static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); } } }
Что я уже пробовал:
I have tried starting the service from computer management - service and application, running cmd through admin rights.
[no name]
служба windows должна быть сначала установлена, а затем запущена с помощью проводника сервера, средств администрирования или команды net start