/// <summary>
 /// This method invokes the finder which scans the assemblies for the types and then loads the result into the type finder.
 /// Once the results are loaded, we update the cached type xml file
 /// </summary>
 /// <param name="typeList"></param>
 /// <param name="resolutionKind"> </param>
 /// <param name="finder"></param>
 /// <remarks>
 /// THIS METHODS IS NOT THREAD SAFE
 /// </remarks>
 private void LoadViaScanningAndUpdateCacheFile <T>(TypeList typeList, TypeResolutionKind resolutionKind, Func <IEnumerable <Type> > finder)
 {
     //we don't have a cache for this so proceed to look them up by scanning
     foreach (var t in finder())
     {
         typeList.AddType(t);
     }
     UpdateCachedPluginsFile <T>(typeList.GetTypes(), resolutionKind);
 }
示例#2
0
 public IActionResult Post([FromBody] Typee TypeP)
 {
     if (ModelState.IsValid)
     {
         if (!TypeList.AddType(TypeP))
         {
             return(BadRequest());
         }
         return(Created(Url.Action("Post", TypeP), TypeP));
     }
     return(BadRequest());
 }
示例#3
0
        private IEnumerable <Type> ResolveTypes <T>(
            Func <IEnumerable <Type> > finder,
            TypeResolutionKind resolutionType,
            bool cacheResult)
        {
            using (var readLock = new UpgradeableReadLock(_lock))
            {
                using (DisposableTimer.TraceDuration <PluginManager>(
                           String.Format("Starting resolution types of {0}", typeof(T).FullName),
                           String.Format("Completed resolution of types of {0}", typeof(T).FullName)))
                {
                    //check if the TypeList already exists, if so return it, if not we'll create it
                    var typeList = _types.SingleOrDefault(x => x.IsTypeList <T>(resolutionType));
                    //if we're not caching the result then proceed, or if the type list doesn't exist then proceed
                    if (!cacheResult || typeList == null)
                    {
                        //upgrade to a write lock since we're adding to the collection
                        readLock.UpgradeToWriteLock();

                        typeList = new TypeList <T>(resolutionType);

                        foreach (var t in finder())
                        {
                            typeList.AddType(t);
                        }

                        //only add the cache if we are to cache the results
                        if (cacheResult)
                        {
                            //add the type list to the collection
                            _types.Add(typeList);
                        }
                    }
                    return(typeList.GetTypes());
                }
            }
        }
        private IEnumerable <Type> ResolveTypes <T>(
            Func <IEnumerable <Type> > finder,
            TypeResolutionKind resolutionType,
            bool cacheResult)
        {
            using (var readLock = new UpgradeableReadLock(Locker))
            {
                var typesFound = new List <Type>();

                using (_logger.TraceDuration <PluginManager>(
                           String.Format("Starting resolution types of {0}", typeof(T).FullName),
                           String.Format("Completed resolution of types of {0}, found {1}", typeof(T).FullName, typesFound.Count)))
                {
                    //check if the TypeList already exists, if so return it, if not we'll create it
                    var typeList = _types.SingleOrDefault(x => x.IsTypeList <T>(resolutionType));

                    //need to put some logging here to try to figure out why this is happening: http://issues.umbraco.org/issue/U4-3505
                    if (cacheResult && typeList != null)
                    {
                        _logger.Logger.Debug <PluginManager>("Existing typeList found for {0} with resolution type {1}", () => typeof(T), () => resolutionType);
                    }

                    //if we're not caching the result then proceed, or if the type list doesn't exist then proceed
                    if (cacheResult == false || typeList == null)
                    {
                        //upgrade to a write lock since we're adding to the collection
                        readLock.UpgradeToWriteLock();

                        typeList = new TypeList <T>(resolutionType);

                        //we first need to look into our cache file (this has nothing to do with the 'cacheResult' parameter which caches in memory).
                        //if assemblies have not changed and the cache file actually exists, then proceed to try to lookup by the cache file.
                        if (RequiresRescanning == false && File.Exists(GetPluginListFilePath()))
                        {
                            var fileCacheResult = TryGetCachedPluginsFromFile <T>(resolutionType);

                            //here we need to identify if the CachedPluginNotFoundInFile was the exception, if it was then we need to re-scan
                            //in some cases the plugin will not have been scanned for on application startup, but the assemblies haven't changed
                            //so in this instance there will never be a result.
                            if (fileCacheResult.Exception != null && fileCacheResult.Exception is CachedPluginNotFoundInFileException)
                            {
                                _logger.Logger.Debug <PluginManager>("Tried to find typelist for type {0} and resolution {1} in file cache but the type was not found so loading types by assembly scan ", () => typeof(T), () => resolutionType);

                                //we don't have a cache for this so proceed to look them up by scanning
                                LoadViaScanningAndUpdateCacheFile <T>(typeList, resolutionType, finder);
                            }
                            else
                            {
                                if (fileCacheResult.Success)
                                {
                                    var successfullyLoadedFromCache = true;
                                    //we have a previous cache for this so we don't need to scan we just load what has been found in the file
                                    foreach (var t in fileCacheResult.Result)
                                    {
                                        try
                                        {
                                            //we use the build manager to ensure we get all types loaded, this is slightly slower than
                                            //Type.GetType but if the types in the assembly aren't loaded yet then we have problems with that.
                                            var type = BuildManager.GetType(t, true);
                                            typeList.AddType(type);
                                        }
                                        catch (Exception ex)
                                        {
                                            //if there are any exceptions loading types, we have to exist, this should never happen so
                                            //we will need to revert to scanning for types.
                                            successfullyLoadedFromCache = false;
                                            _logger.Logger.Error <PluginManager>("Could not load a cached plugin type: " + t + " now reverting to re-scanning assemblies for the base type: " + typeof(T).FullName, ex);
                                            break;
                                        }
                                    }
                                    if (successfullyLoadedFromCache == false)
                                    {
                                        //we need to manually load by scanning if loading from the file was not successful.
                                        LoadViaScanningAndUpdateCacheFile <T>(typeList, resolutionType, finder);
                                    }
                                    else
                                    {
                                        _logger.Logger.Debug <PluginManager>("Loaded plugin types {0} with resolution {1} from persisted cache", () => typeof(T), () => resolutionType);
                                    }
                                }
                            }
                        }
                        else
                        {
                            _logger.Logger.Debug <PluginManager>("Assembly changes detected, loading types {0} for resolution {1} by assembly scan", () => typeof(T), () => resolutionType);

                            //we don't have a cache for this so proceed to look them up by scanning
                            LoadViaScanningAndUpdateCacheFile <T>(typeList, resolutionType, finder);
                        }

                        //only add the cache if we are to cache the results
                        if (cacheResult)
                        {
                            //add the type list to the collection
                            var added = _types.Add(typeList);

                            _logger.Logger.Debug <PluginManager>("Caching of typelist for type {0} and resolution {1} was successful = {2}", () => typeof(T), () => resolutionType, () => added);
                        }
                    }
                    typesFound = typeList.GetTypes().ToList();
                }

                return(typesFound);
            }
        }