移动到新计算机后,服务无法从队列中读取消息

时间:2010-09-28 12:27:32

标签: c# windows-services msmq

此Windows服务从MSMQ读取电子邮件。部署到Windows Server 2003时它正在阅读邮件。移动到Windows Server 2008后,它已停止阅读电子邮件。它不会将任何日志写入文件。启动Windows服务时,没有任何反应。

请有人告诉我可能出错的地方。我是否需要在Windows Server 2008中配置/更改任何注册表值?

以下是不发生记录的相关方法:

    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.");

    }       

http://pastebin.com/aiECMTVL

的完整代码

1 个答案:

答案 0 :(得分:0)

考虑重构一下OnStart()以帮助让事情变得更清洁。您可以在EACH语句之前抛出日志记录语句,以了解事情的进展情况。

考虑重构您的Log方法,始终将当前时间戳插入代码中的避免重复

更新:如果你无法让它工作,请删除一切,并只有一个日志声明:

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

然后使用大量的日志记录添加您的代码。

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.");
}