我在网络上进行了很多搜索,但是找不到适合我的解决方案。
我已经用C#开发了Windows服务。当我将其安装在桌面上时,它可以正常工作。我可以停止,启动或重新启动它,它会执行所有应做的事情。我当前的台式机正在使用Windows 10 1909。
当我在Windows Server 2016版本1607上安装此发行版时,服务启动时会出现以下错误:
我不知道我的OnStart-Function必须更改什么。
public partial class Webshopfiletransfer : ServiceBase
{
private static System.Timers.Timer aTimer;
private static string logfile = Directory.GetCurrentDirectory() + @"\Webshopfiletransferslog-" + DateTime.Today.ToString("yyyy-MM-dd") + ".txt";
public Webshopfiletransfer()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
writetolog("Service started");
try
{
SetTimer();
aTimer.Start();
}
catch (Exception e)
{
writetolog("Error: " + e.Message);
}
}
protected override void OnStop()
{
try
{
aTimer.Stop();
aTimer.Dispose();
}
catch (Exception e)
{
writetolog("Error: " + e.Message);
}
}
private static void SetTimer()
{
// Create a timer with a two second interval.
aTimer = new System.Timers.Timer(30000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
transferfiles("download");
transferfiles("upload");
if (!File.Exists(logfile))
{
string createText = "Logfile created" + Environment.NewLine;
File.WriteAllText(logfile, createText);
}
}
private static void transferfiles(string modus)
{
//This function works when the service is started
}
private static void writetolog(string messagetext)
{
string appendText = DateTime.Now.ToLongTimeString() + ": " + messagetext + Environment.NewLine;
try
{
File.AppendAllText(logfile, appendText);
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
}
}
谢谢你的小费。