Boardy
Boardy

Reputation: 36205

Modify List Array from Console application from Soap Request

I am currently working on a c#/php/soap project. The main app is a console app and the php is a web interface to manage the the console app.

Within the console app, there is a static List array, when the php web interface posts the soap message it calls a function within the console app to update the database and then it is supposed to update the List array. However, this is working, as it is setting the value, however the console app doesn't see that it has been changed.

It looks as if the static List array is being used from within the soap service only, and is not actually accessing the static array that the console app is looking at.

How can I update this static variable so that the console application can see that it has been modified.

Thanks for any help you can provide.

UPDATE As requested below is the code that I am using.

Below is the public web method

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

    return status;
}

Below is the SoapHandler method which gets called in the above code, this class is within the Console App.

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); this is the static variable that seems to be changed by the Soap Service but the Console app never sees that this has changed.

UPDATE I've tried using a singleton but got the same problem, it seems to be doing the same thing.

In the ManageEmail Class I have added this,

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;
            }
        }

When I access the list array I do the following

ManageEmail.Instance.queuedEmailIds.Add(emailId);

These changes though however, haven't made any difference.

Upvotes: 0

Views: 241

Answers (1)

nyxthulhu
nyxthulhu

Reputation: 9752

Without having raw diagnose I think what you have is a instancing issue where one instance of a class has a referenced value than another class.

This might be a good place to look at the Singleton Pattern so you are always referencing the same instance of the class. I realise one of the values is a static variable however that is a instantiated variable (i.e a list)

Upvotes: 1

Related Questions