Esempio n. 1
0
        private List <MetadataReference> GetApplicationReferences()
        {
            var references = new List <MetadataReference>();

            var export = _libraryExporter.GetAllExports(_environment.ApplicationName);

            foreach (var metadataReference in _libraryManager.GetAllMetadataReferences())
            {
                if (export.MetadataReferences.All(x => x.Name != metadataReference.Name))
                {
                    references.Add(ConvertMetadataReference(metadataReference));
                }
            }

            // Get the MetadataReference for the executing application. If it's a Roslyn reference,
            // we can copy the references created when compiling the application to the Razor page being compiled.
            // This avoids performing expensive calls to MetadataReference.CreateFromImage.
            var libraryExport = _libraryExporter.GetExport(_environment.ApplicationName);

            if (libraryExport?.MetadataReferences != null && libraryExport.MetadataReferences.Count > 0)
            {
                Debug.Assert(libraryExport.MetadataReferences.Count == 1,
                             "Expected 1 MetadataReferences, found " + libraryExport.MetadataReferences.Count);
                var roslynReference      = libraryExport.MetadataReferences[0] as IRoslynMetadataReference;
                var compilationReference = roslynReference?.MetadataReference as CompilationReference;
                if (compilationReference != null)
                {
                    references.AddRange(compilationReference.Compilation.References);
                    references.Add(roslynReference.MetadataReference);
                    return(references);
                }
            }

            if (export != null)
            {
                foreach (var metadataReference in export.MetadataReferences)
                {
                    // Taken from https://github.com/aspnet/KRuntime/blob/757ba9bfdf80bd6277e715d6375969a7f44370ee/src/...
                    // Microsoft.Extensions.Runtime.Roslyn/RoslynCompiler.cs#L164
                    // We don't want to take a dependency on the Roslyn bit directly since it pulls in more dependencies
                    // than the view engine needs (Microsoft.Extensions.Runtime) for example
                    references.Add(ConvertMetadataReference(metadataReference));
                }
            }

            return(references);
        }
Esempio n. 2
0
        private List <MetadataReference> GetApplicationReferences()
        {
            var references = new List <MetadataReference>();

            ILibraryExporter libraryExporter;

            if (_hostingEnvironment.IsDevelopment())
            {
                Project project;
                if (!Project.TryGetProject(_environment.ApplicationBasePath, out project))
                {
                    return(references);
                }

                var engine = new CompilationEngine(
                    new CompilationEngineContext(
                        _environment,
                        _runtimeEnvironment,
                        _loader,
                        new CompilationCache()));
                libraryExporter = engine.CreateProjectExporter(
                    project, _environment.RuntimeFramework, _environment.Configuration);
            }
            else
            {
                libraryExporter = _libraryExporter;
            }

            // Get the MetadataReference for the executing application. If it's a Roslyn reference,
            // we can copy the references created when compiling the application to the Razor page being compiled.
            // This avoids performing expensive calls to MetadataReference.CreateFromImage.
            var libraryExport = libraryExporter.GetExport(_environment.ApplicationName);

            if (libraryExport?.MetadataReferences != null && libraryExport.MetadataReferences.Count > 0)
            {
                Debug.Assert(libraryExport.MetadataReferences.Count == 1,
                             "Expected 1 MetadataReferences, found " + libraryExport.MetadataReferences.Count);
                var roslynReference      = libraryExport.MetadataReferences[0] as IRoslynMetadataReference;
                var compilationReference = roslynReference?.MetadataReference as CompilationReference;
                if (compilationReference != null)
                {
                    references.AddRange(compilationReference.Compilation.References);
                    references.Add(roslynReference.MetadataReference);

                    references.AddRange(_libraryManager
                                        .GetAllMetadataReferences()
                                        .OfType <IRoslynMetadataReference>()
                                        .Select(x => x.MetadataReference));

                    return(references);
                }
            }

            var export = libraryExporter.GetAllExports(_environment.ApplicationName);

            foreach (var metadataReference in export.MetadataReferences)
            {
                // Taken from https://github.com/aspnet/KRuntime/blob/757ba9bfdf80bd6277e715d6375969a7f44370ee/src/...
                // Microsoft.Framework.Runtime.Roslyn/RoslynCompiler.cs#L164
                // We don't want to take a dependency on the Roslyn bit directly since it pulls in more dependencies
                // than the view engine needs (Microsoft.Framework.Runtime) for example
                references.Add(ConvertMetadataReference(metadataReference));
            }

            return(references);
        }