user1600279
user1600279

Reputation:

Making a windows service from a console application

Is it possible to make a windows service from a console application. In fact, i made a console application that sends emails to persons from a database but when I tried to make a service with almost the same code it didn't work. After installing it emails aren't nomore sent. So, I want to transform my console application into a service if there is a way because I want to send them automatically and I don't want to use task sheduler.

Here is my console application main

     {
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(ConfigurationManager.AppSettings["email"]);
        mail.Subject = "Rappel délai tâche";
        SmtpClient client = new SmtpClient(ConfigurationManager.AppSettings["domaine"]);
        client.EnableSsl = true;

        client.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["email"], ConfigurationManager.AppSettings["password"]); 

        BDGestionEntities bd = new BDGestionEntities();
        TimeSpan diff;
        DateTime aujourdhui = DateTime.Today;
        List <tache> taches = bd.taches.ToList();

        foreach (var k in taches)
        {
            Console.WriteLine(k.nom_tache);
            diff = k.date_fin.Subtract(aujourdhui);
            int datediff = Convert.ToInt32(diff.TotalDays);
            if (datediff <= 2)
            {



                mail.To.Add(k.utilisateur.email);

                mail.Body = "Bonjour, " + k.utilisateur.nom + " " + k.utilisateur.prenom +
                    "\n\nNous vous envoyons le présent mail pour vous rappeler que la tâche \"" + k.nom_tache + "\" qui vous est accordée touchera à sa fin d'ici deux jours.\nVeuillez respecter le délai. \n\n Bien cordialement.";
                try
                {
                    client.Send(mail);
                    Console.WriteLine("Email envoyé");
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                }
            }
        }
    }

In fact I'm using a model with ado .net inorder to access to my database

Upvotes: 2

Views: 4187

Answers (5)

user1600279
user1600279

Reputation:

I found the solution for that. In fact, it was just a connection problem and all I had to do was to add permissions to the service in order to let it access the database. Thank you everyone.

Upvotes: 1

Jacob Seleznev
Jacob Seleznev

Reputation: 8131

The Windows 2003 Server Resource Kit provides two utilities that allow you to create a Windows user-defined service for Windows applications.

Instrsrv.exe installs and removes system services and Srvany.exe allows any Windows application to run as a service.

This Microsoft Support article shows how.

Upvotes: 0

BugFinder
BugFinder

Reputation: 17858

The basic code will work, you just need to move it into the service portion - however it will probably crash at the console write lines, because services by default dont have access to screen, and arent allocated a console.

If you use visual studio it will template you a service. But, you can do pretty much the same work - I actually changed my service so I can run it from command line if it had the parameter /console it allocated a console so it could use it and I can debug it etc.

Upvotes: 1

Robbie Dee
Robbie Dee

Reputation: 1977

Rather than try and convert it straight off, I'd try writing a very simple service first to get a feel for how it works. Maybe something simple like writing the date and time to a file every 5 minutes. You could then try adding your code to the service body.

Obviously you'll want to avoid anything that writes to the screen. This should be written to either a log or the event viewer.

Upvotes: 1

WouterH
WouterH

Reputation: 1356

Windows services have a bit more to them than console applications. You can transform it into one if you have the source, or you can use an existing service wrapper. If you want to transform your application you may want to start with one of the following

If you want to use a service wrapper this may be interesting, or other products like it.

Upvotes: 0

Related Questions