Add Microsoft.AspNetCore.Mvc.Dnx to allow using Mvc with DNX

This commit is contained in:
Pranav K 2016-02-26 12:01:40 -08:00
parent 00bab92257
commit 385c21fbe2
12 changed files with 249 additions and 12 deletions

View File

@ -1,4 +1,3 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
@ -102,6 +101,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ActionConstraintSample.Web"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MvcSubAreaSample.Web", "samples\MvcSubAreaSample.Web\MvcSubAreaSample.Web.xproj", "{45F6B3B6-D114-4D77-84D6-561B3957F341}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Mvc.Dnx", "src\Microsoft.AspNetCore.Mvc.Dnx\Microsoft.AspNetCore.Mvc.Dnx.xproj", "{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -606,6 +607,18 @@ Global
{45F6B3B6-D114-4D77-84D6-561B3957F341}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{45F6B3B6-D114-4D77-84D6-561B3957F341}.Release|x86.ActiveCfg = Release|Any CPU
{45F6B3B6-D114-4D77-84D6-561B3957F341}.Release|x86.Build.0 = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|x86.ActiveCfg = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|x86.Build.0 = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|Any CPU.Build.0 = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|x86.ActiveCfg = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -654,5 +667,6 @@ Global
{FCFE6024-2720-49B4-8257-9DBC6114F0F1} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
{EE0BD773-4D47-4AA8-8472-5A938A3953BA} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
{45F6B3B6-D114-4D77-84D6-561B3957F341} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
EndGlobalSection
EndGlobal

16
Mvc.sln
View File

@ -1,4 +1,3 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
@ -144,6 +143,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "InlineConstraintSample.Web"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MvcSubAreaSample.Web", "samples\MvcSubAreaSample.Web\MvcSubAreaSample.Web.xproj", "{45F6B3B6-D114-4D77-84D6-561B3957F341}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNetCore.Mvc.Dnx", "src\Microsoft.AspNetCore.Mvc.Dnx\Microsoft.AspNetCore.Mvc.Dnx.xproj", "{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -867,6 +868,18 @@ Global
{45F6B3B6-D114-4D77-84D6-561B3957F341}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{45F6B3B6-D114-4D77-84D6-561B3957F341}.Release|x86.ActiveCfg = Release|Any CPU
{45F6B3B6-D114-4D77-84D6-561B3957F341}.Release|x86.Build.0 = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|x86.ActiveCfg = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Debug|x86.Build.0 = Debug|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|Any CPU.Build.0 = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|x86.ActiveCfg = Release|Any CPU
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -937,5 +950,6 @@ Global
{396B40D7-AC70-49A7-B33C-ED42129FEBE3} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{EA34877F-1AC1-42B7-B4E6-15A093F40CAE} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
{45F6B3B6-D114-4D77-84D6-561B3957F341} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
{8FB691C2-DFD8-4FEE-9628-2BB8466A691C} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
EndGlobalSection
EndGlobal

View File

@ -10,6 +10,7 @@
"Microsoft.AspNetCore.Mvc.Core": { },
"Microsoft.AspNetCore.Mvc.Cors": { },
"Microsoft.AspNetCore.Mvc.DataAnnotations": { },
"Microsoft.AspNetCore.Mvc.Dnx": { },
"Microsoft.AspNetCore.Mvc.Formatters.Json": { },
"Microsoft.AspNetCore.Mvc.Formatters.Xml": { },
"Microsoft.AspNetCore.Mvc.Localization": { },

View File

@ -14,6 +14,9 @@ namespace MvcSandbox
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
#if DNX451
services.AddMvcDnx();
#endif
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -21,7 +21,12 @@
"dotnet-razor-tooling": "1.0.0-*"
},
"frameworks": {
"net451": {},
"dnx451": {
"dependencies": {
"Microsoft.AspNetCore.Mvc.Dnx": "1.0.0-*"
}
},
"net451": { },
"dnxcore50": {
"imports": "portable-net451+win8",
"dependencies": {

View File

@ -0,0 +1,88 @@
// 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.Diagnostics;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.AspNetCore.Mvc.Infrastructure
{
public class DnxAssemblyProvider : IAssemblyProvider
{
private readonly ILibraryManager _libraryManager;
public DnxAssemblyProvider(ILibraryManager libraryManager)
{
_libraryManager = libraryManager;
}
/// <summary>
/// Gets the set of assembly names that are used as root for discovery of
/// MVC controllers, view components and views.
/// </summary>
// DefaultControllerTypeProvider uses CandidateAssemblies to determine if the base type of a POCO controller
// lives in an assembly that references MVC. CandidateAssemblies excludes all assemblies from the
// ReferenceAssemblies set. Consequently adding WebApiCompatShim to this set would cause the ApiController to
// fail this test.
protected virtual HashSet<string> ReferenceAssemblies { get; } = new HashSet<string>(StringComparer.Ordinal)
{
"Microsoft.AspNetCore.Mvc",
"Microsoft.AspNetCore.Mvc.Abstractions",
"Microsoft.AspNetCore.Mvc.ApiExplorer",
"Microsoft.AspNetCore.Mvc.Core",
"Microsoft.AspNetCore.Mvc.Cors",
"Microsoft.AspNetCore.Mvc.DataAnnotations",
"Microsoft.AspNetCore.Mvc.Formatters.Json",
"Microsoft.AspNetCore.Mvc.Formatters.Xml",
"Microsoft.AspNetCore.Mvc.Localization",
"Microsoft.AspNetCore.Mvc.Razor",
"Microsoft.AspNetCore.Mvc.Razor.Host",
"Microsoft.AspNetCore.Mvc.TagHelpers",
"Microsoft.AspNetCore.Mvc.ViewFeatures"
};
/// <inheritdoc />
public IEnumerable<Assembly> CandidateAssemblies
{
get
{
return GetCandidateLibraries().SelectMany(l => l.Assemblies)
.Select(Load);
}
}
/// <summary>
/// Returns a list of libraries that references the assemblies in <see cref="ReferenceAssemblies"/>.
/// By default it returns all assemblies that reference any of the primary MVC assemblies
/// while ignoring MVC assemblies.
/// </summary>
/// <returns>A set of <see cref="Library"/>.</returns>
protected virtual IEnumerable<Library> GetCandidateLibraries()
{
if (ReferenceAssemblies == null)
{
return Enumerable.Empty<Library>();
}
// GetReferencingLibraries returns the transitive closure of referencing assemblies
// for a given assembly.
return ReferenceAssemblies.SelectMany(_libraryManager.GetReferencingLibraries)
.Distinct()
.Where(IsCandidateLibrary);
}
private static Assembly Load(AssemblyName assemblyName)
{
return Assembly.Load(assemblyName);
}
private bool IsCandidateLibrary(Library library)
{
Debug.Assert(ReferenceAssemblies != null);
return !ReferenceAssemblies.Contains(library.Name);
}
}
}

View File

@ -1,7 +1,6 @@
// 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.
#if DNX451
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@ -10,11 +9,13 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.PortableExecutable;
#if DOTNET5_6
using System.Runtime.Loader;
#endif
using System.Text;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Dnx.Compilation.CSharp;
using Microsoft.Extensions.CompilationAbstractions;
@ -35,19 +36,20 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
private readonly ILibraryExporter _libraryExporter;
private readonly RazorViewEngineOptions _options;
private readonly Lazy<List<MetadataReference>> _applicationReferences;
#if DOTNET5_6
private readonly RazorLoadContext _razorLoadContext;
#endif
/// <summary>
/// Initalizes a new instance of the <see cref="DnxRoslynCompilationService"/> class.
/// </summary>
/// <param name="environment">The environment for the executing application.</param>
/// <param name="libraryExporter">The library manager that provides export and reference information.</param>
/// <param name="host">The <see cref="IMvcRazorHost"/> that was used to generate the code.</param>
/// <param name="optionsAccessor">Accessor to <see cref="RazorViewEngineOptions"/>.</param>
/// <param name="fileProviderAccessor">The <see cref="IRazorViewEngineFileProviderAccessor"/>.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public DnxRoslynCompilationService(IApplicationEnvironment environment,
public DnxRoslynCompilationService(
IApplicationEnvironment environment,
ILibraryExporter libraryExporter,
IMvcRazorHost host,
IOptions<RazorViewEngineOptions> optionsAccessor,
IRazorViewEngineFileProviderAccessor fileProviderAccessor)
{
@ -55,6 +57,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
_libraryExporter = libraryExporter;
_options = optionsAccessor.Value;
_applicationReferences = new Lazy<List<MetadataReference>>(GetApplicationReferences);
#if DOTNET5_6
_razorLoadContext = new RazorLoadContext();
#endif
}
public CompilationResult Compile(RelativeFileInfo fileInfo, string compilationContent)
@ -143,7 +149,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
private Assembly LoadStream(MemoryStream ms, MemoryStream assemblySymbols)
{
#if DOTNET5_6
return _razorLoadContext.Load(ms, assemblySymbols);
#else
return Assembly.Load(ms.ToArray(), assemblySymbols?.ToArray());
#endif
}
private List<MetadataReference> GetApplicationReferences()
@ -234,6 +244,19 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
return metadata.GetReference(filePath: path);
}
#if DOTNET5_6
private class RazorLoadContext : AssemblyLoadContext
{
protected override Assembly Load(AssemblyName assemblyName)
{
return Default.LoadFromAssemblyName(assemblyName);
}
public Assembly Load(Stream assembly, Stream assemblySymbols)
{
return LoadFromStream(assembly, assemblySymbols);
}
}
#endif
}
}
#endif

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0.24720" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.24720</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>8fb691c2-dfd8-4fee-9628-2bb8466a691c</ProjectGuid>
<RootNamespace>Microsoft.AspNetCore.Mvc.Dnx</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -0,0 +1,35 @@
// 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 Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.Extensions.CompilationAbstractions;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.Extensions.DependencyInjection
{
public static class MvcDnxServiceCollectionExtensions
{
/// <summary>
/// Adds services required for Mvc applications to work with DNX to the specified <paramref name="services"/>.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddMvcDnx(this IServiceCollection services)
{
if (DnxPlatformServices.Default.LibraryManager != null)
{
// Add IAssemblyProvider services
services.AddSingleton(DnxPlatformServices.Default.LibraryManager);
services.AddTransient<IAssemblyProvider, DnxAssemblyProvider>();
// Add compilation services
services.AddSingleton(CompilationServices.Default.LibraryExporter);
services.AddSingleton<ICompilationService, DnxRoslynCompilationService>();
}
return services;
}
}
}

