Click here to Skip to main content
12,759,647 members (37,275 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 25 Mar 2008

Keep Web Service Objects States

, 26 Mar 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
keep web service running without users


This service keep one web service instance live for a long time.


I recently found here on code project article that explaining "simulation of windows service" by using cache expiring event. You can read it here. And then I came to idea to use it keep my web service alive long time without users.

Using the code

You should make pattern for hitting page as described in article and in your web service implement singleton pattern. Code described in article simulates timer that hitting page every minute. As you can make instance of your web service in you web page first time you visit it and then every next hit will return your instance and will keep your web service alive. So that means that you can use for example timers or other object in your service without fear of loosing their states.

Implement singleton to be sure that you will not make thousands of instances.

    static WebService instance = null;
    static readonly object singletonSync = new object();
    public static WebService Instance
            lock (singletonSync)
                if (instance == null)
                    instance = new WebService();
                return instance;
// next code goes to global asax 

protected void Application_BeginRequest(Object sender, EventArgs e)
        if (HttpContext.Current.Request.Url.ToString() == defaultUrl)

    private void NewCacheEntry()
        if (null != HttpContext.Current.Cache[newCacheItemKey])

            new CacheItemRemovedCallback(CacheItemRemoved));


    public void CacheItemRemoved(string key, object value, CacheItemRemovedReason reason)
    private void CliDownloadThePage()
        System.Net.WebClient client = new System.Net.WebClient();


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Stojan Trajkovic
Software Developer (Junior)
Yugoslavia Yugoslavia
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionCoke? Pin
Mladen Jankovic26-Mar-08 7:44
memberMladen Jankovic26-Mar-08 7:44 
Ti li si, brate? Smile | :)

Mostly, when you see programmers, they aren't doing anything. One of the attractive things about programmers is that you cannot tell whether or not they are working simply by looking at them. Very often they're sitting there seemingly drinking coffee and gossiping, or just staring into space. What the programmer is trying to do is get a handle on all the individual and unrelated ideas that are scampering around in his head. (Charles M Strauss)

GeneralRe: Coke? Pin
Stojan Trajkovic26-Mar-08 23:45
memberStojan Trajkovic26-Mar-08 23:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 26 Mar 2008
Article Copyright 2008 by Stojan Trajkovic
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid