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

C# 移动到新计算机后,服务无法从队列中读取消息,c#,windows-services,msmq,C#,Windows Services,Msmq,此Windows服务从MSMQ读取电子邮件。部署到Windows Server 2003时,它正在读取邮件。移动到Windows Server 2008后,它已停止阅读电子邮件。它不会将任何日志写入该文件。当Windows服务启动时,不会发生任何事情 请任何人都告诉我可能出了什么问题。我是否需要在Windows Server 2008中配置/更改任何注册表值 以下是不进行日志记录的相关方法: public void OnStart() // protected override v

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

    }       

的完整代码中,请考虑对OnStart进行一点重构,以使其更干净。您可以在每条语句之前插入日志语句,以了解事情的发展方向

考虑重构日志方法以始终插入当前时间戳,以避免在代码中的任何地方重复您自己

更新:如果无法使其工作,请删除所有内容,只使用一条日志语句:

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

它在Windows server 2008中不再工作,在Windows server 2003中工作正常。但是谢谢你的建议,重构OnStart,让它更干净一点。好的,最后我找到了解决方案,那就是安全问题。调试服务后,我发现消息队列没有被授予完全权限。在给予消息队列完全权限后,服务开始发送邮件。@jiwan:好东西。也许用你的发现更新你的问题,这样其他人以后可能会找到这些信息@吉万:我也面临同样的问题。您可以让em知道您为消息队列分配了哪些访问权限以使其正常工作吗?