Consol Projesi oluşturarak aşağıdaki kodlar ile windows servis üzerinden çalışan consol uygulaması geliştirebilirsiniz.
static void Main(string[] args)
{
try
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("tr");
if (!Environment.UserInteractive)
Directory.SetCurrentDirectory(Program.GetServicePath());
else
Directory.SetCurrentDirectory(Program.GetProcessPath());
if (!Environment.UserInteractive)
{
// running as service
using (var service = new Service())
ServiceBase.Run(service);
}
else
{
// running as console app
Start(args);
Console.WriteLine("Press any key to stop...");
Console.ReadKey(true);
Stop();
}
}
catch (Exception ex)
{
log.Error(MethodBase.GetCurrentMethod().Name + " - " + ex.Message);
}
}
public static void Start(string[] args)
{
try
{
var config = new HttpSelfHostConfiguration("http://localhost:8082");
config.MapHttpAttributeRoutes();
config.MessageHandlers.Add(new CustomHeaderHandler());
var server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
log.Info("Server started....");
ServisHelpers.getSetting();
}
catch (Exception ex)
{
log.Error(MethodBase.GetCurrentMethod().Name + " - " + ex.Message);
}
}
public static void Stop()
{
// onstop code here
}