Fix loading of references from DynamicLoader

DynamicLoader needs to support loading reference from the "bin"
directory of a module, which is very similar to the "PrecompiledDynamicLoader"
behavior for loading references.

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-07-18 15:27:54 -07:00
parent 9198be72ed
commit a5f47f4de0

View File

@@ -15,6 +15,8 @@ namespace Orchard.Environment.Extensions.Loaders {
private readonly IBuildManager _buildManager;
private readonly IVirtualPathProvider _virtualPathProvider;
private readonly IVirtualPathMonitor _virtualPathMonitor;
private readonly IHostEnvironment _hostEnvironment;
private readonly IAssemblyProbingFolder _assemblyProbingFolder;
private readonly IProjectFileParser _projectFileParser;
private readonly ReloadWorkaround _reloadWorkaround = new ReloadWorkaround();
@@ -22,6 +24,8 @@ namespace Orchard.Environment.Extensions.Loaders {
IBuildManager buildManager,
IVirtualPathProvider virtualPathProvider,
IVirtualPathMonitor virtualPathMonitor,
IHostEnvironment hostEnvironment,
IAssemblyProbingFolder assemblyProbingFolder,
IDependenciesFolder dependenciesFolder,
IProjectFileParser projectFileParser)
: base(dependenciesFolder) {
@@ -29,6 +33,8 @@ namespace Orchard.Environment.Extensions.Loaders {
_buildManager = buildManager;
_virtualPathProvider = virtualPathProvider;
_virtualPathMonitor = virtualPathMonitor;
_hostEnvironment = hostEnvironment;
_assemblyProbingFolder = assemblyProbingFolder;
_projectFileParser = projectFileParser;
Logger = NullLogger.Instance;
@@ -109,6 +115,29 @@ namespace Orchard.Environment.Extensions.Loaders {
}
}
public override void ReferenceActivated(ExtensionLoadingContext context, ExtensionReferenceProbeEntry referenceEntry) {
//Note: This is the same implementation as "PrecompiledExtensionLoader"
if (string.IsNullOrEmpty(referenceEntry.VirtualPath))
return;
string sourceFileName = _virtualPathProvider.MapPath(referenceEntry.VirtualPath);
// Copy the assembly if it doesn't exist or if it is older than the source file.
bool copyAssembly =
!_assemblyProbingFolder.AssemblyExists(referenceEntry.Name) ||
File.GetLastWriteTimeUtc(sourceFileName) > _assemblyProbingFolder.GetAssemblyDateTimeUtc(referenceEntry.Name);
if (copyAssembly) {
context.CopyActions.Add(() => _assemblyProbingFolder.StoreAssembly(referenceEntry.Name, sourceFileName));
// We need to restart the appDomain if the assembly is loaded
if (_hostEnvironment.IsAssemblyLoaded(referenceEntry.Name)) {
Logger.Information("ReferenceActivated: Reference \"{0}\" is activated with newer file and its assembly is loaded, forcing AppDomain restart", referenceEntry.Name);
context.RestartAppDomain = true;
}
}
}
private string GetReferenceVirtualPath(string projectPath, string referenceName) {
var path = _virtualPathProvider.GetDirectoryName(projectPath);
path = _virtualPathProvider.Combine(path, "bin", referenceName + ".dll");
@@ -118,6 +147,11 @@ namespace Orchard.Environment.Extensions.Loaders {
}
public override Assembly LoadReference(DependencyReferenceDescriptor reference) {
// DynamicExtensionLoader has 2 types of references: assemblies from module bin directory
// and .csproj.
if (StringComparer.OrdinalIgnoreCase.Equals(Path.GetExtension(reference.VirtualPath), ".dll"))
return _assemblyProbingFolder.LoadAssembly(reference.Name);
return _buildManager.GetCompiledAssembly(reference.VirtualPath);
}