Service is not working...(It works sometimes while debugging...i used the following way to debug
#if (!DEBUG)
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new PollingService() };
ServiceBase.Run(ServicesToRun);
#else
// Debug code: this allows the process to run as a non-service.
MyService service = new MyServiceService();
service.OnStart(null);
//Use this to make the service keep running
// Shut down the debugger to exit
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
//Use this to make it stop
//System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10));
//service.OnStop();
#endif
#if (!DEBUG)
protected override void OnStart(string[] args)
#else
public new void OnStart(string[] args)
#endif
)[My framework did not allow me to attach a process for debugging ]
Steps followed while creating window service..
http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx
Then
added config and references coz i was linking the service with a
website code.The connection string of the service and webite is the
same.
Service1.cs code
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.Collections;
using System.Configuration;
......References from the website.....
using System.Timers;
using
System.Xml;
using System.IO;
using System.Web;
using
System.Globalization;
namespace ExactService
{
public
partial class Service1 : ServiceBase
{
public
Service1()
{
InitializeComponent();
//code to set timer
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("ExactServiceLog"))
System.Diagnostics.EventLog.CreateEventSource("ExactServiceLog",
"ESLOG");
eventLog1.Source = "ExactServiceLog";
// the event log source by which
//the
application is registered on the computer
eventLog1.Log = "ESLOG";
//code ends
}
private System.Timers.Timer MyTimer;
protected override void OnStart(string[] args)
{
// Interval in milliseconds
string myInterval =
System.Configuration.ConfigurationManager.AppSettings["ServiceInterval"].ToString();
double interval = Convert.ToDouble(myInterval);
//
Create timer object
MyTimer = new
System.Timers.Timer(interval);
MyTimer = new
System.Timers.Timer();
// Set to run more than once
MyTimer.AutoReset = true;
MyTimer.Enabled = true;
// Delegate to handle the timer event
MyTimer.Elapsed +=
new ElapsedEventHandler(timer_Elapsed);
// Start the
timer
MyTimer.Start();
// Prevents
garbage collection from occuring in long running methods
GC.KeepAlive(MyTimer);
// TODO: Add code here to start
your service.
//code for date check
eventLog1.WriteEntry("ExactService started");
}
/// <summary>
///
/// </summary>
private void RunService()
{
string
fileName = string.Format("customers_{0}.xml",
DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"));
CustomerCollection customers = GetCustomers();
string xml
= ExactXMLFormatter.ExportCustomersToXML(customers);
WriteResponseXML(xml, fileName);
eventLog1.WriteEntry("Xml File written.");
}
public static void WriteResponseXML(string xml, string Filename)
{
if (!String.IsNullOrEmpty(xml))
{
String strFilePath =
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
+ "/" + Filename;
XmlDocument document = new
XmlDocument();
document.LoadXml(xml);
document.Save(strFilePath);
}
}
/// <summary>
///
///
</summary>
/// <param
name="sender"></param>
/// <param
name="e"></param>
private void timer_Elapsed(object
sender, ElapsedEventArgs e)
{
try
{
MyTimer.Enabled = false;
MyTimer.Stop();
string startTimeString =
ConfigurationSettings.AppSettings["StartTime"].ToString();
DateTime startTime;
startTime = DateTime.Now;
//If timestamp = myDateTime.ToString("HH:mm")
if(startTimeString==startTime.ToString("HH:mm"))
{
RunService();
}
}
catch (Exception ex)
{
System.IO.File.AppendAllText("C:\\CustomerList.txt", "Exact service
threw Exception " + ex.Message + "\r\n");
}
finally
{
GC.Collect();
MyTimer.Start();
}
}
protected
CustomerCollection GetCustomers()
{
//System.IO.File.AppendAllText("C:\\CustomerList.txt", "File is created
at the follwing location: Customers started.\r\n");
string customerType = string.Empty;
if (customerType ==
string.Empty)
{
customerType =
"Admin,Office,Customer";
}
//System.IO.File.AppendAllText("C:\\CustomerList.txt", "File is created
at the follwing location: Customers 2nd step.\r\n");
DateTime startDate = DateTime.Now;
DateTime endDate =
DateTime.Now;
CustomerCollection customers = null;
////CustomerID Inserted
//System.IO.File.AppendAllText("C:\\CustomerList.txt", "File is created
at the follwing location: Customers 3rd step.\r\n");
//System.IO.File.AppendAllText("C:\\CustomerList.txt", "File is created
at the follwing location: Customers 4th step." + startDate + ", " +
endDate + ", " + int.MaxValue + ", " + customerType + "\r\n");
int totalRecords = 0;
customers =
CustomerManager.GetAllCustomersByDate(startDate,
endDate, "", "", 2147483, 0, out totalRecords, customerType);
//System.IO.File.AppendAllText("C:\\CustomerList.txt", "File is created
at the follwing location: Customers fetched.\r\n");
return customers;
}
protected override void OnStop()
{
// TODO:
Add code here to perform any tear-down necessary to stop your service.
eventLog1.WriteEntry("Exact Service stopped");
MyTimer.AutoReset = false;
MyTimer.Enabled = false;
}
}
}
app.config
<appSettings>
<add key="StartTime" value="2355" />
<add key="ServiceInterval" value="60000" />
</appSettings>