Using this example we can implement FileSystemWatcher in the WCF service. Here we are inserting some xml file in the Redis cache using Hashes. As well if any file got updated, service automatically fire the event and show the updated file information.
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading;
using ServiceStack.Redis.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.IO;
using System.Xml.Linq;
namespace WCFService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher();
Service1()
{
watcher.Path = "C:\\folder";
watcher.IncludeSubdirectories = true;
watcher.Changed += watcher_Changed;
watcher.Filter = "*.*";
watcher.NotifyFilter = NotifyFilters.Attributes |
NotifyFilters.CreationTime |
NotifyFilters.DirectoryName |
NotifyFilters.FileName |
NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.Security |
NotifyFilters.Size;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
}
public string GetData(int value)
{
try
{
string host = "localhost";
string message = string.Empty;
string elementKey = "testKeyRedis";
//MonitorFolder();
#region set example
//using (RedisClient redisClient = new RedisClient(host))
//{
// if (redisClient.Get(elementKey) == null)
// {
// // adding delay to see the difference
// //Thread.Sleep(5000);
// redisClient.Set(elementKey, "some cached value");
// }
// message = "data from" + redisClient.Get<string>(elementKey);
//}
#endregion
using (RedisClient redisClient = new RedisClient(host))
{
//file system xml file reading
String Result;
XElement xmlDoc = null;
string key = "key";
xmlDoc = XElement.Load("file.xml");
XElement xmlResult;
redisClient.SetEntryInHash("urn:dateTime", key, xmlDoc.ToString());
var hash = redisClient.GetAllEntriesFromHash("urn:dateTime");
xmlResult = XElement.Parse(hash[key]);
//xmlResult = hash[key] as XElement;
//if (redisClient.Set(key, xmlDoc))
// xmlResult = redisClient.Get<object>(key) as XElement;
}
return string.Format("Returning from redis", message);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.Read();
return "Exception";
}
}
//public void MonitorFolder()
//{
// System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher();
// watcher.Path = "C:\\ISCSKT";
// watcher.IncludeSubdirectories = false;
// watcher.Changed += watcher_Changed;
// watcher.Filter = "*.*";
// watcher.NotifyFilter = NotifyFilters.Attributes |
// NotifyFilters.CreationTime |
// NotifyFilters.DirectoryName |
// NotifyFilters.FileName |
// NotifyFilters.LastAccess |
// NotifyFilters.LastWrite |
// NotifyFilters.Security |
// NotifyFilters.Size;
// watcher.EnableRaisingEvents = true;
//}
void watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
string host = "localhost";
string message = string.Empty;
string elementKey = "testKeyRedis";
using (RedisClient redisClient = new RedisClient(host))
{
if (redisClient.Get(elementKey) == null)
{
// adding delay to see the difference
//Thread.Sleep(5000);
redisClient.Set(elementKey, "some cached value");
}
//message = "data from" + redisClient.Get<string>(elementKey);
}
//Console.WriteLine(string.Format("Change: {0}, File: {1}", e.ChangeType, e.FullPath));
}
public static void OnChanged(object source, FileSystemEventArgs e)
{
string host = "localhost";
string message = string.Empty;
string elementKey = "changed";
using (RedisClient redisClient = new RedisClient(host))
{
if (redisClient.Get(elementKey) == null)
{
// adding delay to see the difference
//Thread.Sleep(5000);
redisClient.Set(elementKey, "some changed value");
}
//message = "data from" + redisClient.Get<string>(elementKey);
}
// Specify what is done when a file is changed.
//Console.WriteLine("{0}, with path {1} has been {2}", e.Name, e.FullPath, e.ChangeType);
}
public static void OnRenamed(object source, RenamedEventArgs e)
{
string host = "localhost";
string message = string.Empty;
string elementKey = "renamed";
using (RedisClient redisClient = new RedisClient(host))
{
if (redisClient.Get(elementKey) == null)
{
// adding delay to see the difference
//Thread.Sleep(5000);
redisClient.Set(elementKey, "some renamed value");
}
//message = "data from" + redisClient.Get<string>(elementKey);
}
// Specify what is done when a file is renamed.
//Console.WriteLine(" {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading;
using ServiceStack.Redis.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.IO;
using System.Xml.Linq;
namespace WCFService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher();
Service1()
{
watcher.Path = "C:\\folder";
watcher.IncludeSubdirectories = true;
watcher.Changed += watcher_Changed;
watcher.Filter = "*.*";
watcher.NotifyFilter = NotifyFilters.Attributes |
NotifyFilters.CreationTime |
NotifyFilters.DirectoryName |
NotifyFilters.FileName |
NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.Security |
NotifyFilters.Size;
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
}
public string GetData(int value)
{
try
{
string host = "localhost";
string message = string.Empty;
string elementKey = "testKeyRedis";
//MonitorFolder();
#region set example
//using (RedisClient redisClient = new RedisClient(host))
//{
// if (redisClient.Get(elementKey) == null)
// {
// // adding delay to see the difference
// //Thread.Sleep(5000);
// redisClient.Set(elementKey, "some cached value");
// }
// message = "data from" + redisClient.Get<string>(elementKey);
//}
#endregion
using (RedisClient redisClient = new RedisClient(host))
{
//file system xml file reading
String Result;
XElement xmlDoc = null;
string key = "key";
xmlDoc = XElement.Load("file.xml");
XElement xmlResult;
redisClient.SetEntryInHash("urn:dateTime", key, xmlDoc.ToString());
var hash = redisClient.GetAllEntriesFromHash("urn:dateTime");
xmlResult = XElement.Parse(hash[key]);
//xmlResult = hash[key] as XElement;
//if (redisClient.Set(key, xmlDoc))
// xmlResult = redisClient.Get<object>(key) as XElement;
}
return string.Format("Returning from redis", message);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.Read();
return "Exception";
}
}
//public void MonitorFolder()
//{
// System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher();
// watcher.Path = "C:\\ISCSKT";
// watcher.IncludeSubdirectories = false;
// watcher.Changed += watcher_Changed;
// watcher.Filter = "*.*";
// watcher.NotifyFilter = NotifyFilters.Attributes |
// NotifyFilters.CreationTime |
// NotifyFilters.DirectoryName |
// NotifyFilters.FileName |
// NotifyFilters.LastAccess |
// NotifyFilters.LastWrite |
// NotifyFilters.Security |
// NotifyFilters.Size;
// watcher.EnableRaisingEvents = true;
//}
void watcher_Changed(object sender, System.IO.FileSystemEventArgs e)
{
string host = "localhost";
string message = string.Empty;
string elementKey = "testKeyRedis";
using (RedisClient redisClient = new RedisClient(host))
{
if (redisClient.Get(elementKey) == null)
{
// adding delay to see the difference
//Thread.Sleep(5000);
redisClient.Set(elementKey, "some cached value");
}
//message = "data from" + redisClient.Get<string>(elementKey);
}
//Console.WriteLine(string.Format("Change: {0}, File: {1}", e.ChangeType, e.FullPath));
}
public static void OnChanged(object source, FileSystemEventArgs e)
{
string host = "localhost";
string message = string.Empty;
string elementKey = "changed";
using (RedisClient redisClient = new RedisClient(host))
{
if (redisClient.Get(elementKey) == null)
{
// adding delay to see the difference
//Thread.Sleep(5000);
redisClient.Set(elementKey, "some changed value");
}
//message = "data from" + redisClient.Get<string>(elementKey);
}
// Specify what is done when a file is changed.
//Console.WriteLine("{0}, with path {1} has been {2}", e.Name, e.FullPath, e.ChangeType);
}
public static void OnRenamed(object source, RenamedEventArgs e)
{
string host = "localhost";
string message = string.Empty;
string elementKey = "renamed";
using (RedisClient redisClient = new RedisClient(host))
{
if (redisClient.Get(elementKey) == null)
{
// adding delay to see the difference
//Thread.Sleep(5000);
redisClient.Set(elementKey, "some renamed value");
}
//message = "data from" + redisClient.Get<string>(elementKey);
}
// Specify what is done when a file is renamed.
//Console.WriteLine(" {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
Post a Comment
Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.