【问题标题】:Running .NET Core console app as a service, or redo?将 .NET Core 控制台应用程序作为服务运行还是重做?
【发布时间】:2020-10-26 16:53:32
【问题描述】:

我们有来自第三方编码的 UTF-16 LE 的订单。我们的 ERP 只能读取 UTF-8 编码。因此,我创建了 .NET Core 控制台应用程序,用于监视订单到达的目录并将它们写入 ERP 抓取文件的位置。如何让它在我们的 Windows Server 2016 上运行?我应该废弃它并将其编写为 Windows 服务吗?

using System;
using System.IO;

public class RewriteUsingUTF8
{
    public static void Main()
    {
        string ordrstkPath = @"\\Rep-app\sftp_root\supplypro\ordrstk";
        string conrstkPath = @"\\Rep-app\sftp_root\supplypro\Conrstk";
        Watch(ordrstkPath);
        Watch(conrstkPath);
        Console.ReadLine();
    }

    private static void Watch(string path)
    {
        //initialize
        FileSystemWatcher watcher = new FileSystemWatcher();

        //assign parameter path
        watcher.Path = path;

        //create event
        watcher.Created += FileSystemWatcher_Created;
        watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size | NotifyFilters.Attributes;

        //only look for csv
        watcher.Filter = "*.csv";

        // Begin watching.
        watcher.EnableRaisingEvents = true;
    }

    // method when event is triggered (file is created)
    private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)

    {

        ReadWriteStream(e.FullPath, e.Name);

    }

    private static void ReadWriteStream(string path, string fileName)
    {

        FileStream originalFileStream = new FileStream(path, FileMode.Open, FileAccess.Read);

        //destination path by replacing SFTP user directory
        string destinationPath = path.Replace(@"\supplypro\", @"\ftpuser\");

        FileStream destinationFileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write);

        StreamReader streamReader = new StreamReader(originalFileStream);

        StreamWriter streamWriter = new StreamWriter(destinationFileStream);

        string currentLine;

        try
        {
            currentLine = streamReader.ReadLine();
            while (currentLine != null)
            {
                streamWriter.WriteLine(currentLine);
                currentLine = streamReader.ReadLine();
            }

            //archive path
            string archivePath = path.Replace(fileName, @"\archive\" + fileName);

            //move to archive path
            File.Move(path, archivePath);
        }
        catch (Exception e)
        {
            //error path
            string errorPath = path.Replace(fileName, @"\error\" + fileName);

            //move to error path
            File.Move(path, errorPath);

            //need to write code for error to write to event viewer
            Console.WriteLine("Exception: " + e.Message);
        }
        finally
        {
            //dispose resources
            streamReader.Close();
            streamWriter.Close();
            originalFileStream.Close();
            destinationFileStream.Close();
        }

    }

}

我看过一些类似的帖子,但不确定我应该采取什么方向。任何方向将不胜感激!

【问题讨论】:

  • 您可以将其重写为一项服务——这具有一定的优势——但最少的工作是将其保留为控制台应用程序,只需使用Windows task scheduler 安排它偶尔运行。

标签: c# .net-core windows-server


【解决方案1】:

听起来我们在非常相似的环境中工作。我们开始使用控制台应用程序进行 ERP 集成。它们的主要缺点是它们在用户的桌面上运行,因此您必须以该用户的身份进入 RDP 来管理它们,并且如果服务器重新启动,它们不会自动重新启动。我一直在将它们全部转换为 Windows 服务。 .NET Core 让它变得相当容易。

Tim Corey 有一个关于如何编写 Windows services in .NET Core 3 的精彩视频。他谈到在部署新版本时删除和重新创建服务,但我认为没有必要这样做。只需在 RDP 中,停止服务,以便没有文件在使用中,部署并重新启动它。它不像重新部署一个 .NET Core Web 应用程序那样简单,它会为您管理所有停止和重新启动,但也不算太糟糕。

要知道的重要一点是异常绝对不能传播到Worker.ExecuteAsync 之外。它们不会在任何地方得到处理,并且 Windows 会在您的服务未运行时显示您的服务正在运行。无论你在ExecuteAsync 中做什么,都要在一个捕获所有异常并继续运行的循环中执行。

文件观察器看似复杂。确保您正在侦听错误事件。例如,如果您正在观看网络共享并且它变得不可用,FileSystemWatcher 将发出一个错误,并且当共享重新联机时将恢复工作。不过,我还没有找到处理错误的最佳方法。我认为使其可靠需要在出现任何错误后替换 FileSystemWatcher

【讨论】:

  • 谢谢!我将深入研究工作服务并考虑将错误事件监听添加到我的FileSystemWatcher
  • 您好,我已经使用 .NET core worker 服务重写了这个。但是,我在启动服务时收到 1053 错误。你以前遇到过这个问题吗?我很可能会在 SO 上发表一篇新文章,因为我已经添加了 windows 托管包并在我的 HostBuilder 中包含了 UseWindowsService()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多