Пример #1
0
        /// <summary>
        /// Loads the specified library.
        /// </summary>
        /// <param name="libraryReference">The library reference to consider.</param>
        /// <returns>Returns the loaded library.</returns>
        private IBdoLog LoadLibrary(IBdoAssemblyReference libraryReference)
        {
            var log = new BdoLog();

            if (libraryReference != null && _loadOptions?.SourceKinds != null)
            {
                try
                {
                    Assembly assembly = null;

                    // first we load the assembly

                    IBdoLog firstLog = new BdoLog()
                    {
                        DisplayName = "Loading library '" + libraryReference.Name + "'"
                    };

                    foreach (DatasourceKind dataSourceKind in _loadOptions?.SourceKinds)
                    {
                        IBdoLog subLog = firstLog.AddSubLog(title: "Loading assembly from '" + dataSourceKind.ToString() + "'", eventKind: EventKinds.Message);

                        switch (dataSourceKind)
                        {
                        case DatasourceKind.Memory:
                            if (!string.IsNullOrEmpty(libraryReference.Name))
                            {
                                assembly = AppDomainPool.LoadAssembly(_appDomain, libraryReference.Name, subLog);
                            }
                            else
                            {
                                subLog?.AddWarning("File name missing");
                            }
                            break;

                        case DatasourceKind.Repository:
                            string fileName = libraryReference.FileName;
                            if (string.IsNullOrEmpty(fileName))
                            {
                                fileName = libraryReference.Name + ".dll";
                            }

                            string filePath = _loadOptions.LibraryFolderPath.EndingWith(@"\").ToPath() + fileName;
                            if (!File.Exists(filePath))
                            {
                                subLog?.AddError("Could not find the assembly file path '" + filePath + "'");
                            }
                            else
                            {
                                assembly = AppDomainPool.LoadAssemblyFromFile(_appDomain, filePath, subLog);

                                if (assembly == null)
                                {
                                    subLog?.AddError("Could not load assembly '" + filePath + "'");
                                }
                                else
                                {
                                    subLog?.AddCheckpoint("Loading assembly from file '" + filePath + "'");
                                    assembly = Assembly.LoadFrom(filePath);
                                }
                            }
                            break;

                        case DatasourceKind.RestApi:
                            break;
                        }

                        if (assembly != null)
                        {
                            subLog?.AddMessage("Assembly loaded");
                            break;
                        }
                    }

                    // if we have an assembly then we index library items

                    if (assembly == null)
                    {
                        log?.AddSubLog(firstLog, p => p.HasErrorsOrExceptionsOrWarnings());
                    }
                    else
                    {
                        firstLog.GetEvents(true, EventKinds.Error, EventKinds.Exception).ForEach(p => p.Kind = EventKinds.Warning);
                        log?.AddSubLog(firstLog);

                        // we get the extension definition

                        IBdoExtensionDefinition extensionDefinition = ExtractExtensionDefinition(assembly, null, log);

                        // we load the using assemblies

                        if (extensionDefinition?.Dto?.UsingAssemblyFileNames != null)
                        {
                            foreach (var st in extensionDefinition.Dto.UsingAssemblyFileNames)
                            {
                                var fileName = st;
                                if (!fileName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
                                {
                                    fileName += ".dll";
                                }
                                IBdoAssemblyReference reference = BdoAssemblyReferenceFactory.Create(st).WithFileName(fileName);

                                log.AddSubLog(LoadExtensionsInStore(reference), title: "Loading using extensions...");
                            }
                        }

                        // we load the item definition specifiying the extension definition

                        foreach (BdoExtensionItemKind kind in new[] {
                            BdoExtensionItemKind.Carrier,
                            BdoExtensionItemKind.Connector,
                            BdoExtensionItemKind.Entity,
                            BdoExtensionItemKind.Handler,
                            BdoExtensionItemKind.Metrics,
                            BdoExtensionItemKind.Routine,
                            BdoExtensionItemKind.Scriptword,
                            BdoExtensionItemKind.Task
                        })
                        {
                            IBdoLog subSubLog = new BdoLog();
                            int     count     = LoadDictionary(assembly, kind, extensionDefinition, subSubLog);

                            if (subSubLog.HasErrorsOrExceptionsOrWarnings())
                            {
                                log.AddSubLog(
                                    subSubLog,
                                    title: "Dictionary '" + kind.ToString() + "' loaded (" + count.ToString() + " items added)");
                            }
                            else
                            {
                                log.AddMessage("Dictionary '" + kind.ToString() + "' loaded (" + count.ToString() + " items added)");
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    log?.AddException(exception);
                }
            }

            return(log);
        }