예제 #1
0
        internal ServiceScope CreateChildScope(IServiceScopeFactory rootScopeFactory)
        {
            var       scopedRoot           = rootScopeFactory.CreateScope();
            var       preferInterpretation = (Container as Container).PreferInterpretation;
            Container scopedContext        = Container.OpenScope(preferInterpretation: preferInterpretation) as Container;

            Rules rules = scopedContext.Rules;

            foreach (var unknownServiceResolver in scopedContext.Rules.UnknownServiceResolvers)
            {
                rules = rules.WithoutUnknownServiceResolver(unknownServiceResolver);
            }

            var resolver = scopedContext.With(r => rules.WithUnknownServiceResolvers(request =>
            {
                return(new DelegateFactory(_ => scopedRoot.ServiceProvider.GetService(request.ServiceType), setup: _rootScopeFactorySetup));
            }));

            var scope = new ServiceScope(resolver, scopedRoot);

            ChildScopes.TryAdd(scope, null);

            scope.DisposalTask.ContinueWith(t => ChildScopes.TryRemove(scope, out object _));

            return(scope);
        }
예제 #2
0
        internal ServiceScope CreateChildScope(IServiceScopeFactory rootScopeFactory)
        {
            var       scopedRoot    = rootScopeFactory.CreateScope();
            Container scopedContext = Container.OpenScope() as Container;

            Rules rules = scopedContext.Rules;

            foreach (var unknownServiceResolver in scopedContext.Rules.UnknownServiceResolvers)
            {
                rules = rules.WithoutUnknownServiceResolver(unknownServiceResolver);
            }

            var resolver = scopedContext.With(r => rules.WithUnknownServiceResolvers(request =>
            {
                return(new DelegateFactory(_ => scopedRoot.ServiceProvider.GetService(request.ServiceType)));
            }));

            var scope = new ServiceScope(resolver, scopedRoot);

            ChildScopes.Add(scope);

            scope.DisposalTask.ContinueWith(t => ChildScopes.Remove(scope));

            return(scope);
        }
예제 #3
0
        /// <summary>
        /// Removes any program elements defined in the given file. If the scope is defined entirely
        /// within the given file, then it removes itself from its parent.
        /// </summary>
        /// <param name="fileName">The file to remove.</param>
        /// <returns>A collection of any unresolved scopes that result from removing the file. The
        /// caller is responsible for re-resolving these as appropriate.</returns>
        public virtual Collection <IScope> RemoveFile(string fileName)
        {
            if (LocationDictionary.ContainsKey(fileName))
            {
                if (LocationDictionary.Count == 1)
                {
                    //this scope exists solely in the file to be deleted
                    if (ParentScope != null)
                    {
                        ParentScope.RemoveChild(this);
                        ParentScope = null;
                    }
                }
                else
                {
                    Debug.WriteLine("Found Scope with more than one location. Should this be possible?");
                    foreach (var loc in Locations)
                    {
                        Debug.WriteLine("Location: " + loc);
                    }

                    //Remove the file from the children
                    var unresolvedChildScopes = new List <IScope>();
                    foreach (var child in ChildScopes.ToList())
                    {
                        var result = child.RemoveFile(fileName);
                        if (result != null)
                        {
                            unresolvedChildScopes.AddRange(result);
                        }
                    }
                    if (unresolvedChildScopes.Count > 0)
                    {
                        foreach (var child in unresolvedChildScopes)
                        {
                            AddChildScope(child);
                        }
                    }
                    //remove method calls
                    var callsInFile = MethodCallCollection.Where(call => call.Location.SourceFileName == fileName).ToList();
                    foreach (var call in callsInFile)
                    {
                        MethodCallCollection.Remove(call);
                    }
                    //remove declared variables
                    var declsInFile = DeclaredVariablesDictionary.Where(kvp => kvp.Value.Location.SourceFileName == fileName).ToList();
                    foreach (var kvp in declsInFile)
                    {
                        DeclaredVariablesDictionary.Remove(kvp.Key);
                    }
                    //update locations
                    LocationDictionary.Remove(fileName);
                }
            }
            return(null);
        }
