首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ASP.NET编译和回收后出现“无法加载App_Web_*.dll”异常

ASP.NET编译和回收后出现“无法加载App_Web_*.dll”异常
EN

Stack Overflow用户
提问于 2010-10-29 19:55:42
回答 7查看 9.7K关注 0票数 17

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

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

文件内容:

[FooService.cs]

代码语言:javascript
复制
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]

代码语言:javascript
复制
<%@ ServiceHost Language="C#" Service="FooService.FooService" %>

[Default.aspx]

代码语言:javascript
复制
<%@ Page Language="C#" %>
<% var foo = "bar"; %>

我们在Windows Server2003上使用.Net 4.0 (调试)和IIS6,通过一个“fooservice”和一个主机文件条目通过http://fooservice/FooService.svc调用web服务,通过http://fooservice/调用default.aspx。在这一点上,一切都运行得很好。

但是,在以下步骤之后,

  1. 在default.aspx中更改代码,保存文件
  2. Load http://fooservice/ (triggers ASP.NET app pool

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

代码语言:javascript
复制
[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不会出现此问题(?)

<代码>H148删除对<代码>D49的静态引用也可以解决此问题(?)<代码>H250<代码>F251

EN

回答 7

Stack Overflow用户

发布于 2011-12-16 05:39:55

我只是遇到了同样的问题,这个应用程序是一个web应用程序,所以完全预编译了。这为我解决了这个问题,但我不确定为什么:

代码语言:javascript
复制
<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_/

票数 19
EN

Stack Overflow用户

发布于 2011-12-12 08:23:59

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

代码语言:javascript
复制
<compilation tempDirectory="C:\...">

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

票数 3
EN

Stack Overflow用户

发布于 2010-11-16 04:04:26

代码不应为以下结构:

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

但以下内容:

  • App_Code/FooService.cs
  • FooService.svc
  • Default.aspx

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

因此,简单回顾一下:

~/App_Code/FooService.cs

代码语言:javascript
复制
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

代码语言:javascript
复制
<%@ ServiceHost Language="C#" Debug="true" Service="FooService.FooService" CodeBehind="~/App_Code/FooService.cs" %>

~/Web.config

代码语言:javascript
复制
<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

代码语言:javascript
复制
<%@ Page Language="C#" %>
<% var foo = "bar"; %>

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

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

  • bin/MyWebService.dll
  • FooService.svc
  • Default.aspx

这就是我推荐给你的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4051576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档