间歇性无法加载文件或程序集/ PolicyExceptions

时间:2010-06-04 10:56:20

标签: asp.net iis-7 windows-server-2008

我们会在.NET 3.5 Web应用程序中间歇性地收到类似的错误:

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="*"/>

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

在这种情况下,它指向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

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

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

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

  • 我的DLL显示为x86(编译为AnyCPU,在x64 w3wp中运行),所有其他模块显示为x64。
  • 缺少IESHIMS.DLL和LINKINFO.DLL - 根据Google的说法,这些似乎都是红色的鲱鱼。

第三台服务器显示相同,但​​错过LINKINFO.DLL

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

aspnet_regiis -ga [user]

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

通过运行processmonitor,我可以看到它现在使用C:\ Users \ TEMP \ AppData \ Local \ Temp目录用于各种临时文件 - 其他的不使用任何此类目录。到目前为止,我将让它以这种方式运行,看看这是否会发生任何变化。我有点怀疑第三台服务器没有出现问题,但仍然将“加载用户配置文件”设置为相同的值,为假。

我也尝试在所有三台服务器上运行Fuslogvw,将绑定失败记录到磁盘。所有三个服务器都为VJSharpCodeProvider和CppCodeProvider报告相同的绑定错误,但这些似乎也是正常的,可以通过在构建期间不定义DEBUG和TRACE常量来解决。

我们在每台服务器上运行大约500个网站(相同,负载均衡),其中50个处于中等负载状态,但是在重负载和最小负载下都会出现问题。

现在我正在等待错误再次发生,所以我希望看到一个模式并确定“加载用户配置文件”是否可以缓解这个问题。在此期间的任何建议都将非常受欢迎!此外,我不明白缺乏“加载用户配置文件”会导致这样的问题?更进一步,它似乎如何在R2上工作但在2008年没有?

谢谢!

1 个答案:

答案 0 :(得分:1)

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

您可以尝试使用<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>