예제 #4
0
        public void Dispose()
        {
            Task childScopeTasks = Task.WhenAll(ChildScopes.Select(s => s.DisposalTask));

            Task.WhenAny(childScopeTasks, Task.Delay(5000))
            .ContinueWith(t =>
            {
                Container.Dispose();
            });
        }
예제 #5
0
        /// <summary>
        /// Returns the innermost scope that surrounds the given source location.
        /// </summary>
        /// <param name="loc">The source location to search for.</param>
        /// <returns>The lowest child of this scope that surrounds the given location, or null if it
        /// cannot be found.</returns>
        public IScope GetScopeForLocation(SourceLocation loc)
        {
            //first search in children
            var foundScope = ChildScopes.Select(c => c.GetScopeForLocation(loc)).FirstOrDefault(r => r != null);

            //if loc not found, check ourselves
            if (foundScope == null && this.IsScopeFor(loc))
            {
                foundScope = this;
            }
            return(foundScope);
        }
        private void InitializeScope(JsonToken token, int scopeIndex, JSchema schema, ConditionalContext context)
        {
            // cache this for performance
            int scopeCurrentIndex = scopeIndex;

            // check to see whether a scope with the same schema exists
            SchemaScope childScope = GetExistingSchemaScope(schema, ref scopeCurrentIndex);

            if (childScope == null)
            {
                childScope = SchemaScope.CreateTokenScope(token, schema, context, null, InitialDepth);
            }

#if DEBUG
            childScope.ConditionalParents.Add(this);
#endif

            ChildScopes.Add(childScope);
        }
예제 #7
0
        /// <summary>
        /// Removes any program elements defined in the given file. If the scope is defined entirely
        /// within the given file, then it removes itself from its parent.
        /// </summary>
        /// <param name="fileName">The file to remove.</param>
        /// <returns>A collection of any unresolved scopes that result from removing the file. The
        /// caller is responsible for re-resolving these as appropriate.</returns>
        public override Collection <IScope> RemoveFile(string fileName)
        {
            Collection <IScope> unresolvedScopes = null;

            if (LocationDictionary.ContainsKey(fileName))
            {
                if (LocationDictionary.Count == 1)
                {
                    //this scope exists solely in the file to be deleted
                    if (ParentScope != null)
                    {
                        ParentScope.RemoveChild(this);
                        ParentScope = null;
                    }
                }
                else
                {
                    //this NamedScope is defined in more than one file, delete only the parts in the given file
                    //Remove the file from the children
                    var unresolvedChildScopes = new List <IScope>();
                    foreach (var child in ChildScopes.ToList())
                    {
                        var result = child.RemoveFile(fileName);
                        if (result != null)
                        {
                            unresolvedChildScopes.AddRange(result);
                        }
                    }
                    //remove method calls
                    var callsInFile = MethodCallCollection.Where(call => call.Location.SourceFileName == fileName).ToList();
                    foreach (var call in callsInFile)
                    {
                        MethodCallCollection.Remove(call);
                    }
                    //remove declared variables
                    var declsInFile = DeclaredVariablesDictionary.Where(kvp => kvp.Value.Location.SourceFileName == fileName).ToList();
                    foreach (var kvp in declsInFile)
                    {
                        DeclaredVariablesDictionary.Remove(kvp.Key);
                    }
                    //remove parent scope candidates
                    var candidatesInFile = ParentScopeCandidates.Where(psc => psc.Location.SourceFileName == fileName).ToList();
                    foreach (var candidate in candidatesInFile)
                    {
                        ParentScopeCandidates.Remove(candidate);
                    }
                    //update locations
                    LocationDictionary.Remove(fileName);

                    if (DefinitionLocations.Any())
                    {
                        //This NamedScope is still defined somewhere, so re-add the unresolved children to it
                        if (unresolvedChildScopes.Count > 0)
                        {
                            foreach (var child in unresolvedChildScopes)
                            {
                                AddChildScope(child);
                            }
                        }
                    }
                    else
                    {
                        //This NamedScope is no longer defined, only referenced
                        //Return any remaining children to be re-resolved by our parent
                        if (MethodCallCollection.Any())
                        {
                            Debug.WriteLine("Found Namespace containing method calls but with only reference locations!");
                            Debug.WriteLine("Namespace locations:");
                            foreach (var loc in LocationDictionary.Values)
                            {
                                Debug.WriteLine(loc);
                            }
                            Debug.WriteLine("Method call locations:");
                            foreach (var mc in MethodCallCollection)
                            {
                                Debug.WriteLine(mc.Location);
                            }
                        }
                        if (DeclaredVariablesDictionary.Any())
                        {
                            Debug.WriteLine("Found Namespace containing declared variables but with only reference locations!");
                            Debug.WriteLine("Namespace locations:");
                            foreach (var loc in LocationDictionary.Values)
                            {
                                Debug.WriteLine(loc);
                            }
                            Debug.WriteLine("Variable locations:");
                            foreach (var dc in DeclaredVariablesDictionary.Values)
                            {
                                Debug.WriteLine(dc.Location);
                            }
                        }

                        if (ParentScope != null)
                        {
                            ParentScope.RemoveChild(this);
                            ParentScope = null;
                        }
                        unresolvedChildScopes.AddRange(ChildScopes);
                        //reset the UnresolvedParentScopeInUse so the children will be re-resolved by our parent
                        foreach (var namedChild in unresolvedChildScopes.OfType <INamedScope>())
                        {
                            namedChild.UnresolvedParentScopeInUse = null;
                        }
                        unresolvedScopes = new Collection <IScope>(unresolvedChildScopes);
                    }
                }
            }
            return(unresolvedScopes);
        }
