// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{
///
/// A type that uses Roslyn to compile C# content and to locate references.
///
public class DependencyContextCompilationService : RoslynCompilationService
{
private readonly DependencyContext _dependencyContext;
///
/// Initalizes a new instance of the class.
///
/// The environment for the executing application.
/// The that was used to generate the code.
/// Accessor to .
/// The .
/// The .
public DependencyContextCompilationService(
IApplicationEnvironment environment,
IMvcRazorHost host,
IOptions optionsAccessor,
IRazorViewEngineFileProviderAccessor fileProviderAccessor,
ILoggerFactory loggerFactory)
: base(environment, host, optionsAccessor, fileProviderAccessor, loggerFactory)
{
var applicationAssembly = Assembly.Load(new AssemblyName(environment.ApplicationName));
_dependencyContext = DependencyContext.Load(applicationAssembly);
}
protected override List GetApplicationReferences()
{
return _dependencyContext.CompileLibraries
.SelectMany(library => library.ResolveReferencePaths())
.Select(CreateMetadataFileReference)
.ToList();
}
}
}