If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Beginner:c# windows service using webDav

Hi All,

Please i need help with the code below. Am trying to create a windows service that deletes emails older than 4 months. Please help me check the DeleteEmail method, the WebDav query in the method and also the ReadEmails method.

Re: Beginner:c# windows service using webDav

Really, the way to figure this problem out is to step through the code in a debugger.

Of course, since the code is inside a Windows Service, it isn't all that easy to debug.

What I do to all my Windows Services is set them up so I can debug them easily.

Namely, I add code that detects whether they are running in a console window or as a service.

If they are running as a console (non-service), I simply call the service Start() method; otherwise,
I call the servicebase Run method.

Here's what you do to use this technique:
1) In the project properties, change the Output Type from "Windows Application" to "Console Application". This allows you to 'print' out any trace statements with Console.WriteLine.
2) Add the following code to the ServiceBase derived class to detect whether running under a console window.

3) Add a static method to the ServiceBase derived class. This method runs the code in a console window or as a service.

Code:

/// <summary>
/// Call this method to Start the service
/// </summary>
public static void Start(string applicationName)
{
// If not running as a Windows service, manually start the service
if IsRunningFromConsole())
{
try
{
Console.WriteLine( String.Format("Starting the {0}...", applicationName) );
// Start the service in the console
new MyService().OnStart(null);
Console.WriteLine("Service has been started!");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
}
else
{
// Run as a Windows Service
Run(new ServiceBase[] { new MyService() });
}
}

4) Change the program main to call the static MyService.Start() method.