0

我目前正在从事 ac#/php/soap 项目。主应用程序是控制台应用程序,php 是管理控制台应用程序的 Web 界面。

在控制台应用程序中,有一个静态列表数组,当 php web 界面发布肥皂消息时,它会调用控制台应用程序中的一个函数来更新数据库,然后它应该更新列表数组。但是,这是有效的,因为它正在设置值,但是控制台应用程序没有看到它已被更改。

看起来静态列表数组似乎仅在soap服务中使用,实际上并没有访问控制台应用程序正在查看的静态数组。

如何更新此静态变量,以便控制台应用程序可以看到它已被修改。

感谢您的任何帮助,您可以提供。

更新 如下要求是我正在使用的代码。

以下是公共网络方法

[WebMethod]
public List<SoapHandler.SoapStatusDetails> RetryEmailQueue(int emailId)
{
    SoapHandler soapHandler = new SoapHandler();
    List<SoapHandler.SoapStatusDetails> status = soapHandler.addEmailBackToQueue(emailId); 

    return status;
}

下面是上面代码中调用的 SoapHandler 方法,这个类在控制台应用程序中。

public List<SoapStatusDetails> addEmailBackToQueue(int emailId)
        {
            List<SoapStatusDetails> status = new List<SoapStatusDetails>();
            try
            {
                string query = "UPDATE emailqueue SET Status=@status, RetryCount=@count WHERE id=@id";
                using (ConnectDb db = new ConnectDb(appSettings))
                {
                    using (MySqlCommand cmd = new MySqlCommand(query, db.conn))
                    {
                        cmd.Parameters.AddWithValue("@status", ManageEmail.EmailStatus.Queued.ToString());
                        cmd.Parameters.AddWithValue("@count", 0);
                        cmd.Parameters.AddWithValue("@id", emailId);
                        cmd.ExecuteNonQuery();
                        ManageEmail.queuedEmailIds.Add(emailId);
                        status.Add(new SoapStatusDetails()
                        {
                            status = SoapStatus.Success.ToString(),
                            errorMessage = ""
                        });
                    }
                }
            }
            catch (MySqlException ex)
            {
                string error = string.Format("Unable to put email back into queue for email: id {0}. MySQL Error: {1}",
                    emailId, ex.Message);
                library.logging(classDetails + MethodInfo.GetCurrentMethod().Name, error);
                library.setAlarm(error, "Critical", MethodInfo.GetCurrentMethod().Name);
                status.Add(new SoapStatusDetails()
                {
                    status = SoapStatus.Failure.ToString(),
                    errorMessage = ex.Message
                });
            }
            catch (Exception ex)
            {
                string error = string.Format("Unable to put email back into queue: id{0}. General Error: {1}",
                    emailId, ex.Message);
                library.logging(classDetails + MethodInfo.GetCurrentMethod().Name, error);
                library.setAlarm(error, "Critical", MethodInfo.GetCurrentMethod().Name);
                status.Add(new SoapStatusDetails()
                {
                    status = SoapStatus.Failure.ToString(),
                    errorMessage = ex.Message
                });
            }
            return status;
        }

ManageEmail.queuedEmailIds.Add(emailId);这是似乎由 Soap 服务更改的静态变量,但控制台应用程序从未看到它已更改。

更新 我尝试过使用单例但遇到了同样的问题,它似乎在做同样的事情。

在 ManageEmail 类中我添加了这个,

public List<int> queuedEmailIds = new List<int>();

        public static ManageEmail instance;

        private ManageEmail() { }

        public static ManageEmail Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ManageEmail();
                }
                return instance;
            }
        }

当我访问列表数组时,我执行以下操作

ManageEmail.Instance.queuedEmailIds.Add(emailId);

然而,这些变化并没有产生任何影响。

4

1 回答 1

1

在没有原始诊断的情况下,我认为您所拥有的是一个实例化问题,其中一个类的实例比另一个类具有引用值。

这可能是查看单例模式的好地方,因此您始终引用该类的相同实例。我意识到其中一个值是静态变量,但它是实例化变量(即列表)

于 2012-07-09T20:23:06.373 回答