0

Would this static class method be called only once when IIS application starts when used in the context of an ASP.NET web app?

public static class Licensing
{
    public static bool IsThisLicensed(ThisFeature)
    {
       return Licenser.FeatureEnabled(ThisFeature);
    }
}

We have a case wherein a call similar to this returns True (as it should) for a period of time and then subsequently returns false until IIS is restarted. Upon restart of IIS the value returns True again. That said, the timing of this behavior is not predictable.

We can't figure out why the value changes without a restart of IIS or recycle of the app pool. Our expectation was that this static method would be called once when the application is started and the value would be available application wide until the app was restarted.

I think this is similar to a previous post: but in this case we are working with a call to a method as opposed to a property.

Community
  • 1
  • 1
treltub
  • 1
  • 1
  • 2

1 Answers1

2

The example you posted is a method (not a property), and unless something is happening in Licenser.FeatureEnabled(ThisFeature), you are not setting anything. Data is only being returned.

In any case, if you had a static property that was in turn actually setting a value in some other static variable, it should be active until the app pool recycles. It sounds like you may have a bug in the call you are making.


edit to address the comment:

Just because the method is static doesn't mean it is only going to be called once. Static means that only 1 instance of the method is going to exist per app domain. So, anytime you call the method the code is going to be executed.

If it is returning true when IIS first starts and after a while it starts to return false, then there must be a bug somewhere in your logic. Perhaps you are losing state? Perhaps you are relying on something being available, and when the app pool recycles that value is lost? What is going on in Licenser.FeatureEnabled(ThisFeature)? Maybe if we can see that we can help identify the problem.

In any case, just making the method static doesn't mean it is going to cache the result of the first call.

  • I edited the post to more accurately reflect that this is a static call to a method as opposed to setting a property. We are ultimately setting a property but we've isolated the problem to this call. I just wasn't sure if the result of the call is retained after the first call due to the "static" qualifier. As far as we can tell, calling that same method outside of IIS returns the desired value consistently. Thanks for helping me clarify that. – treltub Nov 11 '10 at 16:20
  • 1
    Just to emphasize Joe's comment on app pool recycling. One reason you may be having this problem is that after awhile IIS will recycle the app pool, shutting down your application in the process – Anthony Nov 11 '10 at 16:36
  • The return value of the method is not going to be cached anytime you call that method, it is going to be executed and a value returned. Now, one thing that could fix it us storing the value n a variable. Once inter to work, and have a pc in front of me I will edit my answer to show you what I am talking about. –  Nov 11 '10 at 16:36
  • Licenser.FeatureEnabled(ThisFeature) is a supporting library. This dll is used by both the web site and a web service (same dll but individual copies in the bin directory of each) in the same app pool. We are seeing the same behavior from the web service when this condition exists. Restarting IIS resolves this condition in both. I think we need to step into the Licensing library as Joe recommends. There must be SOMETHING in memory that gets flushed with an IIS restart or app pool recycle. – treltub Nov 11 '10 at 19:04
  • @treltub That sounds like what is happening. Let me know how it goes –  Nov 11 '10 at 21:06
  • Also, to be clear, having a static only means that it is static on a per AppDomain basis, so if you have multiple ASP.NET applications running in the same Application Pool, you will have multiple instances of that class, so it is NOT true to be unique per AppPool. Additionally AppDomins are recycled in situations other than full AppPool recycle, for example if you modify the Web.config file it will create a new AppDomain and hence a new static would exist, or if you change App_code directory, or add a new DLL in the /bin directory, etc. – Carlos Aguilar Mares Nov 11 '10 at 21:55