-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1392 from glopesdev/issue-1383
Rewrite bootstrapper logic to avoid relying on AppDomain
- Loading branch information
Showing
16 changed files
with
439 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,17 @@ | ||
using Bonsai.Configuration; | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Bonsai | ||
{ | ||
class LoaderResource<TLoader> : IDisposable where TLoader : MarshalByRefObject | ||
static class LoaderResource | ||
{ | ||
AppDomain reflectionDomain; | ||
|
||
public LoaderResource(PackageConfiguration configuration) | ||
{ | ||
var currentEvidence = AppDomain.CurrentDomain.Evidence; | ||
var setupInfo = AppDomain.CurrentDomain.SetupInformation; | ||
reflectionDomain = AppDomain.CreateDomain("ReflectionOnly", currentEvidence, setupInfo); | ||
Loader = (TLoader)reflectionDomain.CreateInstanceAndUnwrap( | ||
typeof(TLoader).Assembly.FullName, | ||
typeof(TLoader).FullName, | ||
false, (BindingFlags)0, null, | ||
new[] { configuration }, null, null); | ||
} | ||
|
||
public TLoader Loader { get; private set; } | ||
|
||
public void Dispose() | ||
public static MetadataLoadContext CreateMetadataLoadContext(PackageConfiguration configuration) | ||
{ | ||
AppDomain.Unload(reflectionDomain); | ||
var runtimeAssemblies = Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll"); | ||
var resolver = new PackageAssemblyResolver(configuration, runtimeAssemblies); | ||
return new MetadataLoadContext(resolver); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using Bonsai.Configuration; | ||
|
||
namespace Bonsai | ||
{ | ||
internal class PackageAssemblyResolver : PathAssemblyResolver | ||
{ | ||
public PackageAssemblyResolver(PackageConfiguration configuration, IEnumerable<string> assemblyPaths) | ||
: base(assemblyPaths) | ||
{ | ||
Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); | ||
ConfigurationRoot = ConfigurationHelper.GetConfigurationRoot(configuration); | ||
} | ||
|
||
private PackageConfiguration Configuration { get; } | ||
|
||
private string ConfigurationRoot { get; } | ||
|
||
public override Assembly Resolve(MetadataLoadContext context, AssemblyName assemblyName) | ||
{ | ||
var assembly = base.Resolve(context, assemblyName); | ||
if (assembly != null) return assembly; | ||
|
||
var assemblyLocation = Configuration.GetAssemblyLocation(assemblyName.Name); | ||
if (assemblyLocation != null) | ||
{ | ||
if (assemblyLocation.StartsWith(Uri.UriSchemeFile) && | ||
Uri.TryCreate(assemblyLocation, UriKind.Absolute, out Uri uri)) | ||
{ | ||
return context.LoadFromAssemblyPath(uri.LocalPath); | ||
} | ||
|
||
if (!Path.IsPathRooted(assemblyLocation)) | ||
{ | ||
assemblyLocation = Path.Combine(ConfigurationRoot, assemblyLocation); | ||
} | ||
|
||
if (File.Exists(assemblyLocation)) | ||
{ | ||
return context.LoadFromAssemblyPath(assemblyLocation); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.