View File

@ -0,0 +1,9 @@
// 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.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
[assembly: AssemblyMetadata("Serviceable", "True")]
[assembly: NeutralResourcesLanguage("en-us")]

View File

@ -0,0 +1,27 @@
{
"description": "Bridge for keeping compat with DNX.",
"version": "1.0.0-*",
"repository": {
"type": "git",
"url": "git://github.com/aspnet/mvc"
},
"compilationOptions": {
"warningsAsErrors": true,
"keyFile": "../../tools/Key.snk",
"nowarn": [ "CS1591" ],
"xmlDoc": true
},
"dependencies": {
"Microsoft.AspNetCore.Mvc.Razor": "1.0.0-*",
"Microsoft.Dnx.Compilation.CSharp.Common": "1.0.0-*",
"Microsoft.Dnx.Compilation.CSharp.Abstractions": "1.0.0-*",
"Microsoft.Extensions.DependencyInjection": "1.0.0-*",
"Microsoft.Extensions.PlatformAbstractions.Dnx": "1.0.0-*"
},
"frameworks": {
"dnx451": {},
"dotnet5.6": {
"imports": "portable-net451+win8"
}
}
}

View File

@ -67,8 +67,7 @@
},
"dnx451": {
"dependencies": {
"Microsoft.Dnx.Compilation.CSharp.Common": "1.0.0-*",
"Microsoft.Dnx.Compilation.CSharp.Abstractions": "1.0.0-*",
"Microsoft.AspNetCore.Mvc.Dnx": "1.0.0-*",
"xunit.runner.aspnet": "2.0.0-aspnet-*"
}
},