public BuildManagerTypeFinder(IIOHelper ioHelper, ILogger logger, ITypeFinderConfig typeFinderConfig = null) : base(logger, typeFinderConfig)
        {
            if (ioHelper == null)
            {
                throw new ArgumentNullException(nameof(ioHelper));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            _allAssemblies = new Lazy <HashSet <Assembly> >(() =>
            {
                var isHosted = ioHelper.IsHosted;
                try
                {
                    if (isHosted)
                    {
                        var assemblies = new HashSet <Assembly>(BuildManager.GetReferencedAssemblies().Cast <Assembly>());

                        //here we are trying to get the App_Code assembly
                        var fileExtensions = new[] { ".cs", ".vb" }; //only vb and cs files are supported
                        var appCodeFolder  = new DirectoryInfo(ioHelper.MapPath(ioHelper.ResolveUrl("~/App_code")));
                        //check if the folder exists and if there are any files in it with the supported file extensions
                        if (appCodeFolder.Exists && fileExtensions.Any(x => appCodeFolder.GetFiles("*" + x).Any()))
                        {
                            try
                            {
                                var appCodeAssembly = Assembly.Load("App_Code");
                                if (assemblies.Contains(appCodeAssembly) == false) // BuildManager will find App_Code already
                                {
                                    assemblies.Add(appCodeAssembly);
                                }
                            }
                            catch (FileNotFoundException ex)
                            {
                                //this will occur if it cannot load the assembly
                                logger.Error(typeof(TypeFinder), ex, "Could not load assembly App_Code");
                            }
                        }
                    }
                }
                catch (InvalidOperationException e)
                {
                    if (e.InnerException is SecurityException == false)
                    {
                        throw;
                    }
                }

                // Not hosted, just use the default implementation
                return(new HashSet <Assembly>(base.AssembliesToScan));
            });
        }
Esempio n. 2
0
        public TypeFinder(ILogger logger, ITypeFinderConfig typeFinderConfig = null)
        {
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
            _assembliesAcceptingLoadExceptions = typeFinderConfig?.AssembliesAcceptingLoadExceptions.Where(x => !x.IsNullOrWhiteSpace()).ToArray() ?? Array.Empty <string>();
            _allAssemblies = new Lazy <HashSet <Assembly> >(() =>
            {
                HashSet <Assembly> assemblies = null;
                try
                {
                    //NOTE: we cannot use AppDomain.CurrentDomain.GetAssemblies() because this only returns assemblies that have
                    // already been loaded in to the app domain, instead we will look directly into the bin folder and load each one.
                    var binFolder        = GetRootDirectorySafe();
                    var binAssemblyFiles = Directory.GetFiles(binFolder, "*.dll", SearchOption.TopDirectoryOnly).ToList();
                    //var binFolder = Assembly.GetExecutingAssembly().GetAssemblyFile().Directory;
                    //var binAssemblyFiles = Directory.GetFiles(binFolder.FullName, "*.dll", SearchOption.TopDirectoryOnly).ToList();
                    assemblies = new HashSet <Assembly>();
                    foreach (var a in binAssemblyFiles)
                    {
                        try
                        {
                            var assName = AssemblyName.GetAssemblyName(a);
                            var ass     = Assembly.Load(assName);
                            assemblies.Add(ass);
                        }
                        catch (Exception e)
                        {
                            if (e is SecurityException || e is BadImageFormatException)
                            {
                                //swallow these exceptions
                            }
                            else
                            {
                                throw;
                            }
                        }
                    }

                    //Since we are only loading in the /bin assemblies above, we will also load in anything that's already loaded (which will include gac items)
                    foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        assemblies.Add(a);
                    }
                }
                catch (InvalidOperationException e)
                {
                    if (e.InnerException is SecurityException == false)
                    {
                        throw;
                    }
                }

                return(assemblies);
            });
        }
Esempio n. 3
0
 public TypeFinder(ILogger <TypeFinder> logger, IAssemblyProvider assemblyProvider, ITypeFinderConfig typeFinderConfig = null)
 {
     _logger           = logger ?? throw new ArgumentNullException(nameof(logger));
     _assemblyProvider = assemblyProvider;
     _typeFinderConfig = typeFinderConfig;
 }