Example #1
0
        internal static void LoadAssembly(Assembly assembly, bool loadReferencedAssemblies,
                                          KnownAssembliesSet knownAssemblies, out Dictionary <string, EdmType> typesInLoading, out List <EdmItemError> errors)
        {
            object loaderCookie = null;

            LoadAssembly(assembly, loadReferencedAssemblies, knownAssemblies, null, null, ref loaderCookie, out typesInLoading, out errors);
        }
        internal static void LoadAssembly(
            Assembly assembly, bool loadReferencedAssemblies,
            KnownAssembliesSet knownAssemblies, EdmItemCollection edmItemCollection, Action<String> logLoadMessage, ref object loaderCookie,
            out Dictionary<string, EdmType> typesInLoading, out List<EdmItemError> errors)
        {
            Debug.Assert(
                loaderCookie == null || loaderCookie is Func<Assembly, ObjectItemLoadingSessionData, ObjectItemAssemblyLoader>,
                "This is a bad loader cookie");
            typesInLoading = null;
            errors = null;

            using (var lockedAssemblyCache = AquireLockedAssemblyCache())
            {
                var loadingData = new ObjectItemLoadingSessionData(
                    knownAssemblies, lockedAssemblyCache, edmItemCollection, logLoadMessage, loaderCookie);

                LoadAssembly(assembly, loadReferencedAssemblies, loadingData);
                loaderCookie = loadingData.LoaderCookie;
                // resolve references to top level types (base types, navigation properties returns and associations, and complex type properties)
                loadingData.CompleteSession();

                if (loadingData.EdmItemErrors.Count == 0)
                {
                    // do the validation for the all the new types
                    // Now, perform validation on all the new types
                    var validator = new EdmValidator();
                    validator.SkipReadOnlyItems = true;
                    validator.Validate(loadingData.TypesInLoading.Values, loadingData.EdmItemErrors);
                    // Update the global cache if there are no errors
                    if (loadingData.EdmItemErrors.Count == 0)
                    {
                        if (ObjectItemAssemblyLoader.IsAttributeLoader(loadingData.ObjectItemAssemblyLoaderFactory))
                        {
                            // we only cache items from the attribute loader globally, the 
                            // items loaded by convention will change depending on the cspace 
                            // provided.  cspace will have a cache of it's own for assemblies
                            UpdateCache(lockedAssemblyCache, loadingData.AssembliesLoaded);
                        }
                        else if (loadingData.EdmItemCollection != null
                                 &&
                                 ObjectItemAssemblyLoader.IsConventionLoader(loadingData.ObjectItemAssemblyLoaderFactory))
                        {
                            UpdateCache(loadingData.EdmItemCollection, loadingData.AssembliesLoaded);
                        }
                    }
                }

                if (loadingData.TypesInLoading.Count > 0)
                {
                    foreach (var edmType in loadingData.TypesInLoading.Values)
                    {
                        edmType.SetReadOnly();
                    }
                }

                // Update the out parameters once you are done with loading
                typesInLoading = loadingData.TypesInLoading;
                errors = loadingData.EdmItemErrors;
            }
        }
 internal static void LoadAssembly(
     Assembly assembly, bool loadReferencedAssemblies,
     KnownAssembliesSet knownAssemblies, out Dictionary<string, EdmType> typesInLoading, out List<EdmItemError> errors)
 {
     object loaderCookie = null;
     LoadAssembly(assembly, loadReferencedAssemblies, knownAssemblies, null, null, ref loaderCookie, out typesInLoading, out errors);
 }
