ASP.NET编译和回收后Could not load App_Web_*.dll异常如何解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (197)

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

  • 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"; %>

我们使用.NET 4.0(调试)IIS6和Windows Server 2003上与“FooService接口的Web和HOSTFILE呼叫条目通过Web服务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中),但为什么它要App_Web_*.dll在回收(3.)之后加载旧的(删除的)呢?
  • 使用完全相同的内联代码FooService.svc而不是单独的代码FooService.cs没有这个问题(?)
  • 删除静态引用FooEngine也会导致此问题消失(?)
提问于
用户回答回答于

它必须是一个编译器问题,就像之前提到的那样。如果您无法访问iis,则可以修改自己的web.config文件以使用不同的临时目录。此修改将使iis重新编译您的代码并使用此新编译。

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

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

扫码关注云+社区

领取腾讯云代金券