/// <summary> /// Creates a new algorithm instance. Checks configuration for a specific type name, and if present will /// force it to find that one /// </summary> /// <param name="assemblyPath">Physical path of the algorithm dll.</param> /// <returns>Algorithm instance</returns> public IAlgorithm CreateAlgorithmInstance(string assemblyPath) { string error; IAlgorithm algorithm; var algorithmName = Config.Get("algorithm-type-name"); // don't force load times to be fast here since we're running locally, this allows us to debug // and step through some code that may take us longer than the default 10 seconds var loader = new Loader(TimeSpan.FromHours(1), names => names.Single(name => MatchTypeName(name, algorithmName))); var complete = loader.TryCreateAlgorithmInstanceWithIsolator(assemblyPath, out algorithm, out error); if (!complete) throw new Exception(error + ": try re-building algorithm."); return algorithm; }
/******************************************************** * PUBLIC METHODS *********************************************************/ /// <summary> /// Creates a new algorithm instance. Verified there's only one defined in the assembly and requires /// instantiation to take less than 10 seconds /// </summary> /// <param name="assemblyPath">Physical location of the assembly.</param> /// <returns>Algorithm instance.</returns> public IAlgorithm CreateAlgorithmInstance(string assemblyPath) { string error; IAlgorithm algorithm; // limit load times to 10 seconds and force the assembly to have exactly one derived type var loader = new Loader(TimeSpan.FromSeconds(10), names => names.SingleOrDefault()); bool complete = loader.TryCreateAlgorithmInstanceWithIsolator(assemblyPath, out algorithm, out error); if (!complete) throw new Exception(error + " Try re-building algorithm."); return algorithm; }
/// <summary> /// Create a new instance of an algorithm from a physical dll path. /// </summary> /// <param name="assemblyPath">The path to the assembly's location</param> /// <returns>A new instance of IAlgorithm, or throws an exception if there was an error</returns> public IAlgorithm CreateAlgorithmInstance(string assemblyPath, Language language) { string error; IAlgorithm algorithm; // limit load times to 10 seconds and force the assembly to have exactly one derived type var loader = new Loader(language, TimeSpan.FromSeconds(15), names => { // if there's only one use that guy if (names.Count == 1) { return names.Single(); } // if there's more than one then check configuration for which one we should use var algorithmName = Config.Get("algorithm-type-name"); return names.Single(x => x.Contains(algorithmName)); }); var complete = loader.TryCreateAlgorithmInstanceWithIsolator(assemblyPath, out algorithm, out error); if (!complete) throw new Exception(error + " Try re-building algorithm."); return algorithm; }