示例#1
0
        public void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            Debug.Assert(context.Symbol.Kind == SymbolKind.NamedType);
            Debug.Assert(StartupFacts.IsStartupClass(_context.StartupSymbols, (INamedTypeSymbol)context.Symbol));

            var type = (INamedTypeSymbol)context.Symbol;

            var optionsAnalysis = _context.GetRelatedSingletonAnalysis <OptionsAnalysis>(type);

            // Find the middleware analysis foreach of the Configure methods defined by this class and validate.
            //
            // Note that this doesn't attempt to handle inheritance scenarios.
            foreach (var middlewareAnalysis in _context.GetRelatedAnalyses <MiddlewareAnalysis>(type))
            {
                foreach (var middlewareItem in middlewareAnalysis.Middleware)
                {
                    if (middlewareItem.UseMethod.Name == "UseMvc" || middlewareItem.UseMethod.Name == "UseMvcWithDefaultRoute")
                    {
                        // Report a diagnostic if it's unclear that the user turned off Endpoint Routing.
                        if (!OptionsFacts.IsEndpointRoutingExplicitlyDisabled(optionsAnalysis))
                        {
                            context.ReportDiagnostic(Diagnostic.Create(
                                                         StartupAnalzyer.Diagnostics.UnsupportedUseMvcWithEndpointRouting,
                                                         middlewareItem.Operation.Syntax.GetLocation(),
                                                         middlewareItem.UseMethod.Name,
                                                         optionsAnalysis.ConfigureServicesMethod.Name));
                        }
                    }
                }
            }
        }
        public void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            Debug.Assert(context.Symbol.Kind == SymbolKind.NamedType);
            Debug.Assert(StartupFacts.IsStartupClass(_context.StartupSymbols, (INamedTypeSymbol)context.Symbol));

            var type = (INamedTypeSymbol)context.Symbol;

            var middlwareAnalyses = _context.GetRelatedAnalyses <MiddlewareAnalysis>(type);

            foreach (var middlewareAnalsysis in middlwareAnalyses)
            {
                for (var i = 0; i < middlewareAnalsysis.Middleware.Length; i++)
                {
                    var middlewareItem = middlewareAnalsysis.Middleware[i];
                    if (MiddlewareHappensAfterMap.TryGetValue(middlewareItem.UseMethod.Name, out var cannotComeAfter))
                    {
                        for (var j = i; j < middlewareAnalsysis.Middleware.Length; j++)
                        {
                            var candidate = middlewareAnalsysis.Middleware[j];
                            if (string.Equals(cannotComeAfter, candidate.UseMethod.Name, StringComparison.Ordinal))
                            {
                                // Found the other middleware after current one. This is an error.
                                context.ReportDiagnostic(Diagnostic.Create(
                                                             StartupAnalzyer.Diagnostics.MiddlewareInvalidOrder,
                                                             candidate.Operation.Syntax.GetLocation(),
                                                             middlewareItem.UseMethod.Name,
                                                             candidate.UseMethod.Name));
                            }
                        }
                    }
                }
            }
        }
        public void AnalyzeSymbol(SymbolAnalysisContext context)
        {
            Debug.Assert(context.Symbol.Kind == SymbolKind.NamedType);
            Debug.Assert(StartupFacts.IsStartupClass(_context.StartupSymbols, (INamedTypeSymbol)context.Symbol));

            var type = (INamedTypeSymbol)context.Symbol;

            // Find the services analysis for each of the ConfigureServices methods defined by this class.
            //
            // There should just be one.
            var servicesAnalysis = _context.GetRelatedSingletonAnalysis <ServicesAnalysis>(type);

            var occluded = new HashSet <string>();

            foreach (var entry in servicesAnalysis.Services)
            {
                occluded.Add(entry.UseMethod.Name);

                if (ServicesMap.TryGetValue(entry.UseMethod.Name, out var additional))
                {
                    foreach (var item in additional)
                    {
                        occluded.Add(item);
                    }
                }
            }

            // Find the middleware analysis for each of the Configure methods defined by this class and validate.
            //
            // Note that this doesn't attempt to handle inheritance scenarios.
            foreach (var middlewareAnalsysis in _context.GetRelatedAnalyses <MiddlewareAnalysis>(type))
            {
                foreach (var middlewareItem in middlewareAnalsysis.Middleware)
                {
                    if (MiddlewareMap.TryGetValue(middlewareItem.UseMethod.Name, out var requiredServices))
                    {
                        foreach (var requiredService in requiredServices)
                        {
                            if (!occluded.Contains(requiredService))
                            {
                                context.ReportDiagnostic(Diagnostic.Create(
                                                             StartupAnalzyer.Diagnostics.MiddlewareMissingRequiredServices,
                                                             middlewareItem.Operation.Syntax.GetLocation(),
                                                             middlewareItem.UseMethod.Name,
                                                             requiredService,
                                                             servicesAnalysis.ConfigureServicesMethod.Name));
                            }
                        }
                    }
                }
            }
        }