【问题标题】:"Could not load App_Web_*.dll" Exception after ASP.NET compile & recycleASP.NET 编译和回收后“无法加载 App_Web_*.dll”异常
【发布时间】:2010-10-29 11:55:42
【问题描述】:

我最近遇到了以下结构的代码:

  • FooService.cs
  • FooService.svc
  • Default.aspx

文件内容:

[FooService.cs]

using System.ServiceModel;

namespace FooService
{
    [ServiceContract]
    public class FooService
    {
        static FooEngine engine = new FooEngine();

        [OperationContract]
        public string Foo()
        {
            return "bar";
        }
    }

    public class FooEngine
    {

    }
}

[FooService.svc]

<%@ ServiceHost Language="C#" Service="FooService.FooService" %>

[Default.aspx]

<%@ Page Language="C#" %>
<% var foo = "bar"; %>

我们在 Windows Server 2003 上使用 .Net 4.0(调试)和 IIS6,通过“fooservice”网络和主机文件条目通过http://fooservice/FooService.svcdefault.aspx 通过http://fooservice/ 调用网络服务。此时一切正常。

但是,经过以下步骤,

  1. 更改default.aspx中的代码,保存文件
  2. 加载http://fooservice/(触发 ASP.NET 编译)
  3. 回收应用程序池

http://fooservice/FooService.svc 的调用失败并引发以下异常

[FileNotFoundException: Could not load file or assembly 'App_Web_ynlv0b1k, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.]
   System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
   System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +39
   System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks) +132
   System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +144
   System.Reflection.Assembly.Load(String assemblyString) +28
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +208
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath) +1440
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +44
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +615

