Пример #1
0
        /// <summary>
        /// Copies configuration from poco <paramref name="src"/> to <paramref name="dst"/>.
        ///
        /// Since packageloaders are type names in the poco, any new type will be loaded and instantiated.
        /// </summary>
        /// <param name="src"></param>
        /// <param name="dst"></param>
        public static void Assign(PackageFileProviderOptionsRecord src, IPackageFileProviderOptions dst)
        {
            dst.AllowOpenFiles          = src.AllowOpenFiles;
            dst.ReuseFailedResult       = src.ReuseFailedResult;
            dst.MaxMemorySnapshotLength = src.MaxMemorySnapshotLength;
            dst.MaxTempSnapshotLength   = src.MaxTempSnapshotLength;

            // Update packageloaders.
            // Make list of new types
            List <Type> newPackageLoaders = (src.PackageLoaders ?? new string[0]).Select(typeName => Type.GetType(typeName, true)).ToList();
            // Make list of old types
            Dictionary <Type, IPackageLoader> oldPackageLoaders = (dst.PackageLoaders ?? new IPackageLoader[0]).ToDictionary(pl => pl.GetType());

            // Make new array
            IPackageLoader[] newArray = new IPackageLoader[newPackageLoaders.Count];
            for (int i = 0; i < newArray.Length; i++)
            {
                Type           type = newPackageLoaders[i];
                IPackageLoader pl;
                if (!oldPackageLoaders.TryGetValue(type, out pl))
                {
                    pl = (IPackageLoader)Activator.CreateInstance(type);
                }
                newArray[i] = pl;
            }
            // A new reference must always be created to trigger reload as per contract.
            dst.PackageLoaders = newArray;
        }
Пример #2
0
 /// <summary>
 /// Assign options and return <paramref name="fileProvider"/>.
 /// </summary>
 /// <param name="fileProvider"></param>
 /// <param name="options"></param>
 /// <returns>fileProvider</returns>
 public static IPackageFileProvider SetOptions(this IPackageFileProvider fileProvider, IPackageFileProviderOptions options)
 {
     fileProvider.Options = options;
     return(fileProvider);
 }
 /// <summary>
 /// Set policy whether to cache and reuse failed open package attempt.
 ///
 /// If this policy is allowed, then <see cref="IPackageFileProvider"/> remembers what package files
 /// could not be opened. Error result can be evicted just like other cached info.
 ///
 /// If this policy is disallowed, then <see cref="IPackageFileProvider"/> will retry opening packages
 /// if they are requested again, even if they had failed previously.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="reuseFailedResult"></param>
 /// <returns></returns>
 public static IPackageFileProviderOptions SetReuseFailedResult(this IPackageFileProviderOptions options, bool reuseFailedResult)
 {
     options.ReuseFailedResult = reuseFailedResult;
     return(options);
 }
Пример #4
0
 /// <summary>
 /// Configure package file provider to log errors and then throw them.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="logger"></param>
 /// <returns></returns>
 public static IPackageFileProviderOptions SetToThrowAndLogErrors(this IPackageFileProviderOptions options, ILogger logger)
 => options.SetErrorHandler(e => {
     logger.LogError(e.LoadError, "Failed to open package file: {0}", options);
     return(false);
 });
 /// <summary>
 /// Set maximum memory temp file snapshot length. If value is over 0, then temp file snapshots are allowed.
 ///
 /// Note, that the options must be configured with TempProvider for temp files to work.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="maxTempFileSnapshotLength"></param>
 /// <returns></returns>
 public static IPackageFileProviderOptions SetTempFileSnapshotLength(this IPackageFileProviderOptions options, long maxTempFileSnapshotLength)
 {
     options.MaxTempSnapshotLength = maxTempFileSnapshotLength;
     return(options);
 }
 /// <summary>
 /// Set policy whether open files is allowed or not.
 ///
 /// If open files is allowed, then <see cref="IPackageFileProvider"/> can keep open files
 /// and keep them open for prolonged time.
 ///
 /// If the policy is disallowed, then the <see cref="IPackageFileProvider"/> will open files
 /// only to make snapshot copies of them.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="allowOpenFiles"></param>
 /// <returns></returns>
 public static IPackageFileProviderOptions SetAllowOpenFiles(this IPackageFileProviderOptions options, bool allowOpenFiles)
 {
     options.AllowOpenFiles = allowOpenFiles;
     return(options);
 }
 /// <summary>
 /// Add package loaders.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="loaders"></param>
 /// <returns></returns>
 public static IPackageFileProviderOptions AddPackageLoaders(this IPackageFileProviderOptions options, IEnumerable <IPackageLoader> loaders)
 => options.SetPackageLoaders(options.PackageLoaders.Concat(loaders).ToArray());
 /// <summary>
 /// Add package loaders.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="loaders"></param>
 /// <returns></returns>
 public static IPackageFileProviderOptions AddPackageLoaders(this IPackageFileProviderOptions options, params IPackageLoader[] loaders)
 => options.SetPackageLoaders(options.PackageLoaders.Concat(loaders).ToArray());
 /// <summary>
 /// Add package loader.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="loader"></param>
 /// <returns></returns>
 public static IPackageFileProviderOptions AddPackageLoader(this IPackageFileProviderOptions options, IPackageLoader loader)
 => options.SetPackageLoaders(options.PackageLoaders.Concat(Enumerable.Repeat(loader, 1).ToArray()));
 /// <summary>
 /// Assign new set of package loaders.
 ///
 /// <param name="options"></param>
 /// <param name="packageLoaders"></param>
 /// </summary>
 /// <returns>file provider</returns>
 public static IPackageFileProviderOptions SetPackageLoaders(this IPackageFileProviderOptions options, IEnumerable <IPackageLoader> packageLoaders)
 {
     options.PackageLoaders = packageLoaders;
     return(options);
 }
 /// <summary>
 /// Configure file provider to let package loading errors be thrown.
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public static IPackageFileProviderOptions SetToThrowErrors(this IPackageFileProviderOptions options)
 => options.SetErrorHandler(throw_errors);
 /// <summary>
 /// Configure package file provider to suppress package loading errors.
 /// When package loading fails and error is suppressed, then the file is treated as it is normal non-package file.
 /// </summary>
 /// <param name="options"></param>
 /// <returns></returns>
 public static IPackageFileProviderOptions SetToSuppressErrors(this IPackageFileProviderOptions options)
 => options.SetErrorHandler(suppress_errors);
 /// <summary>
 /// Assign package loading error handler. Logging can be added here.
 ///
 /// Set this delegate to null to let exception be thrown to caller.
 /// Usually when handling <see cref="IFileInfo"/> or <see cref="IDirectoryContents"/>.
 ///
 /// If delegate returns true, then the exception is suppressed and the
 /// package is handled as a regular file and not opened.
 /// </summary>
 /// <param name="options"></param>
 /// <param name="errorHandler"></param>
 /// <returns></returns>
 public static IPackageFileProviderOptions SetErrorHandler(this IPackageFileProviderOptions options, Func <PackageEvent, bool> errorHandler)
 {
     options.ErrorHandler = errorHandler;
     return(options);
 }