[ServiceContract(CallbackContract = typeof(IClientContract))]
public interface IServerContract
{
[OperationContract(IsOneWay = false)]
void StartWatcher(string path);
void GetImagesFromPathAtFirstLoad(string path);
}
[ServiceContract]
public interface IClientContract
[OperationContract(IsOneWay = true)]
void GetImagesFromServer(List<CustomImage> img);
void GetUpdateFromFileShare(CustomImage img);
[DataContract]
public class CustomImage
public enum ChangeType
Add,
Delete
[DataMember]
public string Name { get; set; }
public string FullPath { get; set; }
public Byte[] Img { get; set; }
public ChangeType Modification { get; set; }
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)]
public class ServiceImplementation : IServerContract
#region Member Fields
private System.IO.FileSystemWatcher fsw = null;
private OperationContext context = null;
private IClientContract client = null;
#endregion
#region IServerContract Members
/// <summary>
/// Start the FileSystemWatcher on the server.
/// </summary>
/// <param name="path">The path to the directory to watch.</param>
public void StartWatcher(string path)
try
context = OperationContext.Current;
client = context.GetCallbackChannel<IClientContract>();
fsw = new System.IO.FileSystemWatcher();
fsw.Path = path;
fsw.IncludeSubdirectories = true;
fsw.Filter = "*.*";
fsw.Created += new System.IO.FileSystemEventHandler(GetUpdateFileFromFSW);
fsw.Deleted += new System.IO.FileSystemEventHandler(GetUpdateFileFromFSW);
fsw.EnableRaisingEvents = true;
catch (Exception ex)
throw new FaultException<ServerException>(new ServerException(
string.Format("An error occured !{0}{1}", Environment.NewLine, ex.Message)));
/// Retrieve the images which are in the specified folder during the startup.
/// <param name="path">The folder to get the images.</param>
public void GetImagesFromPathAtFirstLoad(string path)
var imgList = new List<CustomImage>();
CustomImage image = null;
foreach (var file in Directory.GetFiles(path))
if (Path.GetExtension(file).ToLower() == ".png" || Path.GetExtension(file).ToLower() == ".jpg" ||
Path.GetExtension(file).ToLower() == ".jpeg" || Path.GetExtension(file).ToLower() == ".bmp" ||
Path.GetExtension(file).ToLower() == ".gif")
using (var stream = File.OpenRead(file))
if (stream.Length > 0)
image = new CustomImage();
image.Name = Path.GetFileName(file);
image.FullPath = Path.GetFullPath(file);
image.Modification = CustomImage.ChangeType.Add;
byte[] array = GetByteFromImage(file);
image.Img = array;
imgList.Add(image);
if (client != null)
client.GetImagesFromServer(imgList);
/// Convert a file on a byte array.
/// <param name="file">The file to convert.</param>
/// <returns>An array of byte corresponding to the file.</returns>
private static byte[] GetByteFromImage(string file)
byte[] array;
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
BinaryReader reader = new BinaryReader(fs);
array = reader.ReadBytes((int)fs.Length);
reader.Close();
return array;
/// Method called when the FileSystemWatcher detects that a file has been added/deleted.
private void GetUpdateFileFromFSW(object sender, System.IO.FileSystemEventArgs e)
if (e.ChangeType == System.IO.WatcherChangeTypes.Created)
if (File.Exists(e.FullPath) && (Path.GetExtension(e.FullPath).ToLower() == ".png" ||
Path.GetExtension(e.FullPath).ToLower() == ".jpg"
|| Path.GetExtension(e.FullPath).ToLower() == ".jpeg"
|| Path.GetExtension(e.FullPath).ToLower() == ".bmp"
|| Path.GetExtension(e.FullPath).ToLower() == ".gif"))
using (var stream = File.OpenRead(e.FullPath))
image.Name = e.Name;
image.FullPath = e.FullPath;
byte[] array = GetByteFromImage(e.FullPath);
else if (e.ChangeType == System.IO.WatcherChangeTypes.Deleted)
image.Modification = CustomImage.ChangeType.Delete;
if (image != null)
SendUpdateToClient(image);
/// Inforl the client that an update is available.
/// <param name="image">The image that have been added/deleted.</param>
private void SendUpdateToClient(CustomImage image)
client.GetUpdateFromFileShare(image);