-3

I have created a console application in dot net core 3.1 which changes the values of existing json file on running. I created the exe file by using following command :

dotnet publish -r win-x86 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true

here is the code for my application:

 static void Main (string[] args) {
        try {
            // blah blah.. some code to get liscence period
                if (DateTime.Now <= liscencePeriod) {
                    var processor = new JsonDataProcessor ();
                    processor.AmmendJsonData ();
                } 
            }
        } catch (Exception ex) {
            var errorFile = Path.Combine (Environment.CurrentDirectory, "JsonEditorError.txt");
            File.WriteAllText (errorFile, $"{ex.Message}");
        }
        
    }

 //AmmendJsonData code inside JsonDataProcessor class
internal void AmmendJsonData () {
    try {
        var jsonPath = Path.Combine (Environment.CurrentDirectory, "AVLJsonFile.json");
        var jsonData = File.ReadAllText (jsonPath);
        var jsonSchema = JsonConvert.DeserializeObject<Root> (jsonData);
        ChangeJsonContent (jsonSchema);
        var newJson = JsonConvert.SerializeObject (jsonSchema, Formatting.Indented);
        File.WriteAllText (jsonPath, newJson);
    } catch (Exception ex) {
        throw ex;
    }
}

This exe runs fine for few days but after that it stops working and data in json file doesn't change.. I also don't get any error. Please let me know whats going wrong here.

Vivek Mishra
  • 1,772
  • 1
  • 17
  • 37
  • Writing an exception to file may fail as well, and will crash the application. – Peter Bons Nov 07 '20 at 15:40
  • 3
    First, for long running tasks you should be using the worker service template, then you can register the service in your machine and run it indefinitely with better management. Second, did you checked event viewer to see if there are any errors in your write to file routine inside the catch block? – MestreDosMagros Nov 07 '20 at 15:43
  • yeah.. I did not find any error in event viewer.. and this is happening after every few days.. – Vivek Mishra Nov 07 '20 at 15:51
  • @Pac0 Process is running without file being amended. – Vivek Mishra Nov 07 '20 at 15:52
  • And I noticed that after it stops amending the file... when I run this exe from command prompt it starts working again and after few days it again stops. – Vivek Mishra Nov 07 '20 at 15:55
  • what scheduler do you use. I'd suggest windows task manager, easy to configure – Yehor Androsov Nov 07 '20 at 16:05
  • @YegorAndrosov this is not a scheduler. it works only when you click the exe. the problem is this exe stops amending the data after few days when I click the exe to run the program. – Vivek Mishra Nov 07 '20 at 16:11
  • 1
    it can't run without errors - check file you are writing to in catch block – Yehor Androsov Nov 07 '20 at 16:15
  • 1
    Note that if for some reason an exception is thrown related to the fact that CurrentDirectory is somehow unaccessible, you'll get a crash with no error file. Could you set up a "safe" directory? (Like %UserProfile%) and check if you have an error log? – Pac0 Nov 08 '20 at 10:37
  • 1
    [Don't `throw ex`, instead you should just `throw`](https://stackoverflow.com/questions/22623/best-practices-for-catching-and-re-throwing-net-exceptions) – Erik Philips Nov 08 '20 at 12:06
  • VIVEK, you say that `it works only when you click the exe. the problem is this exe stops amending the data after few days when I click the exe to run the program.`. Does that mean that from time to time, you come and click the exe, and it starts up and updates the file and finishes. But after a few rounds of doing that, you come and click the exe and it starts up and does nothing? Maybe something else is holding that output file so your app can't write to it? – quetzalcoatl Nov 08 '20 at 12:12
  • Could you also check the modification timestamp of the target file when "nothing occurs"? Maybe it's rewritten exactly the same, and the issue lies that ChangeJsonContent just output exactly the same data. – Pac0 Nov 09 '20 at 02:10

2 Answers2

0

if (DateTime.Now <= liscencePeriod) ... If this is false it will not do anything and dont give any error. You have to add else with a warning message.

Serge
  • 40,935
  • 4
  • 18
  • 45
-1

As I can see by code, your application runs ones and then stop execution. Why so? Because you don't have any loop (while/for/etc)

Mixim
  • 972
  • 1
  • 11
  • 37
  • OP indicated in comments that the program is started manually at some different days. – Pac0 Nov 09 '20 at 02:08