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,
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
App_Web_*.dll
(during step 2), but why is it trying to load the old (deleted) App_Web_*.dll
after the recycle (3.)?FooService.svc
instead of a separate FooService.cs
does not have this problem (?)FooEngine
also makes this problem go away (?)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_/
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.
The code should not be the following structure:
But the following:
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:
This is what I would recommend you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With