Example #4
0
        internal static void LoadAssembly(Assembly assembly, bool loadReferencedAssemblies,
                                          KnownAssembliesSet knownAssemblies, EdmItemCollection edmItemCollection, Action <String> logLoadMessage, ref object loaderCookie, out Dictionary <string, EdmType> typesInLoading, out List <EdmItemError> errors)
        {
            Debug.Assert(loaderCookie == null || loaderCookie is Func <Assembly, ObjectItemLoadingSessionData, ObjectItemAssemblyLoader>, "This is a bad loader cookie");
            typesInLoading = null;
            errors         = null;

            using (LockedAssemblyCache lockedAssemblyCache = AssemblyCache.AquireLockedAssemblyCache())
            {
                ObjectItemLoadingSessionData loadingData = new ObjectItemLoadingSessionData(knownAssemblies, lockedAssemblyCache, edmItemCollection, logLoadMessage, loaderCookie);

                LoadAssembly(assembly, loadReferencedAssemblies, loadingData);
                loaderCookie = loadingData.LoaderCookie;
                // resolve references to top level types (base types, navigation properties returns and associations, and complex type properties)
                loadingData.CompleteSession();

                if (loadingData.EdmItemErrors.Count == 0)
                {
                    // do the validation for the all the new types
                    // Now, perform validation on all the new types
                    EdmValidator validator = new EdmValidator();
                    validator.SkipReadOnlyItems = true;
                    validator.Validate(loadingData.TypesInLoading.Values, loadingData.EdmItemErrors);
                    // Update the global cache if there are no errors
                    if (loadingData.EdmItemErrors.Count == 0)
                    {
                        if (ObjectItemAssemblyLoader.IsAttributeLoader(loadingData.ObjectItemAssemblyLoaderFactory))
                        {
                            // we only cache items from the attribute loader globally, the
                            // items loaded by convention will change depending on the cspace
                            // provided.  cspace will have a cache of it's own for assemblies
                            UpdateCache(lockedAssemblyCache, loadingData.AssembliesLoaded);
                        }
                        else if (loadingData.EdmItemCollection != null &&
                                 ObjectItemAssemblyLoader.IsConventionLoader(loadingData.ObjectItemAssemblyLoaderFactory))
                        {
                            UpdateCache(loadingData.EdmItemCollection, loadingData.AssembliesLoaded);
                        }
                    }
                }

                if (loadingData.TypesInLoading.Count > 0)
                {
                    foreach (EdmType edmType in loadingData.TypesInLoading.Values)
                    {
                        edmType.SetReadOnly();
                    }
                }

                // Update the out parameters once you are done with loading
                typesInLoading = loadingData.TypesInLoading;
                errors         = loadingData.EdmItemErrors;
            }
        }
