Add an option to enable runtime compilation (#18648)

This commit is contained in:
Pranav K 2020-02-14 08:35:06 -08:00 committed by GitHub
parent d7f98bd562
commit e8d31697ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 144 additions and 20 deletions

View File

@ -12,6 +12,7 @@
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Mvc.Razor.Extensions" />
<Reference Include="Microsoft.AspNetCore.Mvc.RazorPages" />
<Reference Include="Microsoft.AspNetCore.Hosting.Abstractions" />
<Reference Include="Microsoft.AspNetCore.Razor.Runtime" />
<Reference Include="Microsoft.CodeAnalysis.Razor" />
<Reference Include="Microsoft.Extensions.DependencyModel" />

View File

@ -2,6 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Runtime.CompilerServices;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
[assembly: HostingStartup(typeof(RazorRuntimeCompilationHostingStartup))]

View File

@ -0,0 +1,17 @@
// 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.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
{
internal sealed class RazorRuntimeCompilationHostingStartup : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
// Add Razor services
builder.ConfigureServices(services => RazorRuntimeCompilationMvcCoreBuilderExtensions.AddServices(services));
}
}
}

View File

@ -7,16 +7,16 @@
<WebProject_DirectoryAccessLevelKey Condition="'$(OrganizationalAuth)' == 'True' AND '$(OrgReadAccess)' == 'True'">1</WebProject_DirectoryAccessLevelKey>
<NoDefaultLaunchSettingsFile Condition="'$(ExcludeLaunchSettings)' == 'True'">True</NoDefaultLaunchSettingsFile>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.WebApplication1</RootNamespace>
<CopyRefAssembliesToPublishDirectory Condition="'$(RazorRuntimeCompilation)'=='true'">false</CopyRefAssembliesToPublishDirectory>
</PropertyGroup>
<!--#if (IndividualLocalAuth && !UseLocalDB) -->
<!--#endif -->
<ItemGroup Condition=" '$(IndividualLocalAuth)' == 'True' AND '$(UseLocalDB)' != 'True' ">
<ItemGroup>
<None Update="app.db" CopyToOutputDirectory="PreserveNewest" ExcludeFromSingleFile="true" />
</ItemGroup>
<!--#endif -->
<!--#if (IndividualB2CAuth || IndividualLocalAuth || OrganizationalAuth || RazorRuntimeCompilation) -->
<!--#if (IndividualB2CAuth || IndividualLocalAuth || OrganizationalAuth) -->
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="${MicrosoftAspNetCoreAuthenticationAzureADUIPackageVersion}" Condition="'$(OrganizationalAuth)' == 'True'" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureADB2C.UI" Version="${MicrosoftAspNetCoreAuthenticationAzureADB2CUIPackageVersion}" Condition="'$(IndividualB2CAuth)' == 'True'" />
@ -26,6 +26,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="${MicrosoftEntityFrameworkCoreSqlitePackageVersion}" Condition=" '$(IndividualLocalAuth)' == 'True' AND '$(UseLocalDB)' != 'True'" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="${MicrosoftEntityFrameworkCoreSqlServerPackageVersion}" Condition=" '$(IndividualLocalAuth)' == 'True' AND '$(UseLocalDB)' == 'True'" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="${MicrosoftEntityFrameworkCoreToolsPackageVersion}" Condition=" '$(IndividualLocalAuth)' == 'True' " />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="${MicrosoftAspNetCoreMvcRazorRuntimeCompilationPackageVersion}" Condition="'$(RazorRuntimeCompilation)' == 'True'" />
</ItemGroup>
<!--#endif -->

View File

