Topshelf service automatically start after reboot

115 views
Skip to first unread message

Amjad Hussain

unread,
Jun 25, 2017, 7:03:08 PM6/25/17
to topshelf-discuss
I have following code which runs perfect after installing the service but problem is it is not starting automatically after reboot. Any suggestions for this?

using Quartz;
using System;
using System.IO;
using Topshelf;
using Topshelf.Quartz;


namespace FinyaConsole
{
   
class Program
   
{
       
static void Main(string[] args)
       
{
           
HostFactory.Run(x =>
           
{
                x
.Service<MyService>(s =>
               
{
                    s
.WhenStarted(service => service.OnStart());
                    s
.WhenStopped(service => service.OnStop());
                    s
.ConstructUsing(() => new MyService());


                    s
.ScheduleQuartzJob(q =>
                        q
.WithJob(() =>
                           
JobBuilder.Create<MyJob>().Build())
                           
.AddTrigger(() => TriggerBuilder.Create()
                               
.WithSimpleSchedule(b => b
                                   
.WithIntervalInSeconds(10)
                                   
.RepeatForever())
                               
.Build()));
               
});


                x
.RunAsLocalSystem()
                   
.DependsOnEventLog()
                   
.StartAutomatically()
                   
.EnableServiceRecovery(rc => rc.RestartService(1));


                x
.SetServiceName("FinyaHearts");
                x
.SetDisplayName("FinyaGiveHearts");
                x
.SetDescription("This service is used to give hearts to the users after three days to whom have visited your profiles.");
           
});
       
}




   
}


   
public class MyService
   
{
       
public void OnStart()
       
{
       
}


       
public void OnStop()
       
{
       
}
   
}


   
public class MyJob : IJob
   
{
       
public void Execute(IJobExecutionContext context)
       
{
           
try
           
{
               
//Pass the filepath and filename to the StreamWriter Constructor
               
StreamWriter sw = new StreamWriter(".\\visits_to_others.txt", true);


               
//Write a line of text
                sw
.WriteLine($"[{DateTime.Now}] Welcome from MyJob!");
               
//System.IO.File.WriteAllText(@"path\visit_users.txt", userLink);
               
//Close the file
                sw
.Flush();
                sw
.Close();
           
}
           
catch (Exception e)
           
{
               
Console.WriteLine("Exception: " + e.Message);
           
}
           
finally
           
{
               
//Console.WriteLine("Executing finally block.");
           
}
       
}
   
}
}

Chris Patterson

unread,
Jun 26, 2017, 9:52:18 AM6/26/17
to topshelf...@googlegroups.com
Is Quartz using SQL Server or something that isn't available when the service is trying to start? If it starts normally once the machine is already up and running, it's likely to be a startup dependency that isn't ready.


--
You received this message because you are subscribed to the Google Groups "topshelf-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to topshelf-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages