/// <summary> /// Load component based on given configuration /// </summary> /// <param name="configuration"> Application configuration. </param> /// <param name="componentPath"> Path to executable code of the component. </param> protected virtual void LoadComponent(IApplicationConfiguration configuration, string componentPath) { Logger.Info(m => m("Загружается компонент из {0}", componentPath)); AppDomainSetup appDomainSetup = this.appDomainSetupFactory.CreateAppDomainSetup(componentPath, AppDomain.CurrentDomain); AppDomain appDomain = this.appDomainFactory.CreateAppDomain(componentPath, AppDomain.CurrentDomain, appDomainSetup); var component = this.componentFactory.CreateComponent(componentPath, appDomain, AppDomain.CurrentDomain); component.Start(); var componentConfiguration = new ComponentConfiguration { ComponentDomain = appDomain, Component = component }; configuration.ComponentConfigurations.Add(componentConfiguration); }
private StartResult LoadAndRunComponents(CancellationToken cancellationToken) { var loader = PluginLoader.CreateFromAssemblyFile( this.ComponentConfig.Path, new[] { typeof(Borderline.IConfiguration), typeof(DependencyContext) }, this.ComponentConfig.SharedLibraryPath); var entryAssemblyName = loader.LoadDefaultAssembly().GetName(false); var dc = DependencyContext.Load(loader.LoadDefaultAssembly()); var factories = new List<IHostableComponentFactory>(); var componentAssemblies = dc.GetRuntimeAssemblyNames(RuntimeEnvironment.GetRuntimeIdentifier()) .Where(n => n != entryAssemblyName) .Select(loader.LoadAssembly) .ToArray(); foreach (var assembly in componentAssemblies) { var componentFactoryTypes = assembly .GetExportedTypes() .Where(t => t.GetInterfaces().Any(i => i == typeof(IHostableComponentFactory))) .ToArray(); if (!componentFactoryTypes.Any()) { continue; } var instances = componentFactoryTypes .Select(Activator.CreateInstance) .Cast<IHostableComponentFactory>() .ToArray(); factories.AddRange(instances); } var cfg = ComponentConfiguration.Create(this.appConfiguration); this.SetSharedLibrariesConfiguration(componentAssemblies); var componentLoader = new ComponentAssemblyLoader(loader); var components = factories .Select(f => f.CreateComponent(componentLoader, cfg)) .ToArray(); var startTask = Task.Factory.StartNew( () => { foreach (var component in components) { component.Start(); } }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default); return new StartResult { Components = components, StartTask = startTask }; }