2010-03-12 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpRuntime.cs
1 //
2 // System.Web.HttpRuntime.cs 
3 // 
4 // Authors:
5 //      Miguel de Icaza (miguel@novell.com)
6 //      Marek Habersack <mhabersack@novell.com>
7 //
8 //
9 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 //
32 // TODO: Call HttpRequest.CloseInputStream when we finish a request, as we are using the IntPtr stream.
33 //
34 using System.IO;
35 using System.Text;
36 using System.Globalization;
37 using System.Collections;
38 using System.Reflection;
39 using System.Security;
40 using System.Security.Permissions;
41 using System.Web.Caching;
42 using System.Web.Configuration;
43 using System.Web.UI;
44 using System.Web.Util;
45 #if MONOWEB_DEP
46 using Mono.Web.Util;
47 #endif
48 using System.Threading;
49 #if TARGET_J2EE
50 using Mainsoft.Web;
51 #else
52 using System.CodeDom.Compiler;
53 using System.Web.Compilation;
54 #endif
55
56 namespace System.Web
57 {       
58         // CAS - no InheritanceDemand here as the class is sealed
59         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
60         public sealed class HttpRuntime
61         {
62                 static bool domainUnloading;
63                 
64 #if TARGET_J2EE
65                 static QueueManager queue_manager { get { return _runtime._queue_manager; } }
66                 static TraceManager trace_manager { get { return _runtime._trace_manager; } }
67                 static Cache cache { get { return _runtime._cache; } }
68                 static Cache internalCache { get { return _runtime._internalCache; } }
69                 static WaitCallback do_RealProcessRequest;
70                 
71                 QueueManager _queue_manager;
72                 TraceManager _trace_manager;
73                 Cache _cache;
74                 Cache _internalCache;
75
76                 public HttpRuntime ()
77                 {
78                         WebConfigurationManager.Init ();
79                         _queue_manager = new QueueManager ();
80                         _trace_manager = new TraceManager ();
81                         _cache = new Cache ();
82                         _internalCache = new Cache();
83                         _internalCache.DependencyCache = _cache;
84                 }
85
86                 static HttpRuntime _runtimeInstance {
87                         get {
88                                 HttpRuntime runtime = (HttpRuntime) AppDomain.CurrentDomain.GetData ("HttpRuntime");
89                                 if (runtime == null)
90                                         lock (typeof (HttpRuntime)) {
91                                                 runtime = (HttpRuntime) AppDomain.CurrentDomain.GetData ("HttpRuntime");
92                                                 if (runtime == null) {
93                                                         runtime = new HttpRuntime ();
94                                                         AppDomain.CurrentDomain.SetData ("HttpRuntime", runtime);
95                                                 }
96                                         }
97                                 return runtime;
98                         }
99                 }
100                 static HttpRuntime _runtime
101                 {
102                         get
103                         {
104                                 if (HttpContext.Current != null)
105                                         return HttpContext.Current.HttpRuntimeInstance;
106                                 else
107                                         return _runtimeInstance;
108                         }
109                 }
110 #else
111                 static QueueManager queue_manager;
112                 static TraceManager trace_manager;
113                 static Cache cache;
114                 static Cache internalCache;
115                 static WaitCallback do_RealProcessRequest;
116                 static Exception initialException;
117                 static bool firstRun;
118                 static bool assemblyMappingEnabled;
119                 static object assemblyMappingLock = new object ();
120                 static object appOfflineLock = new object ();
121                 
122                 public HttpRuntime ()
123                 {
124
125                 }
126 #endif
127
128                 static HttpRuntime ()
129                 {
130 #if !TARGET_J2EE
131                         firstRun = true;
132                         try {
133                                 WebConfigurationManager.Init ();
134 #if MONOWEB_DEP
135                                 SettingsMappingManager.Init ();
136 #endif
137                         } catch (Exception ex) {
138                                 initialException = ex;
139                         }
140
141                         // The classes in whose constructors exceptions may be thrown, should be handled the same way QueueManager
142                         // and TraceManager are below. The constructors themselves MUST NOT throw any exceptions - we MUST be sure
143                         // the objects are created here. The exceptions will be dealt with below, in RealProcessRequest.
144                         queue_manager = new QueueManager ();
145                         if (queue_manager.HasException)
146                                 initialException = queue_manager.InitialException;
147
148                         trace_manager = new TraceManager ();
149                         if (trace_manager.HasException)
150                                         initialException = trace_manager.InitialException;
151
152                         cache = new Cache ();
153                         internalCache = new Cache ();
154                         internalCache.DependencyCache = internalCache;
155 #endif
156                         do_RealProcessRequest = new WaitCallback (RealProcessRequest);
157                 }
158                 
159 #region AppDomain handling
160                 internal static bool DomainUnloading {
161                         get { return domainUnloading; }
162                 }
163                 
164                 //
165                 // http://radio.weblogs.com/0105476/stories/2002/07/12/executingAspxPagesWithoutAWebServer.html
166                 //
167                 public static string AppDomainAppId {
168                         [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.High)]
169                         get {
170                                 //
171                                 // This value should not change across invocations
172                                 //
173                                 string dirname = (string) AppDomain.CurrentDomain.GetData (".appId");
174                                 if ((dirname != null) && (dirname.Length > 0) && SecurityManager.SecurityEnabled) {
175                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dirname).Demand ();
176                                 }
177                                 return dirname;
178                         }
179                 }
180
181                 // Physical directory for the application
182                 public static string AppDomainAppPath {
183                         get {
184                                 string dirname = (string) AppDomain.CurrentDomain.GetData (".appPath");
185                                 if (SecurityManager.SecurityEnabled) {
186                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dirname).Demand ();
187                                 }
188                                 return dirname;
189                         }
190                 }
191
192                 public static string AppDomainAppVirtualPath {
193                         get {
194                                 return (string) AppDomain.CurrentDomain.GetData (".appVPath");
195                         }
196                 }
197
198                 public static string AppDomainId {
199                         [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.High)]
200                         get {
201                                 return (string) AppDomain.CurrentDomain.GetData (".domainId");
202                         }
203                 }
204
205                 public static string AspInstallDirectory {
206                         get {
207                                 string dirname = (string) AppDomain.CurrentDomain.GetData (".hostingInstallDir");
208                                 if ((dirname != null) && (dirname.Length > 0) && SecurityManager.SecurityEnabled) {
209                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dirname).Demand ();
210                                 }
211                                 return dirname;
212                         }
213                 }
214 #endregion
215
216                 static string _actual_bin_directory;
217                 public static string BinDirectory {
218                         get {
219                                 if (_actual_bin_directory == null) {
220                                         string[] parts = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath.Split (';');
221                                         string mypath = AppDomainAppPath;
222                                         string tmp;
223                                         
224                                         foreach (string p in parts) {
225                                                 tmp = Path.Combine (mypath, p);
226                                                 if (Directory.Exists (tmp)) {
227                                                         _actual_bin_directory = tmp;
228                                                         break;
229                                                 }
230                                         }
231
232                                         if (_actual_bin_directory == null)
233                                                 _actual_bin_directory = Path.Combine (mypath, "bin");
234
235                                         if (_actual_bin_directory [_actual_bin_directory.Length - 1] != Path.DirectorySeparatorChar)
236                                                 _actual_bin_directory += Path.DirectorySeparatorChar;
237                                 }
238                                 
239                                 if (SecurityManager.SecurityEnabled)
240                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, _actual_bin_directory).Demand ();
241
242                                 return _actual_bin_directory;
243                         }
244                 }
245
246                 public static Cache Cache {
247                         get {
248                                 return cache;
249                         }
250                 }
251
252                 internal static Cache InternalCache {
253                         get {
254                                 return internalCache;
255                         }
256                 }
257                 
258                 public static string ClrInstallDirectory {
259                         get {
260                                 string dirname = Path.GetDirectoryName (typeof (Object).Assembly.Location);
261                                 if ((dirname != null) && (dirname.Length > 0) && SecurityManager.SecurityEnabled) {
262                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dirname).Demand ();
263                                 }
264                                 return dirname;
265                         }
266                 }
267
268                 public static string CodegenDir {
269                         get {
270                                 string dirname = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
271                                 if (SecurityManager.SecurityEnabled) {
272                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dirname).Demand ();
273                                 }
274                                 return dirname;
275                         }
276                 }
277
278                 public static bool IsOnUNCShare {
279                         [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Low)]
280                         get {
281                                 return RuntimeHelpers.IsUncShare;
282                         }
283                 }
284
285                 public static string MachineConfigurationDirectory {
286                         get {
287                                 string dirname = Path.GetDirectoryName (ICalls.GetMachineConfigPath ());
288                                 if ((dirname != null) && (dirname.Length > 0) && SecurityManager.SecurityEnabled) {
289                                         new FileIOPermission (FileIOPermissionAccess.PathDiscovery, dirname).Demand ();
290                                 }
291                                 return dirname;
292                         }
293                 }
294
295                 
296                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
297                 public static void Close ()
298                 {
299                         // Remove all items from cache.
300                 }
301
302                 internal static HttpWorkerRequest QueuePendingRequest (bool started_internally)
303                 {
304                         HttpWorkerRequest next = queue_manager.GetNextRequest (null);
305                         if (next == null)
306                                 return null;
307
308                         if (!started_internally) {
309                                 next.StartedInternally = true;
310                                 ThreadPool.QueueUserWorkItem (do_RealProcessRequest, next);
311                                 return null;
312                         }
313                         return next;
314                 }
315
316 #if !TARGET_J2EE
317                 static readonly string[] app_offline_files = {"app_offline.htm", "App_Offline.htm", "APP_OFFLINE.HTM"};
318                 static string app_offline_file;
319                 
320                 static bool AppIsOffline (HttpContext context)
321                 {
322                         if (!HttpApplicationFactory.ApplicationDisabled || app_offline_file == null)
323                                 return false;
324
325                         HttpResponse response = context.Response;
326                         response.Clear ();
327                         response.ContentType = "text/html";
328                         response.ExpiresAbsolute = DateTime.UtcNow;
329                         response.StatusCode = 503;
330                         response.TransmitFile (app_offline_file, true);
331                         
332                         context.Request.ReleaseResources ();
333                         context.Response.ReleaseResources ();
334                         HttpContext.Current = null;
335                         HttpApplication.requests_total_counter.Increment ();
336                         
337                         return true;
338                 }
339
340                 static void AppOfflineFileRenamed (object sender, RenamedEventArgs args)
341                 {
342                         AppOfflineFileChanged (sender, args);
343                 }
344
345                 static void AppOfflineFileChanged (object sender, FileSystemEventArgs args)
346                 {
347                         lock (appOfflineLock) {
348                                 bool offline;
349                                 
350                                 switch (args.ChangeType) {
351                                         case WatcherChangeTypes.Created:
352                                         case WatcherChangeTypes.Changed:
353                                                 offline = true;
354                                                 break;
355
356                                         case WatcherChangeTypes.Deleted:
357                                                 offline = false;
358                                                 break;
359
360                                         case WatcherChangeTypes.Renamed:
361                                                 RenamedEventArgs rargs = args as RenamedEventArgs;
362
363                                                 if (rargs != null &&
364                                                     String.Compare (rargs.Name, "app_offline.htm", StringComparison.OrdinalIgnoreCase) == 0)
365                                                         offline = true;
366                                                 else
367                                                         offline = false;
368                                                 break;
369
370                                         default:
371                                                 offline = false;
372                                                 break;
373                                 }
374                                 SetOfflineMode (offline, args.FullPath);
375                         }
376                 }
377
378                 static void SetOfflineMode (bool offline, string filePath)
379                 {
380                         if (!offline) {
381                                 app_offline_file = null;
382                                 if (HttpApplicationFactory.ApplicationDisabled)
383                                         HttpRuntime.UnloadAppDomain ();
384                         } else {
385                                 app_offline_file = filePath;
386                                 HttpApplicationFactory.DisableWatchers ();
387                                 HttpApplicationFactory.ApplicationDisabled = true;
388                                 InternalCache.InvokePrivateCallbacks ();
389                                 HttpApplicationFactory.Dispose ();
390                         }
391                 }
392                 
393                 static void SetupOfflineWatch ()
394                 {
395                         lock (appOfflineLock) {
396                                 FileSystemEventHandler seh = new FileSystemEventHandler (AppOfflineFileChanged);
397                                 RenamedEventHandler reh = new RenamedEventHandler (AppOfflineFileRenamed);
398
399                                 string app_dir = AppDomainAppPath;
400                                 ArrayList watchers = new ArrayList ();
401                                 FileSystemWatcher watcher;
402                                 string offlineFile = null, tmp;
403                                 
404                                 foreach (string f in app_offline_files) {
405                                         watcher = new FileSystemWatcher ();
406                                         watcher.Path = Path.GetDirectoryName (app_dir);
407                                         watcher.Filter = Path.GetFileName (f);
408                                         watcher.NotifyFilter |= NotifyFilters.Size;
409                                         watcher.Deleted += seh;
410                                         watcher.Changed += seh;
411                                         watcher.Created += seh;
412                                         watcher.Renamed += reh;
413                                         watcher.EnableRaisingEvents = true;
414                                         
415                                         watchers.Add (watcher);
416
417                                         tmp = Path.Combine (app_dir, f);
418                                         if (File.Exists (tmp))
419                                                 offlineFile = tmp;
420                                 }
421
422                                 if (offlineFile != null)
423                                         SetOfflineMode (true, offlineFile);
424                         }
425                 }
426 #endif
427                 
428                 static void RealProcessRequest (object o)
429                 {
430                         HttpWorkerRequest req = (HttpWorkerRequest) o;
431                         bool started_internally = req.StartedInternally;
432                         do {
433                                 Process (req);
434                                 req = QueuePendingRequest (started_internally);
435                         } while (started_internally && req != null);
436                 }
437
438                 static void Process (HttpWorkerRequest req)
439                 {
440 #if TARGET_J2EE
441                         HttpContext context = HttpContext.Current;
442                         if (context == null)
443                                 context = new HttpContext (req);
444                         else
445                                 context.SetWorkerRequest (req);
446 #else
447                         HttpContext context = new HttpContext (req);
448 #endif
449                         HttpContext.Current = context;
450                         bool error = false;
451 #if !TARGET_J2EE
452                         if (firstRun) {
453                                 SetupOfflineWatch ();
454                                 firstRun = false;
455                                 if (initialException != null) {
456                                         FinishWithException (req, new HttpException ("Initial exception", initialException));
457                                         error = true;
458                                 }
459                         }
460
461                         if (AppIsOffline (context))
462                                 return;
463 #endif
464                         
465                         //
466                         // Get application instance (create or reuse an instance of the correct class)
467                         //
468                         HttpApplication app = null;
469                         if (!error) {
470                                 try {
471                                         app = HttpApplicationFactory.GetApplication (context);
472                                 } catch (Exception e) {
473                                         FinishWithException (req, new HttpException ("", e));
474                                         error = true;
475                                 }
476                         }
477                         
478                         if (error) {
479                                 context.Request.ReleaseResources ();
480                                 context.Response.ReleaseResources ();
481                                 HttpContext.Current = null;
482                         } else {
483                                 context.ApplicationInstance = app;
484
485                                 //
486                                 // Ask application to service the request
487                                 //
488                                 
489 #if TARGET_J2EE
490                                 IHttpAsyncHandler ihah = app;
491                                 if (context.Handler == null)
492                                         ihah.BeginProcessRequest (context, new AsyncCallback (request_processed), context);
493                                 else
494                                         app.Tick ();
495                                 //ihh.ProcessRequest (context);
496                                 IHttpExtendedHandler extHandler = context.Handler as IHttpExtendedHandler;
497                                 if (extHandler != null && !extHandler.IsCompleted)
498                                         return;
499                                 if (context.Error is UnifyRequestException)
500                                         return;
501
502                                 ihah.EndProcessRequest (null);
503 #else
504                                 IHttpHandler ihh = app;
505 //                              IAsyncResult appiar = ihah.BeginProcessRequest (context, new AsyncCallback (request_processed), context);
506 //                              ihah.EndProcessRequest (appiar);
507                                 ihh.ProcessRequest (context);
508 #endif
509
510                                 HttpApplicationFactory.Recycle (app);
511                         }
512                 }
513                 
514                 //
515                 // ProcessRequest method is executed in the AppDomain of the application
516                 //
517                 // Observations:
518                 //    ProcessRequest does not guarantee that `wr' will be processed synchronously,
519                 //    the request can be queued and processed later.
520                 //
521                 [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
522                 public static void ProcessRequest (HttpWorkerRequest wr)
523                 {
524                         if (wr == null)
525                                 throw new ArgumentNullException ("wr");
526                         //
527                         // Queue our request, fetch the next available one from the queue
528                         //
529                         HttpWorkerRequest request = queue_manager.GetNextRequest (wr);
530                         if (request == null)
531                                 return;
532
533                         QueuePendingRequest (false);
534                         RealProcessRequest (request);
535                 }
536
537 #if TARGET_J2EE
538                 //
539                 // Callback to be invoked by IHttpAsyncHandler.BeginProcessRequest
540                 //
541                 static void request_processed (IAsyncResult iar)
542                 {
543                         HttpContext context = (HttpContext) iar.AsyncState;
544
545                         context.Request.ReleaseResources ();
546                         context.Response.ReleaseResources ();
547                 }
548 #endif
549                 
550 #if TARGET_JVM
551                 [MonoNotSupported ("UnloadAppDomain is not supported")]
552                 public static void UnloadAppDomain ()
553                 {
554                         throw new NotImplementedException ("UnloadAppDomain is not supported");
555                 }
556 #else
557                 //
558                 // Called when we are shutting down or we need to reload an application
559                 // that has been modified (touch global.asax) 
560                 //
561                 [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
562                 public static void UnloadAppDomain ()
563                 {
564                         //
565                         // TODO: call ReleaseResources
566                         //
567                         domainUnloading = true;
568                         ThreadPool.QueueUserWorkItem (new WaitCallback (ShutdownAppDomain), null);
569                 }
570 #endif
571                 //
572                 // Shuts down the AppDomain
573                 //
574                 static void ShutdownAppDomain (object args)
575                 {
576                         queue_manager.Dispose ();
577                         // This will call Session_End if needed.
578                         InternalCache.InvokePrivateCallbacks ();
579                         // Kill our application.
580                         HttpApplicationFactory.Dispose ();
581                         ThreadPool.QueueUserWorkItem (new WaitCallback (DoUnload), null);
582                 }
583
584                 static void DoUnload (object state)
585                 {
586 #if TARGET_J2EE
587                         // No unload support for appdomains under Grasshopper
588 #else
589                         AppDomain.Unload (AppDomain.CurrentDomain);
590 #endif
591                 }
592
593                 static string content503 = "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" +
594                         "<html><head>\n<title>503 Server Unavailable</title>\n</head><body>\n" +
595                         "<h1>Server Unavailable</h1>\n" +
596                         "</body></html>\n";
597
598                 static void FinishWithException (HttpWorkerRequest wr, HttpException e)
599                 {
600                         int code = e.GetHttpCode ();
601                         wr.SendStatus (code, HttpWorkerRequest.GetStatusDescription (code));
602                         wr.SendUnknownResponseHeader ("Connection", "close");
603                         Encoding enc = Encoding.ASCII;
604                         wr.SendUnknownResponseHeader ("Content-Type", "text/html; charset=" + enc.WebName);
605                         string msg = e.GetHtmlErrorMessage ();
606                         byte [] contentBytes = enc.GetBytes (msg);
607                         wr.SendUnknownResponseHeader ("Content-Length", contentBytes.Length.ToString ());
608                         wr.SendResponseFromMemory (contentBytes, contentBytes.Length);
609                         wr.FlushResponse (true);
610                         wr.CloseConnection ();
611                         HttpApplication.requests_total_counter.Increment ();
612                 }
613
614                 //
615                 // This is called from the QueueManager if a request
616                 // can not be processed (load, no resources, or
617                 // appdomain unload).
618                 //
619                 static internal void FinishUnavailable (HttpWorkerRequest wr)
620                 {
621                         wr.SendStatus (503, "Service unavailable");
622                         wr.SendUnknownResponseHeader ("Connection", "close");
623                         Encoding enc = Encoding.ASCII;
624                         wr.SendUnknownResponseHeader ("Content-Type", "text/html; charset=" + enc.WebName);
625                         byte [] contentBytes = enc.GetBytes (content503);
626                         wr.SendUnknownResponseHeader ("Content-Length", contentBytes.Length.ToString ());
627                         wr.SendResponseFromMemory (contentBytes, contentBytes.Length);
628                         wr.FlushResponse (true);
629                         wr.CloseConnection ();
630                         HttpApplication.requests_total_counter.Increment ();
631                 }
632
633 #if !TARGET_J2EE
634                 static internal void WritePreservationFile (Assembly asm, string genericNameBase)
635                 {
636                         if (asm == null)
637                                 throw new ArgumentNullException ("asm");
638                         if (String.IsNullOrEmpty (genericNameBase))
639                                 throw new ArgumentNullException ("genericNameBase");
640
641                         string compiled = Path.Combine (AppDomain.CurrentDomain.SetupInformation.DynamicBase,
642                                                         genericNameBase + ".compiled");
643                         PreservationFile pf = new PreservationFile ();
644                         try {
645                                 pf.VirtualPath = String.Concat ("/", genericNameBase, "/");
646
647                                 AssemblyName an = asm.GetName ();
648                                 pf.Assembly = an.Name;
649                                 pf.ResultType = BuildResultTypeCode.TopLevelAssembly;
650                                 pf.Save (compiled);
651                         } catch (Exception ex) {
652                                 throw new HttpException (
653                                         String.Format ("Failed to write preservation file {0}", genericNameBase + ".compiled"),
654                                         ex);
655                         }
656                 }
657                 
658                 static Assembly ResolveAssemblyHandler(object sender, ResolveEventArgs e)
659                 {
660                         AssemblyName an = new AssemblyName (e.Name);
661                         string dynamic_base = AppDomain.CurrentDomain.SetupInformation.DynamicBase;
662                         string compiled = Path.Combine (dynamic_base, an.Name + ".compiled");
663
664                         if (!File.Exists (compiled))
665                                 return null;
666
667                         PreservationFile pf;
668                         try {
669                                 pf = new PreservationFile (compiled);
670                         } catch (Exception ex) {
671                                 throw new HttpException (
672                                         String.Format ("Failed to read preservation file {0}", an.Name + ".compiled"),
673                                         ex);
674                         }
675                         
676                         Assembly ret = null;
677                         try {
678                                 string asmPath = Path.Combine (dynamic_base, pf.Assembly + ".dll");
679                                 ret = Assembly.LoadFrom (asmPath);
680                         } catch (Exception) {
681                                 // ignore
682                         }
683                         
684                         return ret;
685                 }
686                 
687                 internal static void EnableAssemblyMapping (bool enable)
688                 {
689                         lock (assemblyMappingLock) {
690                                 if (assemblyMappingEnabled == enable)
691                                         return;
692                                 if (enable)
693                                         AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler (ResolveAssemblyHandler);
694                                 else
695                                         AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler (ResolveAssemblyHandler);
696                                 assemblyMappingEnabled = enable;
697                         }
698                 }
699 #endif // #if !TARGET_J2EE
700                 
701                 internal static TraceManager TraceManager {
702                         get {
703                                 return trace_manager;
704                         }
705                 }
706         }
707 }