WCF/WF - Help in maintain global variable in WCF

Asked By MySpider DotNet on 17-Apr-10 03:07 AM
HI,

    I want to initilize the value one one in the WCF class. So i declar the value in the constructor on the WCF class. Now my problem is it get assigned every time client call the WCF class but i want to increment the value.
Venkat K replied to MySpider DotNet on 17-Apr-10 04:53 AM

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


MySpider DotNet replied to Venkat K on 17-Apr-10 07:27 AM
hey it much usefull for me, because currently am learning WCF, similar to the vairable is it possible to register the custom event in so that once i register that singleton class event it will automatically trigger once i submit some data to that class. Now my main aim is i have to register the event in the WCF, can you help me out in that