예제 #8
0
 /// <summary>
 /// Removes any program elements defined in the given file. If the scope is defined entirely
 /// within the given file, then it removes itself from its parent.
 /// </summary>
 /// <param name="fileName">The file to remove.</param>
 /// <returns>A collection of any unresolved scopes that result from removing the file. The
 /// caller is responsible for re-resolving these as appropriate.</returns>
 public override Collection <IScope> RemoveFile(string fileName)
 {
     if (LocationDictionary.ContainsKey(fileName))
     {
         if (LocationDictionary.Count == 1)
         {
             //this scope exists solely in the file to be deleted
             if (ParentScope != null)
             {
                 ParentScope.RemoveChild(this);
                 ParentScope = null;
             }
         }
         else
         {
             //Method is defined in more than one file, delete the stuff defined in the given file
             //Remove the file from the children
             var unresolvedChildScopes = new List <IScope>();
             foreach (var child in ChildScopes.ToList())
             {
                 var result = child.RemoveFile(fileName);
                 if (result != null)
                 {
                     unresolvedChildScopes.AddRange(result);
                 }
             }
             if (unresolvedChildScopes.Count > 0)
             {
                 foreach (var child in unresolvedChildScopes)
                 {
                     AddChildScope(child);
                 }
             }
             //remove method calls
             var callsInFile = MethodCallCollection.Where(call => call.Location.SourceFileName == fileName).ToList();
             foreach (var call in callsInFile)
             {
                 MethodCallCollection.Remove(call);
             }
             //remove declared variables
             var declsInFile = DeclaredVariablesDictionary.Where(kvp => kvp.Value.Location.SourceFileName == fileName).ToList();
             foreach (var kvp in declsInFile)
             {
                 DeclaredVariablesDictionary.Remove(kvp.Key);
             }
             //remove parameter locations
             foreach (var param in Parameters)
             {
                 var locationsInFile = param.Locations.Where(loc => loc.SourceFileName == fileName).ToList();
                 foreach (var loc in locationsInFile)
                 {
                     param.Locations.Remove(loc);
                 }
                 if (param.Locations.Count == 0)
                 {
                     Debug.WriteLine("MethodDefinition.RemoveFile: Found a method parameter with fewer locations than the rest of the method!");
                 }
             }
             //remove parent scope candidates
             var candidatesInFile = ParentScopeCandidates.Where(psc => psc.Location.SourceFileName == fileName).ToList();
             foreach (var candidate in candidatesInFile)
             {
                 ParentScopeCandidates.Remove(candidate);
             }
             //update locations
             LocationDictionary.Remove(fileName);
             //TODO: update access modifiers based on which definitions/declarations we've deleted
         }
     }
     return(null);
 }