Example #1
0
            private static PluginInstance ActivePlugin(string directoryName)
            {
                PackageMetadata metadata = null;
                string          errorMessage;

                try
                {
                    metadata = PackageUtils.GetPackageMetadataFromPluginDirectory(directoryName, out errorMessage);

                    var dllDirectoryPath = PathUtils.GetPluginDllDirectoryPath(directoryName);
                    if (string.IsNullOrEmpty(dllDirectoryPath))
                    {
                        throw new Exception($"插件可执行文件 {directoryName}.dll 不存在");
                    }

                    //foreach (var filePath in DirectoryUtils.GetFilePaths(DirectoryUtils.GetDirectoryPath(metadata.ExecuteFilePath)))
                    //{

                    //    if (!StringUtils.EqualsIgnoreCase(PathUtils.GetExtension(filePath), ".dll")) continue;
                    //    var fileName = PathUtils.GetFileName(filePath);
                    //    if (StringUtils.EqualsIgnoreCase(fileName, PathUtils.GetFileName(metadata.ExecuteFilePath))) continue;
                    //    if (FileUtils.IsFileExists(PathUtils.Combine(WebConfigUtils.PhysicalApplicationPath, "Bin", fileName))) continue;
                    //    Assembly.Load(File.ReadAllBytes(filePath));
                    //}
                    //var assembly = Assembly.Load(File.ReadAllBytes(metadata.ExecuteFilePath));

                    //metadata.GetDependencyGroups()

                    CopyDllsToBin(metadata.Id, dllDirectoryPath);

                    //var assembly = Assembly.Load(File.ReadAllBytes(PathUtils.Combine(WebConfigUtils.PhysicalApplicationPath, "Bin", PathUtils.GetFileName(metadata.ExecuteFilePath))));

                    Assembly assembly;
                    try
                    {
                        assembly = Assembly.Load(metadata.Id);  // load the dll from bin directory
                    }
                    catch
                    {
                        assembly = Assembly.Load(File.ReadAllBytes(PathUtils.Combine(dllDirectoryPath, $"{metadata.Id}.dll")));
                    }

                    var type = assembly.GetExportedTypes().FirstOrDefault(exportedType => typeof(PluginBase).IsAssignableFrom(exportedType));

                    //var type = assembly.GetTypes().First(o => o.IsClass && !o.IsAbstract && o.IsSubclassOf(typeof(PluginBase)));

                    return(ActiveAndAdd(metadata, type));
                }
                catch (Exception ex)
                {
                    errorMessage = ex.Message;
                    LogUtils.AddErrorLog(ex, $"插件加载:{directoryName}");
                }

                return(new PluginInstance(directoryName, metadata, errorMessage));
            }