簡體   English   中英

將 .NET Core 控制台應用程序作為服務運行,還是重做?

[英]Running .NET Core console app as a service, or redo?

我們有來自第三方編碼的 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();
        }

    }

}

我看過一些類似的帖子,但不確定我應該采取什么方向。 任何方向將不勝感激!

聽起來我們在非常相似的環境中工作。 我們開始使用控制台應用程序進行 ERP 集成。 它們的主要缺點是它們在用戶的桌面上運行,因此您必須以該用戶的身份進入 RDP 來管理它們,並且如果服務器重新啟動它們不會自動重新啟動。 我一直在將它們全部轉換為 Windows 服務。 .NET Core 使它變得相當容易。

Tim Corey 有一個關於如何在 .NET Core 3 中編寫Windows 服務的優秀視頻。 他談到在部署新版本時刪除和重新創建服務,但我認為沒有必要這樣做。 只需 RDP,停止服務以便沒有文件在使用中,部署並重新啟動它。 這不像重新部署 .NET Core Web 應用程序那么簡單,它會為您管理所有停止和重新啟動,但也不錯。

需要知道的關鍵是異常絕對不能從Worker.ExecuteAsync傳播出去。 它們不會在任何地方處理,Windows 會在您的服務未運行時將其顯示為正在運行。 無論您在ExecuteAsync中做什么,都要在捕獲所有異常並保持運行的循環中進行。

文件觀察器看似復雜。 確保您正在偵聽錯誤事件。 例如,如果您正在監視網絡共享並且它變得不可用,則FileSystemWatcher將發出錯誤並且在共享重新聯機時不會恢復工作。 不過,我還沒有想出處理錯誤的最佳方法。 我認為使其可靠需要在出現任何錯誤后更換FileSystemWatcher

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM