【问题标题】:Dot net core console application stops working automatically after few days点网核心控制台应用程序在几天后自动停止工作
【发布时间】:2020-11-07 15:36:58
【问题描述】:

我在 dot net core 3.1 中创建了一个控制台应用程序,它在运行时更改了现有 json 文件的值。我使用以下命令创建了 exe 文件:

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

这是我的应用程序的代码:

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

这个 exe 运行了几天,但之后它停止工作并且 json 文件中的数据没有改变。我也没有收到任何错误。请让我知道这里出了什么问题。

【问题讨论】:

  • 将异常写入文件也可能会失败,并且会使应用程序崩溃。
  • 首先,对于长时间运行的任务,您应该使用工作者服务模板,然后您可以在您的机器中注册该服务并无限期地运行它,从而更好地管理。其次,您是否检查了事件查看器以查看在 catch 块内写入文件例程中是否有任何错误?
  • 它无法正常运行 - 检查您在 catch 块中写入的文件
  • 请注意,如果由于某种原因引发了与 CurrentDirectory 无法访问相关的异常,您将遇到没有错误文件的崩溃。你能建立一个“安全”的目录吗? (如 %UserProfile%)并检查您是否有错误日志?

标签: c# .net-core console-application .net-core-3.1


【解决方案1】:

if (DateTime.Now

【讨论】:

  • 我试过了....对于我正在测试的应用程序来说,这种情况总是正确的..许可期限到 2025 年
  • ChangeJsonContent 怎么样?你能发布代码吗?
【解决方案2】:

正如我从代码中看到的,您的应用程序运行一个然后停止执行。为什么这样?因为你没有任何循环(while/for/etc)

【讨论】:

  • OP 在 cmets 中指出程序是在不同的日子手动启动的。
猜你喜欢
  • 1970-01-01
  • 2015-11-10
  • 2019-02-22
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
  • 2015-05-27
相关资源
最近更新 更多