views:

22

answers:

1

This Windows Service reads email from the MSMQ. It was reading mail when deployed to Windows Server 2003. After being moved to Windows Server 2008, it has stopped reading email. It does not write any logs to the file. When the Windows Service is started, nothing happens.

Please anyone could tell me what could be wrong. Do I need to configure/change any registry value in Windows Server 2008?

Here's the relevant method where logging does NOT occur:

    public void OnStart()
   // protected override void OnStart(string[] args)        
    {
        string queuePath = ConfigurationManager.AppSettings["mqPath"];
        mailFrom = ConfigurationManager.AppSettings["notificationemail"];
        string SMTPSSLConfig = ConfigurationManager.AppSettings["smtpclientssl"];

        if (string.IsNullOrEmpty(queuePath))
        {
            throw new Exception("Message queue path not defined in app.config.");
        }
        if (string.IsNullOrEmpty(mailFrom))
        {
            throw new Exception("Sender email used to send  notifications is not defined in app.config.");
        }
        if (string.IsNullOrEmpty(SMTPSSLConfig))
        {
            throw new Exception("SMTP SSL config not defined in app.config.");
        }
        objSmtpClient = new SmtpClient();
        objSmtpClient.EnableSsl = Convert.ToBoolean(SMTPSSLConfig);

        //QueueService.InsureQueueExists(queuePath);
        msgQ = new MessageQueue(queuePath);
        msgQ.Formatter = new BinaryMessageFormatter();
        msgQ.MessageReadPropertyFilter.SetAll();
        msgQ.ReceiveCompleted += new ReceiveCompletedEventHandler(msgQ_ReceiveCompleted);
        msgQ.BeginReceive();
        try
        {
            if (!MessageQueue.Exists(queuePath))
            {
                MessageQueue.Create(queuePath);
                Log.WriteMessageQueueInitialParamsLog(System.DateTime.Now.ToString()+" :Message queue successfully created at following location:"+queuePath);
            }
            else
            {
                Log.WriteMessageQueueInitialParamsLog(System.DateTime.Now.ToString() + ": Message Queue Path: " + msgQ.Path);
            }                
        }
        catch (Exception ex)
        {
            Log.WriteCommonLog(System.DateTime.Now.ToString() + " :Error checking message queue existence:\n"+GetExceptionMessageString(ex));   
        }

        //eventLog1.WriteEntry(System.DateTime.Now.ToString()+" :EmailService started successfully." );
        Log.WriteMessageQueueInitialParamsLog(System.DateTime.Now.ToString()+" :Message queue started successfully.");

    }       

Full code at http://pastebin.com/aiECMTVL

A: 

Consider refactoring your OnStart() a bit to help make things a bit cleaner. You could throw logging statements in before EACH statement to know where things are going sideways.

Consider refactoring your Log methods to ALWAYS insert current timestamp, to avoid repeating yourself everywhere in your code.

Update: if you can't get it to work, strip out EVERYTHING, and have just one log statement:

protected override void OnStart(string[] args)
{
   Log.Log("In OnStart");
}

Then add in your code, with liberal amounts of logging.

public void OnStart()        
{
    Log.Write("OnStart");

    if (ValidateConfigSettings(out queuePath, out mailFrom, out SMTPSSLConfig))
    { 
        Log.Write("Validated config");
        msgQ = SetupMessageQueue(queuePath);
        Log.Write("Set up the queue obj.");
        msgQ.BeginReceive();
        Log.Write("Begun queue receive.");
        try
        {
            if (!MessageQueue.Exists(queuePath))
            {
                MessageQueue.Create(queuePath);
                Log.Log("Message queue successfully created following location:" + queuePath);
            }
            else                   
                Log.Log("Message Queue Path: " + msgQ.Path);                    
        }
        catch (Exception ex)              
            Log.WriteCommonLog("Error checking message queue existence:\n" + GetExceptionMessageString(ex));                
    }
    else
        Log.Write("Found bad config.");

    Log.Log("Message queue started successfully.");
} 
p.campbell
It didn't work again in Windows server 2008 it works fine in Windows server 2003. But Thanks for the suggestion of refactoring OnStart() to make a bit cleaner.
jiwan
ok finially i found the solution it was security Issues. After debugging the service i found that the Message Queue was not given full permission. after giving the messaage queue full permission the service started sending mail.
jiwan
@jiwan: good stuff. Perhaps update your question with your findings, so others might find that info later!
p.campbell