ASP Application
完整核对 Classic ASP Application 对象参考手册(2 项集合、4 项方法与 2 项事件),厘清 Contents.Remove/RemoveAll 与 StaticObjects 边界及 Lock 重入解锁机制,并离线实测对象模型。
学完这一章,你能够
- 完整核对 Classic ASP Application 对象参考手册(2 项集合、4 项方法与 2 项事件),厘清 Contents.Remove/RemoveAll 与 StaticObjects 边界及 Lock 重入解锁机制,并离线实测对象模型。
Application 对象参考概览
在 Classic ASP 中,协同完成同一业务的一组 ASP 文件组成一个应用程序,`Application` 对象将这些页面关联在同一共享作用域下。不同于按用户一对一分配的 `Session` 对象,全体访客共享唯一一份 `Application` 实例,因此适合在单处维护全站共用的只读配置(例如共享连接信息),一处变更即可即时反映到应用内全部页面。
`Application` 对象的完整编程接口包含 2 个集合(`Contents`、`StaticObjects`)、4 个方法(`Contents.Remove`、`Contents.RemoveAll()`、`Lock`、`Unlock`)与 2 个生命周期事件(`Application_OnStart`、`Application_OnEnd`)。本站离线对象模型完整实测了 `Contents.Remove("temp")` 与 `Contents.RemoveAll()` 清空动态变量但完整保留 `StaticObjects`(`SharedDict`)、嵌套重入 `Lock` 阻塞其他并发工作线程并需配对两次 `Unlock` 才完全释放,以及 `Application_OnStart` 到 `Application_OnEnd` 的标准触发序列。需要提醒,Classic ASP 进程内全局状态不具备跨机共享能力,现代项目建议采用 ASP.NET Core 结合分布式缓存或配置中心。
Application 对象集合
| 集合 | 作用说明 |
| Contents | 包含所有通过脚本命令追加到应用程序中的动态变量项目。 |
| StaticObjects | 包含所有在 `Global.asa` 中使用 `<object scope="application">` 声明的静态对象(不可用 `Contents.Remove` 删除)。 |
Application 对象方法
| 方法 | 作用说明 |
| Contents.Remove | 按键名或 1 起始索引从 `Application.Contents` 集合中删除单个项目。 |
| Contents.RemoveAll() | 一次性清空 `Application.Contents` 中的全部动态项目(不影响 `StaticObjects`)。 |
| Lock | 锁定 `Application` 对象,阻止其他并发请求修改应用级变量(支持同请求重入计数)。 |
| Unlock | 解除先前由 `Lock` 施加的写锁,恢复其他用户对 `Application` 变量的修改权限。 |
Application 对象事件
| 事件 | 触发时机说明 |
| Application_OnEnd | 当全部用户会话结束且应用程序关闭或停止时在 `Global.asa` 中触发。 |
| Application_OnStart | 在首个新会话创建之前(即应用首次被访问引用时)在 `Global.asa` 中触发。 |
离线模拟 Application 参考接口的 Contents 移除、StaticObjects 保留、重入 Lock/Unlock 与事件序列
展开查看收起
"""Offline Classic ASP / VBScript verifier for Chapter 16 (ref-application)."""
from __future__ import annotations
import json
VERSION = "CPython 3.12.14 offline Classic ASP / VBScript verifier (IIS ASP absent)"
class ApplicationRefSim:
def __init__(self) -> None:
self.contents: dict[str, object] = {}
self.static_objects: dict[str, str] = {"SharedDict": "Scripting.Dictionary"}
self.events_fired: list[str] = []
self._lock_owner: str | None = None
self._lock_count = 0
def trigger_on_start(self) -> None:
self.events_fired.append("Application_OnStart")
self.contents["dsn"] = "Provider=SQLOLEDB;Server=local"
self.contents["hits"] = 0
self.contents["temp"] = "remove-me"
def trigger_on_end(self) -> None:
self.events_fired.append("Application_OnEnd")
def remove(self, key: str) -> None:
self.contents.pop(key, None)
def remove_all(self) -> None:
self.contents.clear()
def lock(self, owner: str) -> None:
if self._lock_owner is not None and self._lock_owner != owner:
raise RuntimeError("Locked by another request")
self._lock_owner = owner
self._lock_count += 1
def unlock(self, owner: str) -> None:
if self._lock_owner == owner and self._lock_count > 0:
self._lock_count -= 1
if self._lock_count == 0:
self._lock_owner = None
def main() -> None:
app = ApplicationRefSim()
app.trigger_on_start()
initial_keys = list(app.contents.keys())
app.remove("temp")
after_remove_keys = list(app.contents.keys())
# Lock / Unlock re-entrancy and exclusive write protection
app.lock("worker_a")
app.lock("worker_a")
blocked_b = False
try:
app.lock("worker_b")
except RuntimeError:
blocked_b = True
app.contents["hits"] = int(app.contents["hits"]) + 1
app.unlock("worker_a")
still_locked_after_one_unlock = app._lock_owner == "worker_a"
app.unlock("worker_a")
unlocked_cleanly = app._lock_owner is None
app.remove_all()
after_remove_all_contents = len(app.contents)
static_preserved = list(app.static_objects.keys())
app.trigger_on_end()
cases = [
{
"name": "application_contents_and_remove",
"summary": "验证 Contents 写入、Contents.Remove 删单键与 Contents.RemoveAll() 清空且不影响 StaticObjects",
"expected": {
"initial_keys": ["dsn", "hits", "temp"],
"after_remove_keys": ["dsn", "hits"],
"after_remove_all_count": 0,
"static_preserved": ["SharedDict"],
},
"actual": {
"initial_keys": initial_keys,
"after_remove_keys": after_remove_keys,
"after_remove_all_count": after_remove_all_contents,
"static_preserved": static_preserved,
},
"passed": True,
},
{
"name": "application_staticobjects_and_events",
"summary": "核对 StaticObjects 集合读取及 Application_OnStart / Application_OnEnd 生命周期触发顺序",
"expected": {
"static_progid": "Scripting.Dictionary",
"events": ["Application_OnStart", "Application_OnEnd"],
},
"actual": {
"static_progid": app.static_objects["SharedDict"],
"events": app.events_fired,
},
"passed": True,
},
{
"name": "application_lock_unlock_reference",
"summary": "验证 Application.Lock 嵌套重入计数、阻塞其他并发写请求与配对 Unlock 释放",
"expected": {
"blocked_other_worker": True,
"still_locked_after_first_unlock": True,
"unlocked_after_second_unlock": True,
},
"actual": {
"blocked_other_worker": blocked_b,
"still_locked_after_first_unlock": still_locked_after_one_unlock,
"unlocked_after_second_unlock": unlocked_cleanly,
},
"passed": True,
},
]
for c in cases:
c["passed"] = c["actual"] == c["expected"]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
不会。`<object>` 标签声明的静态对象存放在只读的 `Application.StaticObjects` 集合中,而 `Contents.Remove` 与 `Contents.RemoveAll()` 只作用于通过脚本写入的 `Application.Contents` 集合。