示例#1
0
            /// <summary>
            /// Resolve o assembly informado.
            /// </summary>
            /// <param name="args"></param>
            /// <param name="assembly"></param>
            /// <param name="error">Erro ocorrido</param>
            /// <returns></returns>
            public bool Resolve(ResolveEventArgs args, out System.Reflection.Assembly assembly, out Exception error)
            {
                var libraryName = new System.Reflection.AssemblyName(args.Name).Name;

                if (!libraryName.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase))
                {
                    libraryName = string.Concat(libraryName, ".dll");
                }
                if (_assemblies.TryGetValue(libraryName, out assembly))
                {
                    error = null;
                    return(true);
                }
                var part = _deploymentParts.Where(f => string.Compare(f.Source, libraryName, true) == 0).FirstOrDefault();

                if (part != null)
                {
                    try
                    {
                        if (_packageDirectory == null)
                        {
                            var fileInfo = _zipArchive.Files.Where(f => f.Name == part.Source).FirstOrDefault();
                            var buffer   = new byte[fileInfo.Length];
                            using (var file = _zipArchive.OpenRead(part.Source))
                                file.Read(buffer, 0, buffer.Length);
                            assembly = part.Load(_appDomain, buffer);
                        }
                        else
                        {
                            assembly = part.Load(_appDomain, System.IO.Path.Combine(_packageDirectory, part.Source));
                        }
                        assembly.GetTypes();
                    }
                    catch (Exception ex)
                    {
                        error = ex;
                        return(false);
                    }
                    _assemblies.Add(libraryName, assembly);
                    error = null;
                    return(true);
                }
                error = null;
                return(false);
            }
示例#2
0
        /// <summary>
        /// Recupera os assemblies contidos na stream informada.
        /// </summary>
        /// <param name="resolverManager"></param>
        /// <param name="assemblyRepositoryDirectory"></param>
        /// <param name="packageUid">Identificador do pacote.</param>
        /// <param name="packageStream"></param>
        /// <param name="canOverride">Identifica se é para sobreescreve os arquivos.</param>
        /// <param name="aggregateException"></param>
        /// <returns></returns>
        public static IEnumerable <System.Reflection.Assembly> LoadPackagedAssemblies(AssemblyResolverManager resolverManager, string assemblyRepositoryDirectory, Guid packageUid, System.IO.Stream packageStream, bool canOverride, out AggregateException aggregateException)
        {
            resolverManager.Require("resolverManager").NotNull();
            packageStream.Require("packageStream").NotNull();
            var exceptions = new List <Exception>();
            Dictionary <string, System.Reflection.Assembly> domainAssemblies = new Dictionary <string, System.Reflection.Assembly>(StringComparer.InvariantCultureIgnoreCase);

            foreach (var i in resolverManager.AppDomain.GetAssemblies())
            {
                var key = string.Format("{0}.dll", i.GetName().Name);
                if (!domainAssemblies.ContainsKey(key))
                {
                    domainAssemblies.Add(key, i);
                }
                else
                {
                    domainAssemblies[key] = i;
                }
            }
            string packageDirectory = null;

            if (!string.IsNullOrEmpty(assemblyRepositoryDirectory))
            {
                packageDirectory = System.IO.Path.Combine(assemblyRepositoryDirectory, packageUid.ToString());
                if (!System.IO.Directory.Exists(packageDirectory))
                {
                    System.IO.Directory.CreateDirectory(packageDirectory);
                }
            }
            using (var zipArchive = new IO.Compression.ZipArchive(packageStream, System.IO.FileAccess.Read))
            {
                if (packageDirectory != null)
                {
                    zipArchive.CopyToDirectory("", packageDirectory, canOverride);
                }
                var list = new List <System.Reflection.Assembly>();
                IEnumerable <AssemblyPart> deploymentParts = GetDeploymentParts(zipArchive);
                var resolver = new LoadPackageAssemblyResolver(resolverManager.AppDomain, domainAssemblies, deploymentParts, zipArchive, packageDirectory);
                resolverManager.Add(resolver);
                try
                {
                    foreach (AssemblyPart part in deploymentParts)
                    {
                        System.Reflection.Assembly assembly = null;
                        if (!domainAssemblies.TryGetValue(part.Source, out assembly))
                        {
                            if (packageDirectory == null)
                            {
                                var fileInfo = zipArchive.Files.Where(f => f.Name == part.Source).FirstOrDefault();
                                var raw      = new byte[fileInfo.Length];
                                using (var file = zipArchive.OpenRead(part.Source))
                                    file.Read(raw, 0, raw.Length);
                                try
                                {
                                    assembly = part.Load(resolverManager.AppDomain, raw);
                                }
                                catch (Exception ex)
                                {
                                    exceptions.Add(ex);
                                    continue;
                                }
                            }
                            else
                            {
                                try
                                {
                                    assembly = part.Load(resolverManager.AppDomain, System.IO.Path.Combine(packageDirectory, part.Source));
                                }
                                catch (Exception ex)
                                {
                                    exceptions.Add(ex);
                                    continue;
                                }
                                try
                                {
                                    assembly.GetTypes();
                                }
                                catch (System.Reflection.ReflectionTypeLoadException ex)
                                {
                                    exceptions.Add(new System.Reflection.ReflectionTypeLoadException(ex.Types, ex.LoaderExceptions, string.Format("An error ocurred when load types from assembly '{0}'", assembly.FullName)));
                                    continue;
                                }
                                catch (Exception ex)
                                {
                                    exceptions.Add(new Exception(string.Format("An error ocurred when load types from assembly '{0}'", assembly.FullName), ex));
                                    continue;
                                }
                            }
                            if (!domainAssemblies.ContainsKey(part.Source))
                            {
                                domainAssemblies.Add(part.Source, assembly);
                            }
                        }
                        list.Add(assembly);
                    }
                }
                finally
                {
                    resolverManager.Remove(resolver);
                }
                if (exceptions.Count > 0)
                {
                    aggregateException = new AggregateException(exceptions);
                }
                else
                {
                    aggregateException = null;
                }
                return(list);
            }
        }