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);
                }
            }
        }
        private void RegisterComponent(IServiceCollection services, IMvcBuilder builder,
                                       Assembly assembly,
                                       List <HisarCacheAttribute> cacheItems)
        {
            var assemblyName = assembly.GetName().Name;
            var componentId  = assembly.GetComponentId();

            ComponentAssemblyLookup.Add(componentId, assembly);

            AssemblyLoadContext.Default.Resolving += DefaultResolving;

            if (cacheItems != null)
            {
                cacheItems.AddRange(assembly.GetTypesAttributes <HisarCacheAttribute>());
            }

            try
            {
                var startupType = StartupLoader.FindStartupType(assemblyName, _env.EnvironmentName);
                if (startupType != null)
                {
                    var startup = StartupTypeLoader.CreateHisarConventionBasedStartup(startupType, ServiceProvider, _env);
                    StartupLookup.Add(componentId, startup);
                    startup.ConfigureServices(services);

                    services.AddMenuBuilders(startupType);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                AssemblyLoadContext.Default.Resolving -= null;
            }

            var manager      = builder.PartManager;
            var assemblyPart = new AssemblyPart(assembly);

            if (!manager.ApplicationParts.Contains(assemblyPart))
            {
                manager.ApplicationParts.Add(assemblyPart);
            }
        }
Exemple #3
0
        private void RegisterComponent(IServiceCollection services, IMvcBuilder builder,
                                       Assembly assembly,
                                       List <HisarCacheAttribute> cacheItems)
        {
            var assemblyName = assembly.GetName().Name;
            var componentId  = assembly.GetComponentId();

            ComponentAssemblyLookup.Add(componentId, assembly);

            if (cacheItems != null)
            {
                cacheItems.AddRange(assembly.GetTypesAttributes <HisarCacheAttribute>());
            }

            try
            {
                var startupType = StartupLoader.FindStartupType(assemblyName, _env.EnvironmentName);
                if (startupType != null)
                {
                    var startup = StartupTypeLoader.CreateHisarConventionBasedStartup(startupType, ServiceProvider, _env);
                    StartupLookup.Add(componentId, startup);
                    startup.ConfigureServices(services);

                    services.AddMenuBuilders(startupType);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            var components  = assembly.GetTypes().ToArray();
            var controllers = components.Where(c => IsController(c.GetTypeInfo())).ToList();

            builder.PartManager.ApplicationParts.Add(new TypesPart(components));
        }