/// <summary>
        /// Runs a process inside the Orchard App Host that retrieves a value. The method is thread-safe.
        /// </summary>
        public static async Task <TResult> RunGet <TResult>(this IOrchardAppHost appHost, Func <IWorkContextScope, Task <TResult> > getterProcess, string shellName = ShellSettings.DefaultName, bool wrapInTransaction = true)
        {
            TResult result = default(TResult);
            Func <IWorkContextScope, Task> process = async scope => { result = await getterProcess(scope); };
            await appHost.Run(scope => process(scope), shellName, wrapInTransaction);

            return(result);
        }
예제 #2
0
        public void Dispose()
        {
            if (_host == null)
            {
                return;
            }

            _host.Dispose();
            _host = null;
        }
 private static Task Run(this IOrchardAppHost appHost, Func <IWorkContextScope, Task> process, string shellName, bool wrapInTransaction)
 {
     if (wrapInTransaction)
     {
         return(appHost.RunInTransaction(process, shellName));
     }
     else
     {
         return(appHost.Run(process, shellName));
     }
 }
 public static Task RunInTransaction(this IOrchardAppHost appHost, Func <IWorkContextScope, Task> process, string shellName)
 {
     return(appHost.Run(async scope =>
     {
         var transactionManager = scope.Resolve <ITransactionManager>();
         transactionManager.Demand();
         try
         {
             await process(scope);
         }
         catch (Exception)
         {
             transactionManager.Cancel();
             throw;
         }
     }, shellName));
 }
 public static Task Run <TService1, TService2, TService3, TService4, TService5, TService6, TService7>(this IOrchardAppHost appHost, Func <TService1, TService2, TService3, TService4, TService5, TService6, TService7, Task> process, string shellName = ShellSettings.DefaultName, bool wrapInTransaction = true)
 {
     return(appHost.Run(scope => process(scope.Resolve <TService1>(), scope.Resolve <TService2>(), scope.Resolve <TService3>(), scope.Resolve <TService4>(), scope.Resolve <TService5>(), scope.Resolve <TService6>(), scope.Resolve <TService7>()), shellName, wrapInTransaction));
 }
 public static Task RunInTransaction(this IOrchardAppHost appHost, Func <IWorkContextScope, Task> process)
 {
     return(appHost.RunInTransaction(process, ShellSettings.DefaultName));
 }
예제 #7
0
        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);
        }
 public AsyncBackgroundService(IOrchardAppHost orchardAppHost, ShellSettings shellSettings)
 {
     _orchardAppHost = orchardAppHost;
     _shellSettings  = shellSettings;
 }
예제 #9
0
        public static IContainer CreateHostContainer(IOrchardAppHost appHost, AppHostSettings settings, AppHostRegistrations registrations)
        {
            return(OrchardStarter.CreateHostContainer(builder =>
            {
                builder.RegisterType <AppHostEnvironment>().As <IHostEnvironment>().SingleInstance();

                // Needed also for shells, separately.
                RegisterAppDataFolderRoot(builder, settings.AppDataFolderPath).SingleInstance();

                RegisterVolatileProvider <AppHostVirtualPathMonitor, IVirtualPathMonitor>(builder);
                RegisterVolatileProvider <AppHostVirtualPathProvider, IVirtualPathProvider>(builder);
                RegisterVolatileProvider <AppHostWebSiteFolder, IWebSiteFolder>(builder);

                var shellRegistrations = new ShellContainerRegistrations
                {
                    Registrations = shellBuilder =>
                    {
                        // Despite imported assemblies being handled these registrations are necessary, because they are needed too early.
                        // Adding them as normal services, even with OrchardSuppressDependency, wouldn't work.

                        RegisterAppDataFolderRoot(shellBuilder, settings.AppDataFolderPath).InstancePerMatchingLifetimeScope("shell");

                        RegisterVolatileProviderForShell <AppHostVirtualPathMonitor, IVirtualPathMonitor>(shellBuilder);
                        RegisterVolatileProviderForShell <AppHostVirtualPathProvider, IVirtualPathProvider>(shellBuilder);
                        RegisterVolatileProviderForShell <AppHostWebSiteFolder, IWebSiteFolder>(shellBuilder);

                        if (registrations.ShellRegistrations != null)
                        {
                            registrations.ShellRegistrations(shellBuilder);
                        }
                    }
                };
                builder.RegisterInstance(shellRegistrations).As <IShellContainerRegistrations>();

                // Handling imported assemblies.
                if (settings.ImportedExtensions != null && settings.ImportedExtensions.Any())
                {
                    builder.RegisterType <ImportedExtensionsProvider>().As <IExtensionFolders, IExtensionLoader>().SingleInstance()
                    .WithParameter(new NamedParameter("extensions", settings.ImportedExtensions));
                }

                // Configuring extension loading.
                builder.RegisterType <ExtensionPathsProvider>().As <IExtensionPathsProvider>().SingleInstance()
                .WithParameter(new NamedParameter("hostSettings", settings));
                builder.RegisterType <AppHostExtensionFolders>().As <IExtensionFolders>().SingleInstance();
                builder.RegisterType <AppHostCoreExtensionLoader>().As <IExtensionLoader>().SingleInstance();
                builder.RegisterType <AppHostRawThemeExtensionLoader>().As <IExtensionLoader>().SingleInstance();

                if (settings.DisableConfiguratonCaches)
                {
                    builder.RegisterModule <ConfigurationCacheDisablingModule>();
                }

                if (settings.DisableExtensionMonitoring)
                {
                    builder.RegisterModule <ExtensionMonitoringDisablingModule>();
                }

                // Either we register MVC singletons or we need at least a new IOrchardShell implementation.
                builder.Register(ctx => RouteTable.Routes).SingleInstance();
                builder.Register(ctx => ModelBinders.Binders).SingleInstance();
                builder.Register(ctx => ViewEngines.Engines).SingleInstance();

                builder.RegisterType <LoggerService>().As <ILoggerService>().SingleInstance();

                builder.RegisterInstance(appHost).As <IOrchardAppHost>().ExternallyOwned();

                if (registrations.HostRegistrations != null)
                {
                    registrations.HostRegistrations(builder);
                }
            }));
        }