You can use Static Variables in WCF service to retain the value for each call. You can hook a handler to the ServiceHost.OnOpening event that would set the initial value of this variable, and that handler could spawn a thread to use the file watcher.
The example below uses a self-hosted service to show this.
//Static Variables in a WCF Service
public class Post2331848
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string GetString();
}
public class Service : ITest
{
public static string TheString;
public string GetString()
{
return TheString;
}
}
static void WatcherThreadProc()
{
Console.WriteLine("Started the watcher thread");
int i = 0;
while (true)
{
Thread.Sleep(5000);
i++;
Service.TheString = "The modified string " + i;
}
}
static Thread watcherThread = null;
static void host_Opening(object sender, EventArgs e)
{
Service.TheString = "This is the original string";
watcherThread = new Thread(new ThreadStart(WatcherThreadProc));
watcherThread.Start();
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
host.Opening +=
new EventHandler(host_Opening);
host.Open();
Console.WriteLine("Host opened");
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
ITest proxy = factory.CreateChannel();
Console.WriteLine("Created the channel");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(proxy.GetString());
Thread.Sleep(1800);
}
((
IClientChannel)proxy).Close();
factory.Close();
Console.WriteLine("Press ENTER to close service");
Console.ReadLine();
if (watcherThread != null)
{
watcherThread.Abort();
}
host.Close();
}
}
Thanks