【问题标题】:Execute a web api call on creation of file in a folder在文件夹中创建文件时执行 Web api 调用
【发布时间】:2018-02-16 06:13:51
【问题描述】:

每当在指定文件夹上创建新文件时,我们如何在 Windows 服务器上执行 Web api 调用

我正在考虑服务器上的 Windows 服务,在无限循环中运行并查找其中是否存在文件。但不太确定性能影响。此外,如果服务自动关闭,则整个假设都将失败。任何其他更好的方法也是如此。

【问题讨论】:

  • 您能分享一下您目前的研究成果吗?
  • @FaizanRabbani ,服务器上的 Windows 服务,在无限循环中运行并查找指定文件夹中是否存在文件,如果找到文件则处理它[调用 web api]

标签: c# windows asp.net-web-api windows-services


【解决方案1】:

有点长的评论,所以想张贴作为答案。所以回答:每当在指定文件夹上创建新文件时,我们如何在 Windows 服务器上执行 Web api 调用

每当在指定文件夹上创建新文件时

您可以使用FileSystemWatcher 类和每个文档

侦听文件系统更改通知并在何时引发事件 目录或目录中的文件发生更改。

因此监听事件并在处理程序上调用您的 Api 端点

【讨论】:

  • 阅读更多关于 FileSystemWatcher 的内容,但是我们将在哪里实现这个 FileSystemWatcher 呢?在 Windows 服务上让它连续运行还是我们必须做一些其他类型的项目?
  • @JibinMathew 这个答案是一个提示,首先通过这个,研究,实施它,如果你发现一些歧义,而不是使用代码 sn-ps 发布你的问题。
  • @FaizanRabbani 对项目类型有什么想法/建议吗?是windows服务
  • @JibinMathew 以下是链接:c-sharpcorner.com/UploadFile/7d3362/…
  • @JibinMathew 是的,它可以是 Windows 服务,甚至可以是像 Zappier 这样的 pub/sub 服务
【解决方案2】:

我已经构建了许多文件处理应用程序,Windows 服务是一个不错的选择。我将创建 3 个文件夹,而不是 FileSystemWatcher;在,完成和错误。您的服务只需要遍历 In 文件夹中的所有文件进行处理,然后在处理时将它们复制到 Done 文件夹,如果无法处理则将其复制到错误文件夹。通过这种方式,您可以轻松查看已完成的操作和错误以及剩下的操作。该服务还将拾取剩余的内容。

服务类可以非常轻量级,只需调用你的处理方法:

namespace App.WindowsService
{
    public partial class FileProcessingService : ServiceBase
    {
        #region System Components

        private readonly System.Timers.Timer _processingTimer;
        private static readonly Logger Logger = Logger.Get();

        #endregion

        #region Service Properties

        private int _interval;
        private bool Stopped { get; set; } = true;

        /// <summary>
        /// Returns the interval for processing
        /// </summary>
        private int Interval
        {
            get
            {
                if (_interval <= 0)
                {
                    return _interval = Settings.ServiceInterval;
                }

                return _interval;
            }
        }

        #endregion

        #region Constructor

        public FileProcessingService()
        {
            InitializeComponent();
            CanShutdown = true;
            _processingTimer = new System.Timers.Timer();
            _processingTimer.Elapsed += ProcessingTimerElapsed;
        }

        #endregion

        #region Service Events

        protected override void OnStart(string[] args)
        {
            _processingTimer.Interval = Interval;
            _processingTimer.Start();
            Stopped = false;
            Logger.Trace("Service Started");
        }

        protected override void OnStop()
        {
            _processingTimer.Stop();
            Stopped = true;
            Logger.Trace("Service Stopped");
        }

        protected override void OnShutdown()
        {
            OnStop();
            base.OnShutdown();
        }

        #endregion

        #region Processing Timer Elapsed Event

        private void ProcessingTimerElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Logger.MethodEntry();

            try
            {
                _processingTimer.Stop();
                Process();
            }
            // ReSharper disable once EmptyGeneralCatchClause
            catch
            {
            }

            if (!Stopped)
            {
                _processingTimer.Start();
            }
        }

        #endregion

        #region Public Methods

        public void Process()
        {
            // Call your processing method
        }

        #endregion
    }
}

【讨论】:

  • 您是否遇到过在服务器上无限期地在后台运行服务的问题?另外是否有可能由于某些未知原因偶尔停止服务?
  • 主要的 Process 方法被包裹在一个 try..catch 中,以确保服务不会崩溃并继续,无论错误如何,最好在您自己的方法错误时写入错误日志。这应该确保服务运行不停止,我们很少有服务无缘无故停止。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
相关资源
最近更新 更多