public static async Task RunSample(AppHostSettings settings) { Console.WriteLine("=== Loop sample starts === "); Console.WriteLine("Press Ctrl+C to exit the loop."); using (var host = await OrchardAppHostFactory.StartHost(settings)) { var run = true; // Hit Ctrl+C to exit the loop, but not the app (other samples will follow up). Console.CancelKeyPress += (sender, e) => { e.Cancel = true; run = false; }; while (run) { Console.WriteLine("Cycle starts."); await host.Run <ILoggerService, IClock>((logger, clock) => Task.Run(() => { logger.Error("Test log entry."); Console.WriteLine(clock.UtcNow.ToString()); })); // Another overload of Run() for simple transaction handling and for using the work context scope // directly. await host.RunInTransaction(async scope => { Console.WriteLine(scope.Resolve <ISiteService>().GetSiteSettings().SiteName); // Simulating an async call. Because of this the delegate is marked as async (and we don't // need to wrap it into a Task.Run()). await Task.Delay(3000); Console.WriteLine(scope.Resolve <ISiteService>().GetSiteSettings().SiteName); Console.WriteLine(scope.Resolve <ShellSettings>().Name); }); Console.WriteLine("Cycle ends."); Console.WriteLine(); } } Console.WriteLine("=== Loop sample ended === "); }
public static async Task RunSample(AppHostSettings settings) { Console.WriteLine("=== Transient host sample starts === "); using (var host = await OrchardAppHostFactory.StartTransientHost(settings, null, null)) { await host.Run <ITestService, ILoggerService, IClock>((testService, logger, clock) => Task.Run(() => { testService.Test(); // Custom dependencies from imported and enabled extensions work too. logger.Error("Test log entry from transient shell."); Console.WriteLine(clock.UtcNow.ToString()); })); // You can even run such "getters" to just fetch something from Orchard. var utcNow = await host.RunGet(scope => Task.Run(() => scope.Resolve <IClock>().UtcNow)); } Console.WriteLine("=== Transient host sample ended === "); }
public static async Task RunSample(AppHostSettings settings) { Console.WriteLine("=== Setup sample starts === "); using (var host = await OrchardAppHostFactory.StartHost(settings)) { // We can even run the setup on a new shell. A project reference to Orchard.Setup is needed. // The setup shouldn't run in a transaction. await host.Run <ISetupService, ShellSettings>((setupService, shellSettings) => Task.Run(() => { Console.WriteLine("Running setup for the following shell: " + shellSettings.Name); setupService.Setup(new SetupContext { SiteName = "Test", AdminUsername = "******", AdminPassword = "******", DatabaseProvider = "SqlCe", Recipe = setupService.Recipes().Where(recipe => recipe.Name == "Default").Single() }); Console.WriteLine("Setup done"); }), wrapInTransaction : false); } using (var host = await OrchardAppHostFactory.StartHost(settings)) { // After setup everything else should be run in a newly started host. await host.Run <ISiteService, ShellSettings>((siteService, shellSettings) => Task.Run(() => { Console.WriteLine(siteService.GetSiteSettings().SiteName); Console.WriteLine(shellSettings.Name); })); } Console.WriteLine("=== Setup host sample ended === "); }
private async Task LoadHost() { var moduleFolderPaths = new List <string>(); // Since Hast.Core either exists or not we need to start by probing for the Hast.Abstractions folder. var abstractionsPath = Path.GetDirectoryName(GetType().Assembly.Location); var currentDirectory = Path.GetFileName(abstractionsPath); if (currentDirectory.Equals("Debug", StringComparison.OrdinalIgnoreCase) || currentDirectory.Equals("Release", StringComparison.OrdinalIgnoreCase)) { abstractionsPath = Path.GetDirectoryName(abstractionsPath); } currentDirectory = Path.GetFileName(abstractionsPath); if (currentDirectory.Equals("bin", StringComparison.OrdinalIgnoreCase)) { abstractionsPath = Path.GetDirectoryName(abstractionsPath); } // Now we're at the level above the current project's folder. abstractionsPath = Path.GetDirectoryName(abstractionsPath); var coreFound = false; while (abstractionsPath != null && !coreFound) { var abstractionsSubFolder = Path.Combine(abstractionsPath, "Hast.Abstractions"); if (Directory.Exists(abstractionsSubFolder)) { abstractionsPath = abstractionsSubFolder; coreFound = true; } else { abstractionsPath = Path.GetDirectoryName(abstractionsPath); } } // There won't be an Abstractions folder, nor a Core one when the app is being run from a deployment folder // (as opposed to a solution). if (!string.IsNullOrEmpty(abstractionsPath)) { moduleFolderPaths.Add(abstractionsPath); } if (_configuration.Flavor == HastlayerFlavor.Developer) { var corePath = !string.IsNullOrEmpty(abstractionsPath) ? Path.Combine(Path.GetDirectoryName(abstractionsPath), "Hast.Core") : null; if (corePath != null && Directory.Exists(corePath)) { moduleFolderPaths.Add(corePath); } } var importedExtensions = new[] { typeof(Hastlayer).Assembly, typeof(IProxyGenerator).Assembly, typeof(IHardwareImplementationComposer).Assembly, typeof(ITransformer).Assembly, typeof(Nexys4DdrManifestProvider).Assembly } .ToList(); if (_configuration.Flavor == HastlayerFlavor.Client) { importedExtensions.Add(typeof(Remote.Client.RemoteTransformer).Assembly); } // Adding imported extensions last so they can override anything. importedExtensions.AddRange(_configuration.Extensions); var settings = new AppHostSettings { // Setting a custom path so if the parent app is also an AppHost app then with the default settings // those won't clash. AppDataFolderPath = "~/Hastlayer/App_Data", ImportedExtensions = importedExtensions, DefaultShellFeatureStates = new[] { new DefaultShellFeatureState { ShellName = ShellName, EnabledFeatures = importedExtensions.Select(extension => extension.ShortName()) } }, ModuleFolderPaths = moduleFolderPaths }; var registrations = new AppHostRegistrations { HostRegistrations = builder => builder .RegisterType <HardwareExecutionEventHandlerHolder>() .As <IHardwareExecutionEventHandlerHolder>() .SingleInstance() }; _host = await OrchardAppHostFactory.StartTransientHost(settings, registrations, null); await _host.Run <IHardwareExecutionEventHandlerHolder>(proxy => Task.Run(() => proxy.RegisterExecutedOnHardwareEventHandler(eventArgs => ExecutedOnHardware?.Invoke(this, eventArgs)))); // Enable all loaded features. This is needed so extensions just added to the solution, but not referenced // anywhere in the current app can contribute dependencies. await _host .Run <Orchard.Environment.Features.IFeatureManager>( (featureManager) => { featureManager.EnableFeatures(featureManager.GetAvailableFeatures().Select(feature => feature.Id), true); return(Task.CompletedTask); }, ShellName, false); }