コード例 #1
0
        /// <summary>
        /// Creates an instance of <see cref="FileSystemProvider"/>, uses it to initialize
        /// a <seealso cref="FileModule"/>, and adds the latter to a module container,
        /// giving it the specified <paramref name="name"/> if not <see langword="null"/>.
        /// </summary>
        /// <remarks>
        /// OSX doesn't support <see cref="FileSystemWatcher" />, the parameter <paramref name="isImmutable" /> will be always <see langword="true"/>.
        /// </remarks>
        /// <typeparam name="TContainer">The type of the module container.</typeparam>
        /// <param name="this">The <typeparamref name="TContainer"/> on which this method is called.</param>
        /// <param name="name">The name.</param>
        /// <param name="baseRoute">The base route of the module.</param>
        /// <param name="fileSystemPath">The path of the directory to serve.</param>
        /// <param name="isImmutable"><see langword="true"/> if files and directories in
        /// <paramref name="fileSystemPath"/> are not expected to change during a web server's
        /// lifetime; <see langword="false"/> otherwise.</param>
        /// <param name="configure">A callback used to configure the module.</param>
        /// <returns><paramref name="this"/> with a <see cref="FileModule"/> added.</returns>
        /// <exception cref="NullReferenceException"><paramref name="this"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="fileSystemPath"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException"><paramref name="fileSystemPath"/> is not a valid local path.</exception>
        /// <seealso cref="FileModule"/>
        /// <seealso cref="FileSystemProvider"/>
        /// <seealso cref="IWebModuleContainer.Modules"/>
        /// <seealso cref="IComponentCollection{T}.Add"/>
        public static TContainer WithStaticFolder <TContainer>(
            this TContainer @this,
            string?name,
            string baseRoute,
            string fileSystemPath,
            bool isImmutable,
            Action <FileModule>?configure = null)
            where TContainer : class, IWebModuleContainer
        {
#pragma warning disable CA2000 // Call Dispose on disposable - Ownership of provider is transferred to module
            var provider = new FileSystemProvider(fileSystemPath, isImmutable);
#pragma warning restore CA2000
            try
            {
                var module = new FileModule(baseRoute, provider);
                return(WithModule(@this, name, module, configure));
            }
            catch
            {
                provider.Dispose();
                throw;
            }
        }