@ -7,16 +7,16 @@
<WebProject_DirectoryAccessLevelKey Condition="'$(OrganizationalAuth)' == 'True' AND '$(OrgReadAccess)' == 'True'">1</WebProject_DirectoryAccessLevelKey>
<NoDefaultLaunchSettingsFile Condition="'$(ExcludeLaunchSettings)' == 'True'">True</NoDefaultLaunchSettingsFile>
<RootNamespace Condition="'$(name)' != '$(name{-VALUE-FORMS-}safe_namespace)'">Company.WebApplication1</RootNamespace>
<CopyRefAssembliesToPublishDirectory Condition="'$(RazorRuntimeCompilation)'=='true'">false</CopyRefAssembliesToPublishDirectory>
</PropertyGroup>
<!--#if (IndividualLocalAuth && !UseLocalDB) -->
<!--#endif -->
<ItemGroup Condition=" '$(IndividualLocalAuth)' == 'True' AND '$(UseLocalDB)' != 'True' ">
<ItemGroup>
<None Update="app.db" CopyToOutputDirectory="PreserveNewest" ExcludeFromSingleFile="true" />
</ItemGroup>
<!--#endif -->
<!--#if (IndividualB2CAuth || IndividualLocalAuth || OrganizationalAuth || RazorRuntimeCompilation) -->
<!--#if (IndividualB2CAuth || IndividualLocalAuth || OrganizationalAuth) -->
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="${MicrosoftAspNetCoreAuthenticationAzureADUIPackageVersion}" Condition="'$(OrganizationalAuth)' == 'True'" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.AzureADB2C.UI" Version="${MicrosoftAspNetCoreAuthenticationAzureADB2CUIPackageVersion}" Condition="'$(IndividualB2CAuth)' == 'True'" />
@ -26,6 +26,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="${MicrosoftEntityFrameworkCoreSqlServerPackageVersion}" Condition=" '$(IndividualLocalAuth)' == 'True' AND '$(UseLocalDB)' == 'True'" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="${MicrosoftEntityFrameworkCoreSqlitePackageVersion}" Condition=" '$(IndividualLocalAuth)' == 'True' AND '$(UseLocalDB)' != 'True'" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="${MicrosoftEntityFrameworkCoreToolsPackageVersion}" Condition=" '$(IndividualLocalAuth)' == 'True' " />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="${MicrosoftAspNetCoreMvcRazorRuntimeCompilationPackageVersion}" Condition="'$(RazorRuntimeCompilation)' == 'True'" />
</ItemGroup>
<!--#endif-->

View File

@ -67,6 +67,10 @@
"NoHttps": {
"longName": "no-https",
"shortName": ""
},
"RazorRuntimeCompilation": {
"longName": "razor-runtime-compilation",
"shortName": "rrc"
}
},
"usageExamples": [

View File

@ -304,6 +304,12 @@
"defaultValue": "false",
"description": "Whether to use LocalDB instead of SQLite. This option only applies if --auth Individual or --auth IndividualB2C is specified."
},
"RazorRuntimeCompilation": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"description": "Determines if the project is configured to use Razor runtime compilation in Debug builds."
},
"Framework": {
"type": "parameter",
"description": "The target framework for the project.",

View File

@ -59,6 +59,15 @@
"useHttps": true
}
],
"symbolInfo": [
{
"id": "RazorRuntimeCompilation",
"name": {
"text": "Enable _Razor runtime compilation"
},
"isVisible": "true"
}
],
"excludeLaunchSettings": false,
"azureReplyUrlPortName": "HttpsPort",
"minFullFrameworkVersion": "4.6.1",

View File

@ -1,11 +1,11 @@
{
"iisSettings": {
//#if (WindowsAuth)
"windowsAuthentication": true,
"anonymousAuthentication": false,
//#else
"windowsAuthentication": false,
"anonymousAuthentication": true,
//#if (WindowsAuth)
"windowsAuthentication": true,
"anonymousAuthentication": false,
//#else
"windowsAuthentication": false,
"anonymousAuthentication": true,
//#endif
"iisExpress": {
"applicationUrl": "http://localhost:8080",
@ -21,7 +21,12 @@
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
//#if(RazorRuntimeCompilation)
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
//#else
"ASPNETCORE_ENVIRONMENT": "Development"
//#endif
}
},
"Company.WebApplication1": {
@ -33,7 +38,12 @@
"applicationUrl": "http://localhost:5000",
//#endif
"environmentVariables": {
//#if(RazorRuntimeCompilation)
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
//#else
"ASPNETCORE_ENVIRONMENT": "Development"
//#endif
}
}
}

View File

