CLR何时尝试加载引用的程序集? [英] When does the CLR try to load a referenced assembly?

查看:102
本文介绍了CLR何时尝试加载引用的程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

问题是:对于IIS 6,我们通过调用WMI/Metabase API来创建virt dirs,对于IIS 7,有一个更好的API:Microsoft.Web.Administration,但其程序集仅在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.

天真的方法:

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

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

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?

我想,如果没有CLR/.NET规范保证一定的确定性,测试就无济于事.

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.

推荐答案

我无法像规范中所指出的那样确定何时必须加载组件,以及何时不应该加载程序集.但是,根据

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 (启动时加载较少的模块"部分)

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

和本书摘录,网址为www.informit.com/articles/article.aspx?p=30601&seqNum=5(摘自"Essential .NET,第一卷:公共语言运行时").

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").

CLR的JIT仅在需要编译方法时才加载所需的程序集.因此,您应该将对Microsoft.Web.Administration ...的任何使用都移至单独的方法,仅当您确信该程序集存在于系统上时才调用该方法.也就是说,

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 ....
   }

这篇关于CLR何时尝试加载引用的程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