[ServiceActivationException: The service '/FooService.svc' cannot be activated due to an exception during compilation.  The exception message is: Could not load file or assembly 'App_Web_ynlv0b1k, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified..]
   System.Runtime.AsyncResult.End(IAsyncResult result) +679246
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +190
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.ExecuteSynchronous(HttpApplication context, String routeServiceVirtualPath, Boolean flowContext, Boolean ensureWFService) +234
   System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +355
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

这里到底发生了什么?

备注

  • 我知道 ASP.NET 正在编译和加载一个新的App_Web_*.dll(在第 2 步中),但为什么它在回收 (3.) 之后尝试加载旧的(已删除)App_Web_*.dll
  • FooService.svc 中使用完全相同的内联代码而不是单独的FooService.cs 不会出现此问题 (?)
  • 删除对FooEngine 的静态引用也会使这个问题消失 (?)

【问题讨论】:

  • 可能问题与 FooEngine 有关?如果您在示例中将 FooEngine 更改为对象,错误会重现吗?
  • 是的,FooEngine 是这里的问题。但我想知道为什么这是问题所在。
  • 你解决了吗?我面临同样的问题
  • @ShayErlichmen 不是真的,我们只是在构建过程中添加了 aspcompile。

标签: asp.net wcf iis


【解决方案1】:

我也遇到了同样的问题, 该应用程序是一个 Web 应用程序,因此完全预编译。 这为我解决了这个问题,但我不知道为什么:

<compilation debug="false" batch="false">  

我从这里得到了这个解决方案:http://web.archive.org/web/20140116171941/http://www.creative-jar.com/insights/labs/programming/could-not-load-file-or-assembly-app_web_/

【讨论】:

    【解决方案2】:

    这一定是编译器问题,正如之前提到的。如果您无权访问 iis,您可以修改自己的 web.config 文件以使用不同的临时目录。此修改将使 iis 重新编译您的代码并使用此新编译。

    <compilation tempDirectory="C:\...">
    

    iis_iusrs 用户必须对此新目录具有写入权限。

    【讨论】:

      【解决方案3】:

      代码不应为以下结构:

      • FooService.cs
      • FooService.svc
      • 默认.aspx

      但是如下:

      • App_Code/FooService.cs
      • FooService.svc
      • 默认.aspx

      请注意包含源代码的特殊App_Code 文件夹。

      回顾一下:

      ~/App_Code/FooService.cs:

      using System.ServiceModel;
      
      namespace FooService
      {
          [ServiceContract]
          public class FooService
          {
              static FooEngine engine = new FooEngine();
      
              [OperationContract]
              public string Foo()
              {
                  return "bar";
              }
          }
      
          public class FooEngine
          {
      
          }
      }
      

      ~/FooService.svc:

      <%@ ServiceHost Language="C#" Debug="true" Service="FooService.FooService" CodeBehind="~/App_Code/FooService.cs" %>
      

      ~/Web.config:

      <configuration>
        <system.web>
          <compilation debug="false" targetFramework="4.0" />
        </system.web>
      
        <system.webServer>
           <modules runAllManagedModulesForAllRequests="true"/>
        </system.webServer>
      
        <system.serviceModel>
          <behaviors>
            <serviceBehaviors>
              <behavior name="">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
              </behavior>
            </serviceBehaviors>
          </behaviors>
          <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
        </system.serviceModel>
      </configuration>
      

      ~/Default.aspx:

      <%@ Page Language="C#" %>
      <% var foo = "bar"; %>
      

      话虽如此,我建议您使用Web Applications instead of Web Sites 并预编译所有内容以避免此类问题。另一个好处是您不再需要在服务器上部署源代码。

      如果您使用预编译的 Web 应用程序,您的结构将如下所示:

      • bin/MyWebService.dll
      • FooService.svc
      • 默认.aspx

      这是我推荐给你的。

      【讨论】:

      • 我非常了解不同的 ASP.NET 设置和部署,并且显然在其他项目中使用它们。然而,我遇到了这种特殊的安排,并提出了这个问题,以了解它为什么会这样。
      • 如果您使用的是网站,所有源代码都应位于App_Code 文件夹中。这是你的情况吗?
      • 不是,但在这种情况下,将 FooService.cs 移动到 App_Code 文件夹不会改变任何内容 - 结果相同。
      【解决方案4】:

      我们已经看到,在某些情况下,应用程序回收从未真正执行。我已经在负载较重的大型网站上看到了这一点。 IIS 使用了一种非常激进的缓存机制,并且根据请求队列长度实际上不会强制完全回收。

      如果您看到此内容,请尝试执行 IISRESET /noforce。这应该告诉服务器真正回收所有东西。

      【讨论】:

      • 感谢您的回答。但是,我所描述的实际上每次都会发生,而不仅仅是在重负载下。我试过IISRESET /noforce:它会导致同样的错误。
      【解决方案5】:

      您可以将 IIS 应用程序池设置为在特定时间段或特定应用程序故障时回收。请检查以下可能对您有帮助的网址:

      http://forums.asp.net/p/1551597/3934757.aspx http://weblogs.asp.net/albertpascual/archive/2010/07/01/installing-asp-net-4-in-iis-6.aspx

      Gaurav Maniar MCP | MCSE | MCST | MCITP | ITILv3 认证

      【讨论】:

      • 这实际上会使情况变得更糟,因为服务会在第一次重置后立即变得不可用。
      • @josef,您检查过 URL 和回收应用程序池不会影响应用程序池不可用。
      【解决方案6】:

      这似乎是 Asp.Net 编译器的问题。它似乎没有在重新编译时使用新的 DLL 更新 WCF 的临时 ASP.NET 文件。您能否使用 batch=false 测试应用程序,即

      【讨论】:

        【解决方案7】:

        我的项目遇到了类似的问题,网站运行正常,但任何 svc 网络服务都出现了同样的错误。

        我尝试在 web.config 中更改以下行,但这没有帮助

            <compilation debug="false" batch="false"> 
        

        我找到的解决方案与应用程序的构建方式有关。

        在使用部署项目时,我发现取消选中该选项:

        当作库组件处理(去掉 App_Code.compiled 文件)

        在“输出程序集”页面中,然后重建和部署项目似乎对我有用。

        希望对其他人有所帮助。

        【讨论】:

          猜你喜欢
          • 2015-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多