@ -67,6 +67,10 @@
"NoHttps": {
"longName": "no-https",
"shortName": ""
},
"RazorRuntimeCompilation": {
"longName": "razor-runtime-compilation",
"shortName": "rrc"
}
},
"usageExamples": [

View File

@ -294,6 +294,12 @@
"defaultValue": "false",
"description": "Whether to use LocalDB instead of SQLite. This option only applies if --auth Individual or --auth IndividualB2C is specified."
},
"RazorRuntimeCompilation": {
"type": "parameter",
"datatype": "bool",
"defaultValue": "false",
"description": "Determines if the project is configured to use Razor runtime compilation in Debug builds."
},
"Framework": {
"type": "parameter",
"description": "The target framework for the project.",

View File

@ -59,6 +59,15 @@
"useHttps": true
}
],
"symbolInfo": [
{
"id": "RazorRuntimeCompilation",
"name": {
"text": "Enable _Razor runtime compilation"
},
"isVisible": "true"
}
],
"excludeLaunchSettings": false,
"azureReplyUrlPortName": "HttpsPort",
"minFullFrameworkVersion": "4.6.1",

View File

@ -1,11 +1,11 @@
{
"iisSettings": {
//#if (WindowsAuth)
"windowsAuthentication": true,
"anonymousAuthentication": false,
//#else
"windowsAuthentication": false,
"anonymousAuthentication": true,
//#if (WindowsAuth)
"windowsAuthentication": true,
"anonymousAuthentication": false,
//#else
"windowsAuthentication": false,
"anonymousAuthentication": true,
//#endif
"iisExpress": {
"applicationUrl": "http://localhost:8080",
@ -21,7 +21,12 @@
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
//#if(RazorRuntimeCompilation)
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
//#else
"ASPNETCORE_ENVIRONMENT": "Development"
//#endif
}
},
"Company.WebApplication1": {
@ -33,7 +38,12 @@
"applicationUrl": "http://localhost:5000",
//#endif
"environmentVariables": {
//#if(RazorRuntimeCompilation)
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
//#else
"ASPNETCORE_ENVIRONMENT": "Development"
//#endif
}
}
}

View File

@ -212,5 +212,24 @@ namespace Templates.Test
await aspNetProcess.AssertPagesOk(pages);
}
}
[Fact]
public async Task MvcTemplate_RazorRuntimeCompilation_BuildsAndPublishes()
{
Project = await ProjectFactory.GetOrCreateProject("mvc_rc", Output);
var createResult = await Project.RunDotNetNewAsync("mvc", args: new[] { "--razor-runtime-compilation" });
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", Project, createResult));
// Verify building in debug works
var buildResult = await Project.RunDotNetBuildAsync();
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", Project, buildResult));
// Publish builds in "release" configuration. Running publish should ensure we can compile in release and that we can publish without issues.
buildResult = await Project.RunDotNetPublishAsync();
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", Project, buildResult));
Assert.False(Directory.Exists(Path.Combine(Project.TemplatePublishDir, "refs")), "The refs directory should not be published.");
}
}
}

View File

@ -209,6 +209,29 @@ namespace Templates.Test
}
}
[Fact]
public async Task RazorPagesTemplate_RazorRuntimeCompilation_BuildsAndPublishes()
{
Project = await ProjectFactory.GetOrCreateProject("razorpages_rc", Output);
var createResult = await Project.RunDotNetNewAsync("razor", args: new[] { "--razor-runtime-compilation" });
Assert.True(0 == createResult.ExitCode, ErrorMessages.GetFailedProcessMessage("create/restore", Project, createResult));
// Verify building in debug works
var buildResult = await Project.RunDotNetBuildAsync();
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("build", Project, buildResult));
// Publish builds in "release" configuration. Running publish should ensure we can compile in release and that we can publish without issues.
buildResult = await Project.RunDotNetPublishAsync();
Assert.True(0 == buildResult.ExitCode, ErrorMessages.GetFailedProcessMessage("publish", Project, buildResult));
Assert.False(Directory.Exists(Path.Combine(Project.TemplatePublishDir, "refs")), "The refs directory should not be published.");
// Verify ref assemblies isn't published
var refsDirectory = Path.Combine(Project.TemplatePublishDir, "refs");
Assert.False(Directory.Exists(refsDirectory), $"{refsDirectory} should not be in the publish output.");
}
private string ReadFile(string basePath, string path)
{