Example #1
0
        /// <summary>
        /// This method creates a thread-safe lifetime scope DI container to isolate unit of work in a separate database transaction.
        /// To commit changes to database, call <see cref="UnitOfWorkScope.CommitAndClose"/> at the end of the 'using' block.
        /// </summary>
        /// <remarks>
        /// In most cases it is preferred to use a <see cref="RhetosHost"/> instance, instead of this static method, for better control over the DI container.
        /// The static method is useful in some special cases, for example to optimize LINQPad scripts that can reuse the external static instance
        /// after recompiling the script.
        /// </remarks>
        /// <param name="rhetosAppAssemblyPath">
        /// Path to assembly where the CreateHostBuilder method is located.
        /// </param>
        /// <param name="registerCustomComponents">
        /// Register custom components that may override application's services and plugins.
        /// This is commonly used by utilities and tests that need to override host application's Rhetos components or register additional plugins.
        /// <para>
        /// Note that the transaction-scope component registration will not affect singleton components.
        /// To customize the behavior of singleton components use <see cref="RhetosHost"/> directly.
        /// </para>
        /// </param>
        /// /// <param name="configureServices">
        /// Configures host application's dependency injection components and configuration.
        /// </param>
        public static UnitOfWorkScope CreateScope(
            string rhetosAppAssemblyPath,
            Action <ContainerBuilder> registerCustomComponents = null,
            Action <HostBuilderContext, IServiceCollection> configureServices = null)
        {
            if (_singleRhetosHost == null)
            {
                lock (_singleContainerLock)
                    if (_singleRhetosHost == null)
                    {
                        _singleRhetosHostAssemblyPath = rhetosAppAssemblyPath;
                        _singleRhetosHost             = RhetosHost.CreateFrom(rhetosAppAssemblyPath, ConfigureRhetosHost, OverrideHostLogging + configureServices);
                    }
            }

            if (_singleRhetosHostAssemblyPath != rhetosAppAssemblyPath)
            {
                throw new FrameworkException($"Static {nameof(LinqPadRhetosHost)}.{nameof(CreateScope)} cannot be used for different" +
                                             $" application contexts: Provided folder 1: '{_singleRhetosHostAssemblyPath}', folder 2: '{rhetosAppAssemblyPath}'." +
                                             $" Use a {nameof(RhetosHost)} instances instead.");
            }

            return(_singleRhetosHost.CreateScope(registerCustomComponents));
        }
Example #2
0
 public RhetosScopeServiceProvider(RhetosHost rhetosHost, IUserInfo rhetosUser)
 {
     unitOfWorkScope = rhetosHost.CreateScope(builder => builder.RegisterInstance(rhetosUser));
 }