C# 在文件夹中创建文件时执行web api调用
每当在指定文件夹上创建新文件时,我们如何在windows服务器上执行web api调用C# 在文件夹中创建文件时执行web api调用,c#,windows,asp.net-web-api,windows-services,C#,Windows,Asp.net Web Api,Windows Services,每当在指定文件夹上创建新文件时,我们如何在windows服务器上执行web api调用 我想在服务器上安装一个windows服务,在一个不确定的循环中运行,并查找其中是否存在文件。但对性能的影响不是很确定。此外,如果服务自动关闭,整个假设将失败。其他更好的方法也是如此 评论时间有点长,所以想把帖子作为答案。所以要回答:每当在指定文件夹上创建新文件时,我们如何在windows服务器上执行web api调用 每当在指定文件夹上创建新文件时 您可以使用类和每个文档 侦听文件系统更改通知,并在 目录或目
我想在服务器上安装一个windows服务,在一个不确定的循环中运行,并查找其中是否存在文件。但对性能的影响不是很确定。此外,如果服务自动关闭,整个假设将失败。其他更好的方法也是如此 评论时间有点长,所以想把帖子作为答案。所以要回答:每当在指定文件夹上创建新文件时,我们如何在windows服务器上执行web api调用 每当在指定文件夹上创建新文件时 您可以使用类和每个文档 侦听文件系统更改通知,并在 目录或目录中的文件发生更改
因此,倾听事件并在处理程序上调用您的Api端点以获取注释,因此考虑将其作为答案发布。所以要回答:每当在指定文件夹上创建新文件时,我们如何在windows服务器上执行web api调用 每当在指定文件夹上创建新文件时 您可以使用类和每个文档 侦听文件系统更改通知,并在 目录或目录中的文件发生更改
因此,侦听事件并在处理程序上调用您的Api端点我构建了许多文件处理应用程序,windows服务是一个不错的选择。我将创建3个文件夹,而不是一个FileSystemWatcher;在,完成和错误。您的服务只需循环处理in文件夹中的所有文件,然后在处理时将其复制到Done文件夹,如果无法处理,则复制到error文件夹。通过这种方式,您可以很容易地看到所做的事情、错误以及剩下要做的事情。该服务还将提取剩余的内容 服务类可以非常轻量级,只需调用处理方法即可:
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
}
}
namespace App.WindowsService
{
公共部分类FileProcessingService:ServiceBase
{
#区域系统组件
专用只读System.Timers.Timer\u processingTimer;
私有静态只读记录器Logger=Logger.Get();
#端区
#区域服务属性
私有整数区间;
private bool Stopped{get;set;}=true;
///
///返回处理的间隔
///
私有整数间隔
{
得到
{
如果(_interval我构建了许多文件处理应用程序,windows服务是一个不错的选择。我将创建3个文件夹:In、Done和Error,而不是FileSystemWatcher。您的服务只需循环处理In文件夹中的所有文件,然后在处理时将其复制到Done文件夹,或者如果不能,则复制到Error文件夹通过这种方式,您可以很容易地看到已完成的操作、错误以及剩余的操作。服务还将提取剩余的操作
服务类可以非常轻量级,只需调用处理方法即可:
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
}
}
namespace App.WindowsService
{
公共部分类FileProcessingService:ServiceBase
{
#区域系统组件
专用只读System.Timers.Timer\u processingTimer;
私有静态只读记录器Logger=Logger.Get();
#端区
#区域服务属性
私有整数区间;
private bool Stopped{get;set;}=true;
///
///返回处理的间隔
///
私有整数间隔
{
得到
{
如果(_interval)您能分享您迄今为止的研究成果吗?@FaizanRabbani,服务器上的windows服务,以不确定循环运行并查找指定文件夹中是否存在文件,如果找到文件,只需处理它[调用web api]你能分享你到目前为止的研究成果吗?@FaizanRabbani,服务器上的一个windows服务,在不确定循环中运行,查找指定文件夹中是否存在文件,如果找到文件,只需处理它[调用web api]阅读更多关于FileSystemWatcher的信息,但是我们将在哪里实现这个FileSystemWatcher?在windows服务上,让它连续运行,还是我们必须进行其他类型的项目?@JibinMathew这个答案是一个提示,先看一看,研究,实现它,如果发现一些不明确的地方,就用代码片段发布您的问题s、 @FaizanRabbani对项目类型有什么想法/建议吗?是windows吗service@JibinMathew下面是一个链接:@JibinMathew是的,它可以是windows服务,甚至可以是像Zappier这样的发布/订阅服务。了解更多有关FileSystemWatcher的信息,但我们将在哪里实现此FileSystemWatcher?在windows服务上,让它连续运行或执行我们必须做一些其他类型的项目吗?@JibinMathew这个答案是一个提示,先看一下,研究一下,实施一下,如果你发现一些含糊不清的地方,就用代码片段发布你的问题。@FaizanRabbani关于项目类型有什么想法/建议吗?是windows吗service@JibinMathew下面是一个链接:@JibinMathew是的,它可以是一个窗口s服务,甚至可以是像Zappier这样的发布/订阅服务。您是否发现在服务器上无限期地在后台运行服务有任何问题?服务是否偶尔会因为未知原因而停止?主进程方法包含在try..catch中,以确保服务不会崩溃并继续运行,而不考虑e错误,当您自己的方法出现错误时,最好写一个错误日志。这应该确保服务不停止运行,我们很少让服务无故停止。您是否看到在服务器上无限期地在后台运行服务有任何问题?服务是否偶尔会因未知原因停止?主要工艺方法被包装在一个try..catch中,以确保