Example #5
0
        internal ObjectItemLoadingSessionData(KnownAssembliesSet knownAssemblies, LockedAssemblyCache lockedAssemblyCache, EdmItemCollection edmItemCollection, Action <String> logLoadMessage, object loaderCookie)
        {
            Debug.Assert(loaderCookie == null || loaderCookie is Func <Assembly, ObjectItemLoadingSessionData, ObjectItemAssemblyLoader>, "This is a bad loader cookie");

            _typesInLoading      = new Dictionary <string, EdmType>(StringComparer.Ordinal);
            _errors              = new List <EdmItemError>();
            _knownAssemblies     = knownAssemblies;
            _lockedAssemblyCache = lockedAssemblyCache;
            _loadersThatNeedLevel1PostSessionProcessing = new HashSet <ObjectItemAssemblyLoader>();
            _loadersThatNeedLevel2PostSessionProcessing = new HashSet <ObjectItemAssemblyLoader>();
            _edmItemCollection    = edmItemCollection;
            _loadMessageLogger    = new LoadMessageLogger(logLoadMessage);
            _cspaceToOspace       = new Dictionary <EdmType, EdmType>();
            _loaderFactory        = (Func <Assembly, ObjectItemLoadingSessionData, ObjectItemAssemblyLoader>)loaderCookie;
            _originalLoaderCookie = loaderCookie;
            if (_loaderFactory == ObjectItemConventionAssemblyLoader.Create && _edmItemCollection != null)
            {
                foreach (KnownAssemblyEntry entry in _knownAssemblies.GetEntries(_loaderFactory, edmItemCollection))
                {
                    foreach (EdmType type in entry.CacheEntry.TypesInAssembly.OfType <EdmType>())
                    {
                        if (Helper.IsEntityType(type))
                        {
                            ClrEntityType entityType = (ClrEntityType)type;
                            _cspaceToOspace.Add(_edmItemCollection.GetItem <StructuralType>(entityType.CSpaceTypeName), entityType);
                        }
                        else if (Helper.IsComplexType(type))
                        {
                            ClrComplexType complexType = (ClrComplexType)type;
                            _cspaceToOspace.Add(_edmItemCollection.GetItem <StructuralType>(complexType.CSpaceTypeName), complexType);
                        }
                        else if (Helper.IsEnumType(type))
                        {
                            ClrEnumType enumType = (ClrEnumType)type;
                            _cspaceToOspace.Add(_edmItemCollection.GetItem <EnumType>(enumType.CSpaceTypeName), enumType);
                        }
                        else
                        {
                            Debug.Assert(Helper.IsAssociationType(type));
                            _cspaceToOspace.Add(_edmItemCollection.GetItem <StructuralType>(type.FullName), type);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Loads the OSpace types in the assembly and returns them as a dictionary
        /// </summary>
        /// <param name="assembly">The assembly to load</param>
        /// <returns>A mapping from names to OSpace EdmTypes</returns>
        internal static Dictionary <string, EdmType> LoadTypesExpensiveWay(Assembly assembly)
        {
            Dictionary <string, EdmType> typesInLoading = null;

            List <EdmItemError> errors;
            KnownAssembliesSet  knownAssemblies = new KnownAssembliesSet();

            AssemblyCache.LoadAssembly(assembly, false /*loadAllReferencedAssemblies*/,
                                       knownAssemblies, out typesInLoading, out errors);

            // Check for errors
            if (errors.Count != 0)
            {
                throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage(errors));
            }

            return(typesInLoading);
        }
 internal KnownAssembliesSet(KnownAssembliesSet set)
 {
     _assemblies = new Dictionary<Assembly, KnownAssemblyEntry>(set._assemblies);
 }
        internal ObjectItemLoadingSessionData(KnownAssembliesSet knownAssemblies, LockedAssemblyCache lockedAssemblyCache, EdmItemCollection edmItemCollection, Action<String> logLoadMessage, object loaderCookie)
        {
            Debug.Assert(loaderCookie == null || loaderCookie is Func<Assembly, ObjectItemLoadingSessionData, ObjectItemAssemblyLoader>, "This is a bad loader cookie");
            
            _typesInLoading = new Dictionary<string, EdmType>(StringComparer.Ordinal);
            _errors = new List<EdmItemError>();
            _knownAssemblies = knownAssemblies;
            _lockedAssemblyCache = lockedAssemblyCache;
            _loadersThatNeedLevel1PostSessionProcessing = new HashSet<ObjectItemAssemblyLoader>();
            _loadersThatNeedLevel2PostSessionProcessing = new HashSet<ObjectItemAssemblyLoader>();
            _edmItemCollection = edmItemCollection;
            _loadMessageLogger = new LoadMessageLogger(logLoadMessage);
            _cspaceToOspace = new Dictionary<EdmType, EdmType>();
            _loaderFactory = (Func<Assembly, ObjectItemLoadingSessionData, ObjectItemAssemblyLoader>)loaderCookie;
            _originalLoaderCookie = loaderCookie;
            if (_loaderFactory == ObjectItemConventionAssemblyLoader.Create && _edmItemCollection != null)
            {
                foreach (KnownAssemblyEntry entry in _knownAssemblies.GetEntries(_loaderFactory, edmItemCollection))
                {
                    foreach (EdmType type in entry.CacheEntry.TypesInAssembly.OfType<EdmType>())
                    {
                        if (Helper.IsEntityType(type))
                        {
                            ClrEntityType entityType = (ClrEntityType)type;
                            _cspaceToOspace.Add(_edmItemCollection.GetItem<StructuralType>(entityType.CSpaceTypeName), entityType);
                        }
                        else if (Helper.IsComplexType(type))
                        {
                            ClrComplexType complexType = (ClrComplexType)type;
                            _cspaceToOspace.Add(_edmItemCollection.GetItem<StructuralType>(complexType.CSpaceTypeName), complexType);
                        }
                        else if(Helper.IsEnumType(type))
                        {
                            ClrEnumType enumType = (ClrEnumType)type;
                            _cspaceToOspace.Add(_edmItemCollection.GetItem<EnumType>(enumType.CSpaceTypeName), enumType);
                        }
                        else
                        {
                            Debug.Assert(Helper.IsAssociationType(type));
                            _cspaceToOspace.Add(_edmItemCollection.GetItem<StructuralType>(type.FullName), type);
                        }
                    }
                }
            }

        }
 internal KnownAssembliesSet(KnownAssembliesSet set)
 {
     _assemblies = new Dictionary <Assembly, KnownAssemblyEntry>(set._assemblies);
 }
Example #10
0
        private static bool LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly,
                                                  bool loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action <String> logLoadMessage)
        {
            // Check if its loaded in the cache - if the call is for loading referenced assemblies, make sure that all referenced
            // assemblies are also loaded
            KnownAssemblyEntry entry;

            if (objectItemCollection._knownAssemblies.TryGetKnownAssembly(assembly, objectItemCollection._loaderCookie, edmItemCollection, out entry))
            {
                // Proceed if only we need to load the referenced assemblies and they are not loaded
                if (loadReferencedAssemblies == false)
                {
                    // don't say we loaded anything, unless we actually did before
                    return(entry.CacheEntry.TypesInAssembly.Count != 0);
                }
                else if (entry.ReferencedAssembliesAreLoaded == true)
                {
                    // this assembly was part of a all hands reference search
                    return(true);
                }
            }

            lock (objectItemCollection.LoadAssemblyLock)
            {
                // Check after acquiring the lock, since the known assemblies might have got modified
                // Check if the assembly is already loaded. The reason we need to check if the assembly is already loaded, is that
                if (objectItemCollection._knownAssemblies.TryGetKnownAssembly(assembly, objectItemCollection._loaderCookie, edmItemCollection, out entry))
                {
                    // Proceed if only we need to load the referenced assemblies and they are not loaded
                    if (loadReferencedAssemblies == false || entry.ReferencedAssembliesAreLoaded == true)
                    {
                        return(true);
                    }
                }

                Dictionary <string, EdmType> typesInLoading;
                List <EdmItemError>          errors;
                KnownAssembliesSet           knownAssemblies;

                if (objectItemCollection != null)
                {
                    knownAssemblies = new KnownAssembliesSet(objectItemCollection._knownAssemblies);
                }
                else
                {
                    knownAssemblies = new KnownAssembliesSet();
                }

                // Load the assembly from the cache
                AssemblyCache.LoadAssembly(assembly, loadReferencedAssemblies, knownAssemblies, edmItemCollection, logLoadMessage, ref objectItemCollection._loaderCookie, out typesInLoading, out errors);

                // Throw if we have encountered errors
                if (errors.Count != 0)
                {
                    throw EntityUtil.InvalidSchemaEncountered(Helper.CombineErrorMessage(errors));
                }

                // We can encounter new assemblies, but they may not have any time in them
                if (typesInLoading.Count != 0)
                {
                    // No errors, so go ahead and add the types and make them readonly
                    // The existence of the loading lock tells us whether we should be thread safe or not, if we need
                    // to be thread safe, then we need to use AtomicAddRange. We don't need to actually use the lock
                    // because the caller should have done it already
                    // Recheck the assemblies added, another list is created just to match up the collection type
                    // taken in by AtomicAddRange()
                    List <GlobalItem> globalItems = new List <GlobalItem>();
                    foreach (EdmType edmType in typesInLoading.Values)
                    {
                        globalItems.Add(edmType);

                        string cspaceTypeName = "";
                        try
                        {
                            // Also populate the ocmapping information
                            if (Helper.IsEntityType(edmType))
                            {
                                cspaceTypeName = ((ClrEntityType)edmType).CSpaceTypeName;
                                objectItemCollection._ocMapping.Add(cspaceTypeName, edmType);
                            }
                            else if (Helper.IsComplexType(edmType))
                            {
                                cspaceTypeName = ((ClrComplexType)edmType).CSpaceTypeName;
                                objectItemCollection._ocMapping.Add(cspaceTypeName, edmType);
                            }
                            else if (Helper.IsEnumType(edmType))
                            {
                                cspaceTypeName = ((ClrEnumType)edmType).CSpaceTypeName;
                                objectItemCollection._ocMapping.Add(cspaceTypeName, edmType);
                            }
                            // for the rest of the types like a relationship type, we do not have oc mapping,
                            // so we don't keep that information
                        }
                        catch (ArgumentException e)
                        {
                            throw new MappingException(Strings.Mapping_CannotMapCLRTypeMultipleTimes(cspaceTypeName), e);
                        }
                    }

                    // Create a new ObjectItemCollection and add all the global items to it.
                    // Also copy all the existing items from the existing collection
                    objectItemCollection.AtomicAddRange(globalItems);
                }


                // Update the value of known assemblies
                objectItemCollection._knownAssemblies = knownAssemblies;

                foreach (Assembly loadedAssembly in knownAssemblies.Assemblies)
                {
                    CollectIfViewGenAssembly(loadedAssembly);
                }

                return(typesInLoading.Count != 0);
            }
        }