Ejemplo n.º 1
0
        private static void TryLoadFromLocal(IAssemblyProviderResolveCallback resolveCallback,
                                             string externalComponentsRefDirectory,
                                             string targetFrameworkName,
                                             IDictionary <string, string> dependencies,
                                             string packageId,
                                             string version)
        {
            var assemblyFullName = Path.Combine(externalComponentsRefDirectory, $"{packageId}.dll");

            try
            {
                AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyFullName);
            }
            catch
            {
                try
                {
                    if (dependencies.TryGetValue(packageId, out string explicitVersion))
                    {
                        version = explicitVersion;
                    }

                    NugetHelper.TryLoadFromNuget(externalComponentsRefDirectory, targetFrameworkName, packageId, version);
                }
                catch (Exception ex)
                {
                    var entryAssembly = Assembly.GetEntryAssembly();
                    var resolved      = resolveCallback.TryLoad(AssemblyLoadContext.Default, entryAssembly, targetFrameworkName, ex);
                    if (!resolved)
                    {
                        throw new FileLoadException($"{packageId}.{version} could not be loaded! [Local Source]");
                    }
                }
            }
        }
        protected virtual void LoadComponentsJson(IServiceCollection services,
                                                  IMvcBuilder builder,
                                                  List <HisarCacheAttribute> cacheItems,
                                                  string componentJsonFilePath,
                                                  string externalComponentsRefDirectory)
        {
            if (string.IsNullOrEmpty(componentJsonFilePath))
            {
                throw new ArgumentNullException(nameof(componentJsonFilePath));
            }

            if (!File.Exists(componentJsonFilePath))
            {
                throw new FileNotFoundException(nameof(componentJsonFilePath));
            }

            ComponentsJson json = null;

            using (StreamReader file = File.OpenText(componentJsonFilePath))
            {
                JsonSerializer serializer = new JsonSerializer();
                json = (ComponentsJson)serializer.Deserialize(file, typeof(ComponentsJson));
                if (json == null || json.Components == null)
                {
                    return;
                }
            }

            foreach (KeyValuePair <string, ComponentJsonDefinition> entry in json.Components)
            {
                var componentId = entry.Key.GetComponentId();
                if (ComponentAssemblyLookup.ContainsKey(componentId))
                {
                    continue; // local wins
                }
                var packageId       = entry.Key;
                var targetFramework = entry.Value.TargetFramework;
                var version         = entry.Value.Version;

                var assembly = NugetHelper.TryLoadFromNuget(externalComponentsRefDirectory, targetFramework, packageId, version);
                if (assembly != null)
                {
                    RegisterComponent(services, builder, assembly, cacheItems);
                }
            }
        }