Пример #1
0
        /// <summary>
        /// Adds assemblies from the current <see cref="AppDomain.BaseDirectory"/> to the builder.
        /// </summary>
        /// <param name="manager">The builder.</param>
        /// <returns>The builder with the additionally added assemblies.</returns>
        public static IApplicationPartManagerWithAssemblies AddFromApplicationBaseDirectory(this IApplicationPartManager manager)
        {
            var appDomainBase = AppDomain.CurrentDomain.BaseDirectory;

            if (string.IsNullOrWhiteSpace(appDomainBase) || !Directory.Exists(appDomainBase))
            {
                return(new ApplicationPartManagerWithAssemblies(manager, Enumerable.Empty <Assembly>()));
            }

            var dirs = new Dictionary <string, SearchOption> {
                [appDomainBase] = SearchOption.TopDirectoryOnly
            };

            AssemblyLoaderPathNameCriterion[] excludeCriteria =
            {
                AssemblyLoaderCriteria.ExcludeResourceAssemblies
            };

            AssemblyLoaderReflectionCriterion[] loadCriteria =
            {
                AssemblyLoaderReflectionCriterion.NewCriterion(ReferencesOrleansOrAbstractionsAssemblyPredicate)
            };

            var loadedAssemblies = AssemblyLoader.LoadAssemblies(dirs, excludeCriteria, loadCriteria, NullLogger.Instance);

            foreach (var assembly in loadedAssemblies)
            {
                manager.AddApplicationPart(new AssemblyPart(assembly));
            }

            return(new ApplicationPartManagerWithAssemblies(manager, loadedAssemblies));

            // Returns true if the provided assembly references the Orleans core or abstractions assembly.
            bool ReferencesOrleansOrAbstractionsAssemblyPredicate(Assembly assembly, out IEnumerable <string> complaints)
            {
                var referencesOrleans = assembly.GetReferencedAssemblies().Any(ReferencesOrleansOrAbstractions);

                complaints = referencesOrleans ? null : NoReferenceComplaint;
                return(referencesOrleans);

                bool ReferencesOrleansOrAbstractions(AssemblyName reference)
                {
                    return(string.Equals(reference.Name, CoreAssemblyName, StringComparison.OrdinalIgnoreCase) ||
                           string.Equals(reference.Name, AbstractionsAssemblyName, StringComparison.OrdinalIgnoreCase));
                }
            }
        }
        /// <summary>
        /// Attempts to load and add assemblies from the specified directories as application parts.
        /// </summary>
        /// <param name="applicationPartManager">The application part manager.</param>
        /// <param name="directories">The directories to search.</param>
        private static void AddApplicationPartsFromProbingPath(this ApplicationPartManager applicationPartManager, params string[] directories)
        {
            if (directories == null)
            {
                throw new ArgumentNullException(nameof(directories));
            }
            var dirs = new Dictionary <string, SearchOption>();

            foreach (var dir in directories)
            {
                dirs[dir] = SearchOption.TopDirectoryOnly;
            }

            AssemblyLoaderPathNameCriterion[] excludeCriteria =
            {
                AssemblyLoaderCriteria.ExcludeResourceAssemblies
            };

            AssemblyLoaderReflectionCriterion[] loadCriteria =
            {
                AssemblyLoaderReflectionCriterion.NewCriterion(ReferencesOrleansOrAbstractionsAssemblyPredicate)
            };

            var loadedAssemblies = AssemblyLoader.LoadAssemblies(dirs, excludeCriteria, loadCriteria, new LoggerWrapper(nameof(ApplicationPartManagerExtensions), NullLoggerFactory.Instance));

            foreach (var assembly in loadedAssemblies)
            {
                applicationPartManager.AddApplicationPart(assembly);
            }

            // Returns true if the provided assembly references the Orleans core or abstractions assembly.
            bool ReferencesOrleansOrAbstractionsAssemblyPredicate(Assembly assembly, out IEnumerable <string> complaints)
            {
                var referencesOrleans = assembly.GetReferencedAssemblies().Any(ReferencesOrleansOrAbstractions);

                complaints = referencesOrleans ? null : NoReferenceComplaint;
                return(referencesOrleans);

                bool ReferencesOrleansOrAbstractions(AssemblyName reference)
                {
                    return(string.Equals(reference.Name, CoreAssemblyName, StringComparison.OrdinalIgnoreCase) ||
                           string.Equals(reference.Name, AbstractionsAssemblyName, StringComparison.OrdinalIgnoreCase));
                }
            }
        }