asp.net - Shared/Static variable in Global.asax isolated per request? -
i have asp.net web services share common helper class need instantiate 1 instance of per server. it's used simple translation of data, spend time during start-up loading things web.config file, etc. the helper class 100% thread-safe. think of simple library of utility calls. i'd make methods shared on class, want load initial configuration web.config. we've deployed web services iis 6.0 , using application pool, web garden of 15 workers.
i declared helper class private shared variable in global.asax, , added lazy load shared readonly property this:
private shared _helper myhelperclass public shared readonly property helper() myhelperclass if _helper nothing _helper = new myhelperclass() end if return _helper end end property
i have logging code in constructor myhelperclass()
, , shows constructor running each request, on same thread. i'm sure i'm missing key detail of asp.net msdn hasn't been helpful.
i've tried doing similar things using both application("helper")
, cache("helper")
, still saw constructor run each request.
it's not wise use application state unless absolutely require it, things simpler if stick using per-request objects. addition of state helper classes cause sorts of subtle errors. use httpcontext.current items collection , intialise per request. vb module want, must sure not make stateful.
Comments
Post a Comment