Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Could not load App_Web_*.dll" Exception after ASP.NET compile & recycle

Tags:

asp.net

iis

wcf

I recently came across code of the following structure:

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

Content of the files:

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

We're using .Net 4.0 (Debug) and IIS6 on Windows Server 2003 with a 'fooservice' web and a hostfile entry to call the web service via http://fooservice/FooService.svc and default.aspx via http://fooservice/. Everything works perfectly at this point.

However, after the following steps,

  1. Change code in default.aspx, save file
  2. Load http://fooservice/ (triggers ASP.NET compile)
  3. Recycle app pool

a call to http://fooservice/FooService.svc fails and throws the following exception

[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

What exactly is going on here?

Remarks

  • I understand that ASP.NET is compiling and loading a new App_Web_*.dll (during step 2), but why is it trying to load the old (deleted) App_Web_*.dll after the recycle (3.)?
  • using the exact same code inline in FooService.svc instead of a separate FooService.cs does not have this problem (?)
  • removing the static reference to the FooEngine also makes this problem go away (?)
like image 497
Josef Pfleger Avatar asked Oct 29 '10 11:10

Josef Pfleger


3 Answers

I just had the same problem, The app was a web application, so fully precompiled. This fixed it for me, but I'm not sure why:

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

I got this solution from here: http://web.archive.org/web/20140116171941/http://www.creative-jar.com/insights/labs/programming/could-not-load-file-or-assembly-app_web_/

like image 162
agrath Avatar answered Oct 15 '22 14:10

agrath


It must be a compiler issue, as it was mentioned before. If you don't have access to the iis, you can modify your own web.config file to use a different temp directory. This modification will make the iis to recompile your code and use this new compilation.

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

The iis_iusrs user must have write access to this new directory.

like image 24
NOWAN Avatar answered Oct 15 '22 14:10

NOWAN


The code should not be the following structure:

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

But the following:

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

Notice the special App_Code folder which contains the source code.

So to recap:

~/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"; %>

This being said I would recommend you using Web Applications instead of Web Sites and precompile everything to avoid those kind of problems. Another advantage is that you no longer need to deploy the source code on the server.

If you use a percompiled web application your structure will be the following:

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

This is what I would recommend you.

like image 2
Darin Dimitrov Avatar answered Oct 15 '22 15:10

Darin Dimitrov