none
Static variables RRS feed

  • Question

  • We have a business logic which leans on static variables. Our original product was Windows application but now we would like to use the same business libraries in our web applications – mainly WCF services which are used from Silverlight and Windows Phone clients.

    The problem is that ASP.NET/IIS stores these static variables per application and holds their value between different calls.

    For example when Person A makes a call and set Static value to X. While the method is processing Person B makes another call to the same service and changes Static value to Y. Now both persons have same value even they are totally different users using totally different sessions and making a call which are not related to each other.

    Is there any way to isolate this static variable in a way that every call has its own? Maybe some setting or workaround in IIS?

    Friday, October 5, 2012 12:56 PM

Answers

  • Sorry, a rewrite is necessary. You can use overridden properties to return a value from the current HttpContext.  ThreadStatic won't work as ASP.Net application pool reuse worker threads.


    Visual C++ MVP

    • Marked as answer by MikaMat Tuesday, October 9, 2012 5:11 AM
    Monday, October 8, 2012 2:20 PM

All replies

  • That is totally what static variable is not for. Is there any reason why you cannot use IIS's session object via ASP.Net? If you need help in that you can visit forums.asp.net.



    Visual C++ MVP

    Friday, October 5, 2012 3:11 PM
  • Actually we are using sessions when we can. The problems is that we would like to re-use our current business libraries which are huge and these libraries rely on some static variables. We know this isn’t really the best practise on web products but if there is some handy workaround we would like to try it instead of rewriting the business libraries.

    Monday, October 8, 2012 7:57 AM
  • Sorry, a rewrite is necessary. You can use overridden properties to return a value from the current HttpContext.  ThreadStatic won't work as ASP.Net application pool reuse worker threads.


    Visual C++ MVP

    • Marked as answer by MikaMat Tuesday, October 9, 2012 5:11 AM
    Monday, October 8, 2012 2:20 PM