Skip to content

Commit

Permalink
Initializing search locations where dependencies could be located
Browse files Browse the repository at this point in the history
  • Loading branch information
macacq committed Aug 6, 2016
1 parent 8e918c1 commit cca7290
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions Xrm.Sdk.PluginRegistration/AssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Xrm.Sdk.PluginRegistration
{
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
Expand All @@ -32,7 +33,7 @@ internal static class AssemblyResolver
/// This is useful for architecture specific assemblies that may be different depending on the process architecture
/// of the current assembly.
/// </remarks>
private static readonly string[] AssemblyProbeSubdirectories = new string[] { string.Empty, "amd64", "i386", @"..\..\..\..\..\private\lib", "Plugins" };
private static readonly string[] AssemblyProbeSubdirectories = new string[] { string.Empty, "amd64", "i386", @"..\..\..\..\..\private\lib" };

/// <summary>
/// Contains a list of the assemblies that were resolved via the custom assembly resolve event
Expand All @@ -42,7 +43,36 @@ internal static class AssemblyResolver
/// <summary>
/// List of base directories that should be checked
/// </summary>
private static Collection<string> _baseDirectories = new Collection<string>();
private static Collection<string> _baseDirectories = SetLocations();

private static Collection<string> SetLocations()
{
var list = new Collection<string>();
var location = string.Empty;

// Adding current AppDomain location
location = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory).ToUpperInvariant();
list.Add(location);

// Searching for plugins in subfolder of current AppDomain location
location = Path.Combine(location, "Plugins").ToUpperInvariant();

// Adding (if different) current working folder location
location = Path.GetDirectoryName(Environment.CurrentDirectory).ToUpperInvariant();
if (!list.Contains(location))
{
list.Add(location);
}

// Searching for plugins in subfolder of current working folder location
location = Path.Combine(location, "Plugins").ToUpperInvariant();
if (!list.Contains(location))
{
list.Add(location);
}

return list;
}

/// <summary>
/// Attaches the resolver to the current app domain
Expand All @@ -58,7 +88,6 @@ internal static void AttachResolver()
internal static void AttachResolver(AppDomain domain)
{
domain.AssemblyResolve -= new ResolveEventHandler(ResolveAssembly);
domain.AssemblyResolve -= new ResolveEventHandler(ResolveAssembly);

if (null == domain)
{
Expand All @@ -77,23 +106,19 @@ private static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
return resolvedAssembly;
}

//Check if the base directories have been initialized
if (_baseDirectories.Count == 0)
{
_baseDirectories.Add(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory).ToUpperInvariant());
//Check if the assembly has been already loaded
resolvedAssembly = AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName == args.Name).FirstOrDefault();

var currentDirectory = Path.GetDirectoryName(Environment.CurrentDirectory);
if (!_baseDirectories.Contains(currentDirectory))
{
_baseDirectories.Add(currentDirectory);
}
if (resolvedAssembly != null)
{
return resolvedAssembly;
}

//Create an AssemblyName from the event arguments so that the name can be retrieved
var name = new AssemblyName(args.Name);

//Create a file name for the assembly to start probing for a location
string fileName = name.Name + ".dll";
var fileName = name.Name + ".dll";

//Loop through the probing subdirectories to see if the assembly exists
foreach (var baseDirectory in _baseDirectories)
Expand Down

0 comments on commit cca7290

Please sign in to comment.