开发者

Static class method persistence in ASP.NET and IIS app pool

开发者 https://www.devze.com 2023-01-24 08:45 出处:网络
Would this static class method be called only once when IIS application starts when used in the context of an ASP.NET web app?

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开发者_如何学Python 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消