c# - Saving a cache object when a WCF webservice is ended -


i have simple wcf webservice 2 methods : 1 save/updates obect in cache , other 1 deletes it. how can save object when close webservice server.

using cacheitemremovedcallback doesn't work because object removed everytime update it. using global.asax.cs.application_end() doesn't work because cache cleared time here. using dispose() method method doesn't work because called every time call has finished.

[servicecontract] public class webservice {     [operationcontract]     public void test(string message)     {         list<string> logs;          logs = httpruntime.cache.get("logmessages") list<string>;         if (logs == null)         {             logs = new list<string>();             logs.add(message);         }         else logs.add(message);          httpruntime.cache.insert("logmessages", logs, null, cache.noabsoluteexpiration, cache.noslidingexpiration, cacheitempriority.notremovable, null);     }      [operationcontract]     public void writetofile()     {         list<string> logs;          logs = httpruntime.cache.get("logmessages") list<string>;         if (logs == null)         {             string filename = datetime.now.tostring("yyyy_mm_dd_hh_mm_ss_fff");             system.threading.tasks.task.factory.startnew(() =>             {                 //any method of writing object disk                 httpruntime.cache.remove("logmessages");             });         }     } } 

generally, if need when wcf service starts or stops should done extending servicehostfactory.

check servicehost onclose or onclosing event need.

public class derivedhost : servicehost {    public derivedhost( type t, params uri baseaddresses ) :       base( t, baseaddresses ) {}     protected override void onclose(system.timespan timeout)    {        ...         base.onclose(timeout);    }     protected override void onclosing()    {        ...         base.onclosing();    } } 

you can implement own instance provider , use releaseinstance method.

public class myinstanceproviderbehavior : iinstanceprovider {    ...    #region iinstanceprovider members        public void releaseinstance(instancecontext instancecontext, object instance)   {      ...   }    #endregion } 

for more information wcf extensibility, take @ carlos figueira blog.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -