CLR 何时尝试加载引用的程序集?

发布于 2024-08-15 11:52:10 字数 598 浏览 3 评论 0原文

我想编写一个小型安装程序应用程序来安装网站并创建 IIS 虚拟目录。该应用程序应在 Windows XP/Server 2003 (IIS 6) 以及 Vista/2008 (IIS 7) 上运行。

问题是:对于 IIS 6,我们通过调用 WMI/Metabase API 创建虚拟目录,对于 IIS 7,有一个更好的 API:Microsoft.Web.Administration,但其程序集仅在 IIS 7 系统上可用。

天真的方法:

...
if (OperatingSystem == old)
{
    call metabase API...
}
else
{
    call Microsoft.Web.Administration...
}
...

很好,不是吗?但是,我如何确保在尝试加载 Microsoft.Web.Administration DLL 时不会在旧系统上崩溃?或者程序集是在第一次使用时刚刚加载的吗?何时首次使用调用程序集的方法?

我想,如果没有 CLR/.NET 规范保证某些确定性,测试就没有帮助。

我真的很期待听到您对此主题的经验、提示或解决方案。到目前为止,我还没有在网络上找到任何可远程使用的内容。

I want to write a small installer app that installs a web site and creates IIS virtual directories. The app should run on Windows XP/Server 2003 (IIS 6) as well as on Vista/2008 (IIS 7).

The problem is: for IIS 6 we create virt dirs by calling WMI/Metabase API, for IIS 7 there is a much better API: Microsoft.Web.Administration, but its assembly is available only on IIS 7 systems.

Naive approach:

...
if (OperatingSystem == old)
{
    call metabase API...
}
else
{
    call Microsoft.Web.Administration...
}
...

Nice, isn't it? But how can I make sure that this does not crash on a old system just while trying to load the Microsoft.Web.Administration DLL? Or is an assembly just loaded, when it is first used? When a method that is calling into the assembly is first used?

I guess, testing does not help without some determinism being guaranteed by CLR/.NET spec.

I am really looking forward to hearing your experiences, hints or solutions for this topic. I have not found anything remotely usable on the web so far.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一萌ing 2024-08-22 11:52:10

我无法在规范中找到明确的答案,说明何时必须加载程序集,何时不得加载程序集。但是,根据

http://msdn.microsoft.com/en-us/ magazine/cc163655.aspx(“启动时加载更少的模块”部分)

以及 www.informit.com/articles/article.aspx?p=30601&seqNum=5 上的书籍摘录(摘自“Essential .NET”) ,第一卷:公共语言运行时”)。

仅当需要编译方法时,CLR 的 JIT 才会加载所需的程序集。因此,您应该将 Microsoft.Web.Administration... 的任何使用转移到单独的方法中,仅当您确信系统上存在该程序集时才调用该方法。那是,

   setup()
   { 
       if ( Operating.System == Old )
          call metabase API
       else
          doIIS7Setup()
   }

   void doIIS7Setup()
   {
     call Microsoft.Web.Administration ....
   }

I have not been able to find the definitive answer as in a specification stating when assemblies must and must not be loaded. However, according to

http://msdn.microsoft.com/en-us/magazine/cc163655.aspx (section "Load Fewer Modules at Startup")

and the book extract at www.informit.com/articles/article.aspx?p=30601&seqNum=5 (extract from "Essential .NET, Volume I: The Common Language Runtime").

the JIT of the CLR will load a needed assembly only when needed to compile a method. Thus, you should move any use of Microsoft.Web.Administration... to a seperate method which is only called when your confident that the assembly exists on the system. That is,

   setup()
   { 
       if ( Operating.System == Old )
          call metabase API
       else
          doIIS7Setup()
   }

   void doIIS7Setup()
   {
     call Microsoft.Web.Administration ....
   }
苏辞 2024-08-22 11:52:10

就个人而言,我不会尝试依赖 JIT 的任何内置行为,而是将对 Microsoft.Web.Administration 的依赖关系完全移至另一个程序集。

然后,在调用程序集中的某个位置,我将检查 %systemroot%\inetsrv\Microsoft.Web.Administration.dll 是否存在。如果是这样,那么我会假设我正在使用托管接口并调用程序集;如果没有,我会恢复到元数据库 API。

Personally, rather than trying to rely on any inbuilt behaviour of the JIT, I'd move the dependency on Microsoft.Web.Administration to another assembly altogether.

Then, somewhere in the calling assembly, I'd check to see if %systemroot%\inetsrv\Microsoft.Web.Administration.dll is present. If so, then I'd assume I'm using the managed interface and call the assembly; if not, I'd revert to the metabase API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文