代码之家  ›  专栏  ›  技术社区  ›  Mark S. Rasmussen

间歇性无法加载文件或程序集/策略异常

  •  1
  • Mark S. Rasmussen  · 技术社区  · 14 年前

    间歇性地,我们会从.NET3.5Web应用程序中得到如下错误:

    Exception:
    System.Configuration.ConfigurationErrorsException: Could not load file or assembly 'itextsharp, Version=4.1.2.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca' or one of its dependencies. Failed to grant permission to execute. (Exception from HRESULT: 0x80131418) (C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Config\web.config line 59) ---> System.IO.FileLoadException: Could not load file or assembly 'itextsharp, Version=4.1.2.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca' or one of its dependencies. Failed to grant permission to execute. (Exception from HRESULT: 0x80131418) File name: 'itextsharp, Version=4.1.2.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca' ---> System.Security.Policy.PolicyException: Execution permission cannot be acquired.
       at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Boolean checkExecutionPermission)
       at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Int32& securitySpecialFlags, Boolean checkExecutionPermission)
       at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
       at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
       at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
       at System.Reflection.Assembly.Load(String assemblyString)
       at System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective)
    
    
       --- End of inner exception stack trace ---
       at System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective)
       at System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory()
       at System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai)
       at System.Web.Configuration.AssemblyInfo.get_AssemblyInternal()
       at System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig)
       at System.Web.Compilation.WebDirectoryBatchCompiler..ctor(VirtualDirectory vdir)
       at System.Web.Compilation.BuildManager.BatchCompileWebDirectoryInternal(VirtualDirectory vdir, Boolean ignoreErrors)
       at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
       at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
       at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)
       at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert)
       at System.Web.Compilation.BuildManager.GetCompiledType(String virtualPath)
       at System.Web.Script.Services.WebServiceData.GetWebServiceData(HttpContext context, String virtualPath, Boolean failIfNoData, Boolean pageMethods, Boolean inlineScript)
       at System.Web.Script.Services.RestHandler.CreateHandler(HttpContext context)
       at System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated)
       at System.Web.HttpApplication.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
    
    Inner exception:
    System.IO.FileLoadException: Could not load file or assembly 'itextsharp, Version=4.1.2.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca' or one of its dependencies. Failed to grant permission to execute. (Exception from HRESULT: 0x80131418) File name: 'itextsharp, Version=4.1.2.0, Culture=neutral, PublicKeyToken=8354ae6d2174ddca' ---> System.Security.Policy.PolicyException: Execution permission cannot be acquired.
       at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Boolean checkExecutionPermission)
       at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Int32& securitySpecialFlags, Boolean checkExecutionPermission)
       at System.Reflection.Assembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, Assembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection)
       at System.Reflection.Assembly.InternalLoad(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
       at System.Reflection.Assembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
       at System.Reflection.Assembly.Load(String assemblyString)
       at System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective)
    

    web.config第59行为:

    <add assembly="*"/>
    

    当这些发生时,这些站点将一直运行,直到我们回收应用程序池。网站可能会在这之前运行几天/几周,或者在一小时内运行两次。我无法将此精确定位到系统中的任何特定请求/功能。

    在本例中,它指向itextsharp,但它随机指向应用程序引用的任何程序集,包括内部程序集和外部程序集。

    运行caspol将验证dll是否具有完全信任权限:

    C:\Windows\Microsoft.NET\Framework64\v2.0.50727>caspol -rsg D:\...\bin\itextsharp.dll
    Microsoft (R) .NET Framework CasPol 2.0.50727.3053
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    Level = Enterprise
    
    Code Groups:
    
    1.  All code: FullTrust
    
    
    Level = Machine
    
    Code Groups:
    
    1.  All code: Nothing
       1.1.  Zone - MyComputer: FullTrust
    
    
    Level = User
    
    Code Groups:
    
    1.  All code: FullTrust
    
    Success
    

    我们的应用程序运行在三台服务器上,其中两台在服务器2008 Web x64上,而第三台运行服务器2008 R2 Web x64,所有服务器都安装了.NET 3.5,没有安装.NET 4.0。

    问题 只发生 在运行2008非R2的前两个上。

    在所有三台服务器上运行depends.exe可为非R2服务器提供相同的结果:

    • 我的dll显示为x86(编译为anycpu,在x64 w3wp中运行),所有其他模块显示为x64。
    • 缺少ieshims.dll和linkinfo.dll-根据谷歌的说法,这两个文件都是过眼云烟。

    第三台服务器显示的是相同的,除了它 没有 linkinfo.dll小姐

    所有服务器都在已被授予必要权限的自定义域帐户下运行iis7(r2 1为7.5):

    aspnet_regiis -ga [user]
    

    加载用户配置文件设置为 在所有三台服务器上。我在一台故障服务器上尝试将此设置为true,根据: IIS7 Failed to grant minimum permission requests

    通过运行processmonitor,我可以看到它现在正在对各种临时文件使用c:\users\temp\appdata\local\temp目录,而其他文件则没有使用任何这样的目录。到目前为止,我会让它以这种方式运行,看看这是否会改变什么。不过,考虑到第三台服务器没有显示出问题,但仍将“加载用户配置文件”设置为相同的值false,我对此表示怀疑。

    我还尝试在所有三台服务器上运行fuslogvw,将绑定失败记录到磁盘。所有三台服务器都报告了vjsharpcodeprovider和cppcodeprovider的相同绑定错误,但这些错误看起来也是正常的,可以通过在生成期间不定义调试和跟踪常量来解决。

    我们在每台服务器上运行大约500个网站(相同,负载平衡),其中50个处于中等负载下,无论是在高负载下还是在最小负载下都会出现问题。

    现在我正在等待错误再次发生,所以我希望能够看到一个模式,并确定“加载用户配置文件”是否缓解了这个问题。在此期间任何建议都是非常欢迎的!另外,我不明白缺少“加载用户配置文件”会导致这样的问题吗?更进一步说,它在R2上看起来是如何工作的,但在普通的2008上却不是这样?

    谢谢!

    1 回复  |  直到 7 年前
        1
  •  1
  •   James Santiago    14 年前

    运行时如何定位程序集以及它对负载平衡环境的影响可能是一个问题。

    您可以尝试使用 <probing> 元素。

    <configuration>
       <runtime>
          <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
             <probing privatePath="bin;bin2\subbin;bin3"/>
          </assemblyBinding>
       </runtime>
    </configuration>
    

    编辑:

    试试这个:

    <configuration>
       <runtime>
          <disableCachingBindingFailures enabled="1" />
       </runtime>
    </configuration>