Beispiel #1
0
        public IActionResult Index()
        {
            var viewModel = new FeaturesViewModel();

            var controllerFeature = new ControllerFeature();

            _partManager.PopulateFeature(controllerFeature);
            viewModel.Controllers = controllerFeature.Controllers.ToList();

            var metaDataReferenceFeature = new MetadataReferenceFeature();

            _partManager.PopulateFeature(metaDataReferenceFeature);
            viewModel.MetadataReferences = metaDataReferenceFeature.MetadataReferences
                                           .ToList();

            var tagHelperFeature = new TagHelperFeature();

            _partManager.PopulateFeature(tagHelperFeature);
            viewModel.TagHelpers = tagHelperFeature.TagHelpers.ToList();

            var viewComponentFeature = new ViewComponentFeature();

            _partManager.PopulateFeature(viewComponentFeature);
            viewModel.ViewComponents = viewComponentFeature.ViewComponents.ToList();

            return(View(viewModel));
        }
        public void GetCompilationReferences_CombinesApplicationPartAndOptionMetadataReferences()
        {
            // Arrange
            var options = new RazorViewEngineOptions();
            var objectAssemblyLocation          = typeof(object).GetTypeInfo().Assembly.Location;
            var objectAssemblyMetadataReference = MetadataReference.CreateFromFile(objectAssemblyLocation);

            options.AdditionalCompilationReferences.Add(objectAssemblyMetadataReference);

            var applicationPartManager = GetApplicationPartManager();
            var feature = new MetadataReferenceFeature();

            applicationPartManager.PopulateFeature(feature);
            var partReferences            = feature.MetadataReferences;
            var expectedReferenceDisplays = partReferences
                                            .Concat(new[] { objectAssemblyMetadataReference })
                                            .Select(r => r.Display);
            var referenceManager = new DefaultRazorReferenceManager(
                applicationPartManager,
                Options.Create(options));

            // Act
            var references        = referenceManager.CompilationReferences;
            var referenceDisplays = references.Select(reference => reference.Display);

            // Assert
            Assert.Equal(expectedReferenceDisplays, referenceDisplays);
        }
