在Web服务中使用FileWatcher

时间:2013-11-07 11:46:07

标签: c# web-services

我有一个调用[web方法]的客户端。在[web方法]中,我正在检查是否存在某些文件,这些文件本身包含在while(true)循环中。当存在文件或发生超时时,回调将返回给客户端。

我注意到运行它是因为wp3进程在内存使用方面有所增加。

有人告诉我,如果使用FileWatcher而不是while(true)循环,则将内存放在.Net框架而不是IIS进程上。我试图测试这个但是当找不到文件时我无法看到如何将回调返回给客户端。

我的代码:

[桌面应用]

private void _tmrRequestHandler_Tick(object sender, EventArgs e)
{
try
{
_tmrRequestHandler.enabled = false;
//call my web service async
}
catch
{
_tmrRequestHandler.enabled = true;
}
}

private void WSconnector_GetRequestsCompleted(object sender, wsConnector.GetRequestsCompletedEventArgs e)
{
//do stuff
_tmrRequestHandler.enabled = true;
}

[网络服务器] - 旧方式

[WebMethod]
public string[] GetRequests(string _mac)
{
   string[] _response = null;
   while (_fileCount == 0)
   {
    string[] _files = Directory.GetFiles("my root path" + _mac, "*.dat");
    _fileCount = _files.Length;
    if (_files.Length > 0)
    {
     _response = new string[_files.Length];
     _files.CopyTo(_response, 0);
     return _response;
    }
 }

}

[网络服务器] - 建议的新方法

[WebMethod]
public string[] GetRequests(string _mac)
{
    string[] _response = null;
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = AbsoluteRequestQueue + _mac;
    watcher.NotifyFilter = NotifyFilters.LastWrite;
    watcher.Filter = "*.dat*";
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.EnableRaisingEvents = true;
}

private void OnChanged(object source, FileSystemEventArgs e)
{
    //file found!!
    //HOW DO I GIVE CALLBACK TO MY CLIENT AND SHOULD I REALLY BE CONSIDEREING DOING IT THIS WAY??
}

由于

0 个答案:

没有答案