"无法加载App_Web _*.dll"ASP.NET编译和回收后的异常

Jos*_*ger 17 asp.net iis wcf

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

  • 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
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

[FooService.svc]

<%@ ServiceHost Language="C#" Service="FooService.FooService" %>
Run Code Online (Sandbox Code Playgroud)

[Default.aspx]

<%@ Page Language="C#" %>
<% var foo = "bar"; %>
Run Code Online (Sandbox Code Playgroud)

我们使用.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
Run Code Online (Sandbox Code Playgroud)

到底发生了什么?

备注

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

agr*_*ath 19

我刚遇到同样的问题,该应用程序是一个Web应用程序,因此完全预编译.这为我修好了,但我不确定原因:

<compilation debug="false" batch="false">  
Run Code Online (Sandbox Code Playgroud)

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


Dar*_*rov 2

代码不应是以下结构:

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

但以下情况:

  • 应用程序代码/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
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

~/FooService.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="FooService.FooService" CodeBehind="~/App_Code/FooService.cs" %>
Run Code Online (Sandbox Code Playgroud)

~/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>
Run Code Online (Sandbox Code Playgroud)

~/Default.aspx:

<%@ Page Language="C#" %>
<% var foo = "bar"; %>
Run Code Online (Sandbox Code Playgroud)

话虽这么说,我建议您使用Web 应用程序而不是网站,并预编译所有内容以避免此类问题。另一个优点是您不再需要在服务器上部署源代码。

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

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

这就是我要推荐给你的。