Beispiel #3
0
        public void GetCompilationReferences_CombinesApplicationPartAndOptionMetadataReferences()
        {
            // Arrange
            var options = new RazorViewEngineOptions();
            var objectAssemblyLocation          = typeof(object).GetTypeInfo().Assembly.Location;
            var objectAssemblyMetadataReference = MetadataReference.CreateFromFile(objectAssemblyLocation);

            options.AdditionalCompilationReferences.Add(objectAssemblyMetadataReference);
            var applicationPartManager = GetApplicationPartManager();
            var compilationService     = new TestRoslynCompilationService(applicationPartManager, options);
            var feature = new MetadataReferenceFeature();

            applicationPartManager.PopulateFeature(feature);
            var partReferences     = feature.MetadataReferences;
            var expectedReferences = new List <MetadataReference>();

            expectedReferences.AddRange(partReferences);
            expectedReferences.Add(objectAssemblyMetadataReference);
            var expectedReferenceDisplays = expectedReferences.Select(reference => reference.Display);

            // Act
            var references        = compilationService.GetCompilationReferencesPublic();
            var referenceDisplays = references.Select(reference => reference.Display);

            // Assert
            Assert.NotNull(references);
            Assert.NotEmpty(references);
            Assert.Equal(expectedReferenceDisplays, referenceDisplays);
        }
        private IReadOnlyList <MetadataReference> GetCompilationReferences()
        {
#pragma warning disable CS0618 // Type or member is obsolete
            var feature = new MetadataReferenceFeature();
#pragma warning restore CS0618 // Type or member is obsolete
            _partManager.PopulateFeature(feature);
            var applicationReferences = feature.MetadataReferences;

            if (_additionalMetadataReferences.Count == 0)
            {
                return(applicationReferences.ToArray());
            }

            var compilationReferences = new List <MetadataReference>(applicationReferences.Count + _additionalMetadataReferences.Count);
            compilationReferences.AddRange(applicationReferences);
            compilationReferences.AddRange(_additionalMetadataReferences);

            return(compilationReferences);
        }
        private IList <MetadataReference> GetCompilationReferences()
        {
            var feature = new MetadataReferenceFeature();

            _partManager.PopulateFeature(feature);
            var applicationReferences = feature.MetadataReferences;

            if (_additionalMetadataReferences.Count == 0)
            {
                return(applicationReferences);
            }

            var compilationReferences = new List <MetadataReference>(applicationReferences.Count + _additionalMetadataReferences.Count);

            compilationReferences.AddRange(applicationReferences);
            compilationReferences.AddRange(_additionalMetadataReferences);

            return(compilationReferences);
        }
        /// <summary>
        /// Creates the service assembly for a service
        /// </summary>
        /// <param name="org">The Organization code for the service owner</param>
        /// <param name="service">The service code for the current service</param>
        /// <param name="edition">The edition code for the current service</param>
        /// <param name="outputLocation">The directory where the resulting assembly should be saved</param>
        /// <param name="loadAssemblyContext">Defines if assembly should be loaded in context</param>
        /// <returns>The assembly name</returns>
        public CodeCompilationResult CreateServiceAssembly(string org, string service, string edition, string outputLocation = null, bool loadAssemblyContext = true)
        {
            CodeCompilationResult compilationResult = new CodeCompilationResult()
            {
                CompileStarted = DateTime.Now
            };
            string assemblykey = org + "_" + service + "_" + edition;

            List <AltinnCoreFile> implementationFiles = _repository.GetImplementationFiles(org, service, edition);

            DateTime lastChanged = new DateTime(2000, 01, 01);

            foreach (AltinnCoreFile file in implementationFiles)
            {
                if (file.LastChanged > lastChanged)
                {
                    lastChanged = file.LastChanged;
                }
            }

            if (_assemblyNames.ContainsKey(assemblykey) && _assemblyNames[assemblykey].CompileStarted > lastChanged && string.IsNullOrWhiteSpace(outputLocation))
            {
                compilationResult = _assemblyNames[assemblykey];
                return(compilationResult);
            }

            SyntaxTree[]             syntaxTrees = GetSyntaxTrees(org, service, edition);
            List <MetadataReference> references  = new List <MetadataReference>();
            Assembly root = Assembly.GetEntryAssembly();

            string assemblyName = Path.GetRandomFileName();

            MetadataReferenceFeature referenceFeature = new MetadataReferenceFeature();

            _partManager.PopulateFeature(referenceFeature);

            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                syntaxTrees: syntaxTrees,
                references: referenceFeature.MetadataReferences,
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var pdbMs = new MemoryStream())
                using (var ms = new MemoryStream())
                {
                    EmitResult result;
                    Stopwatch  stopWatch = new Stopwatch();
                    stopWatch.Start();
                    if (!string.IsNullOrEmpty(outputLocation))
                    {
                        result = compilation.Emit(outputLocation + assemblyName + ".dll", outputLocation + assemblyName + ".pdb");
                    }
                    else
                    {
                        result = compilation.Emit(ms, pdbMs);
                    }

                    stopWatch.Stop();
                    compilationResult.TimeUsed = stopWatch.Elapsed;

                    compilationResult.CompilationInfo = new List <CompilationInfo>();
                    foreach (Diagnostic diag in result.Diagnostics)
                    {
                        // TODO: Decide how to handle this special CS1701 we get
                        if (!diag.Id.Equals("CS1701"))
                        {
                            var compInfo = new CompilationInfo
                            {
                                Info         = diag.GetMessage(),
                                FilePath     = diag.Location.SourceTree.FilePath,
                                FileName     = System.IO.Path.GetFileName(diag.Location.SourceTree.FilePath),
                                Severity     = diag.Severity.ToString(),
                                Code         = diag.Id,
                                WarningLevel = diag.WarningLevel,
                                LineNumber   = diag.Location.GetLineSpan().StartLinePosition.Line + 1
                            };

                            if (diag.Severity.Equals(DiagnosticSeverity.Warning))
                            {
                                compilationResult.Warnings++;
                            }
                            else if (diag.Severity.Equals(DiagnosticSeverity.Error))
                            {
                                compilationResult.Errors++;
                            }

                            compilationResult.CompilationInfo.Add(compInfo);
                        }
                    }

                    if (!result.Success)
                    {
                        LogEmitResult(result);
                    }
                    else
                    {
                        compilationResult.AssemblyName = compilation.AssemblyName;
                        compilationResult.Succeeded    = true;

                        if (string.IsNullOrEmpty(outputLocation) && loadAssemblyContext)
                        {
                            ms.Seek(0, SeekOrigin.Begin);
                            pdbMs.Seek(0, SeekOrigin.Begin);
                            AssemblyLoadContext.Default.LoadFromStream(ms, pdbMs);
                            ms.Seek(0, SeekOrigin.Begin);
                            MetadataReference newReference = MetadataReference.CreateFromStream(ms);
                            if (_roslynCompilation.ServiceReferences.ContainsKey(assemblykey))
                            {
                                _roslynCompilation.ServiceReferences.Remove(assemblykey);
                            }

                            _roslynCompilation.ServiceReferences.Add(assemblykey, newReference);

                            if (_assemblyNames.ContainsKey(assemblykey))
                            {
                                _assemblyNames.Remove(assemblykey);
                            }

                            _assemblyNames.Add(assemblykey, compilationResult);
                        }

                        return(compilationResult);
                    }
                }

            return(compilationResult);
        }