0

I have an ASP.NET Core MVC application that might be restarted from time to time (maintenance); how can make some variable values persistent from an execution to the next?

PS: That's the code that needs to write value as persistent. For example "LastMaintenanceRestartTime = 03/04-2020", the maintenance restart occurs once a day so the code needs to remember the last time it was restarted.

In UWP, I could do the following code but I can't seem to find an equivalent for ASP.NET Core:

Windows.Storage.ApplicationData.Current.LocalSettings.Values[key] = value;

The best I could find is the following code but the values are only persistent within the same execution:

AppDomain.CurrentDomain.SetData(key, value);

Some talk about "Application.Settings" but I can't seem to be able to reach this namespace...

I've also seen some people talking about "AppSettings" files that can be modified during execution but it seems rather complex to keep a simple value persistent...

Do you have any recommendation, solution or ideas for me?

Don Madrino
  • 147
  • 1
  • 11

3 Answers3

0

I found the solution:

static void ReadSetting(string key)  
        {  
            try  
            {  
                var appSettings = ConfigurationManager.AppSettings;  
                string result = appSettings[key] ?? "Not Found";  
                Console.WriteLine(result);  
            }  
            catch (ConfigurationErrorsException)  
            {  
                Console.WriteLine("Error reading app settings");  
            }  
        }  

        static void AddUpdateAppSettings(string key, string value)  
        {  
            try  
            {  
                var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);  
                var settings = configFile.AppSettings.Settings;  
                if (settings[key] == null)  
                {  
                    settings.Add(key, value);  
                }  
                else  
                {  
                    settings[key].Value = value;  
                }  
                configFile.Save(ConfigurationSaveMode.Modified);  
                ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);  
            }  
            catch (ConfigurationErrorsException)  
            {  
                Console.WriteLine("Error writing app settings");  
            }  
        }  

Link: https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.appsettings?redirectedfrom=MSDN&view=dotnet-plat-ext-5.0#System_Configuration_ConfigurationManager_AppSettings

Don Madrino
  • 147
  • 1
  • 11
-1

Create a model to save data and last execution time

public class ApplicationData {
      public DateTime LastExecutionTime {get;set;}
      public string Data {get;set;}
      public bool isRunningFirstTime {get;set}
   }

1.On first application run, model should be updated to current values and isRunningFirstTime should be set to false. 2. On second run, read or update values based on date and application running count

rashidali
  • 330
  • 1
  • 5
  • 16
  • How about the writing part? I need the code to write the variable as persistent for the next execution... – Don Madrino Nov 27 '20 at 13:45
  • How would you persist data to any given json file? Use the same approach. – Chris Cavell Nov 27 '20 at 14:12
  • @rashidali Sorry but I'm missing how to save the model... That's what I'm looking for... – Don Madrino Nov 27 '20 at 14:47
  • @ChrisCavell So the best way is to create a physical file to hold properties? – Don Madrino Nov 27 '20 at 14:48
  • Not necessarily, you could persist to database just as well. Basically take what @rashidali has given you and persist in a way that works best for your scenario. – Chris Cavell Nov 27 '20 at 15:32
  • @ChrisCavell I'm searching for the best way to do it, and especially how to do it (the saving part)... – Don Madrino Nov 27 '20 at 15:37
  • Not necessarily the best way but a way I do things is I have an [AppSettings](https://github.com/cdcavell/cdcavell.name/blob/master/Source/Web/cdcavell/Models/AppSettings/AppSettings.cs) model class where I store application configuration settings. Mostly bound to appsettings.json but some fields from other sources. One such is `LastModifiedDate`. Not what you are asking (many examples of persisting data out there) but shows how I am reading file attribute to set value. You could just as easily read from file or database. – Chris Cavell Nov 27 '20 at 15:54
-1

Expanding on @rashidali answer (and not saying best, but):

public class ApplicationData 
{
      private DateTime _lastExecutionTime;
      public DateTime LastExecutionTime 
      {
            get
            {
                  _lastExecutionTime = (read from file/database);
                  return _lastExecutionTime;
            }
            set
            {
                  _lastExecutionTime = value;
                  (write _lastExecutionTime to file/database);
            }
      }
      public string Data {get;set;}
      public bool isRunningFirstTime {get;set}
}
Chris Cavell
  • 189
  • 2
  • 10