Skip to content

Commit 1e8ff02

Browse files
committed
NET5 support: self contained build fix
1 parent 07b1c0f commit 1e8ff02

File tree

9 files changed

+132
-28
lines changed

9 files changed

+132
-28
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\RazorEngineCore\RazorEngineCore.csproj" />
10+
</ItemGroup>
11+
12+
</Project>

ExampleAppNET5/Program.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using System.Runtime.InteropServices;
5+
using RazorEngineCore;
6+
7+
namespace ExampleAppNET5
8+
{
9+
10+
class Program
11+
{
12+
static string Content = @"
13+
Hello @Model.Name
14+
15+
@foreach(var item in @Model.Items)
16+
{
17+
<div>- @item</div>
18+
}
19+
20+
<div data-name=""@Model.Name""></div>
21+
22+
<area>
23+
@{ RecursionTest(3); }
24+
</area>
25+
26+
@{
27+
void RecursionTest(int level){
28+
if (level <= 0)
29+
{
30+
return;
31+
}
32+
33+
<div>LEVEL: @level</div>
34+
@{ RecursionTest(level - 1); }
35+
}
36+
}";
37+
38+
static void Main(string[] args)
39+
{
40+
IRazorEngine razorEngine = new RazorEngine();
41+
IRazorEngineCompiledTemplate template = razorEngine.Compile(Content);
42+
43+
string result = template.Run(new
44+
{
45+
Name = "Alexander",
46+
Items = new List<string>()
47+
{
48+
"item 1",
49+
"item 2"
50+
}
51+
});
52+
53+
Console.WriteLine(result);
54+
Console.ReadKey();
55+
}
56+
}
57+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
is_global = true
2+
build_property.TargetFramework =
3+
build_property.TargetPlatformMinVersion =
4+
build_property.UsingMicrosoftNETSdkWeb =
5+
build_property.ProjectTypeGuids =
6+
build_property.PublishSingleFile =
7+
build_property.IncludeAllContentForSelfExtract =
8+
build_property._SupportedPlatformList =

ExampleAppNet472/ExampleAppNET472.csproj

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,21 @@
3636
<WarningLevel>4</WarningLevel>
3737
</PropertyGroup>
3838
<ItemGroup>
39-
<Reference Include="Microsoft.AspNetCore.Razor.Language, Version=3.1.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
40-
<HintPath>..\packages\Microsoft.AspNetCore.Razor.Language.3.1.8\lib\netstandard2.0\Microsoft.AspNetCore.Razor.Language.dll</HintPath>
39+
<Reference Include="Microsoft.AspNetCore.Razor.Language, Version=5.0.3.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
40+
<HintPath>..\packages\Microsoft.AspNetCore.Razor.Language.5.0.3\lib\netstandard2.0\Microsoft.AspNetCore.Razor.Language.dll</HintPath>
4141
</Reference>
42-
<Reference Include="Microsoft.CodeAnalysis, Version=3.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
43-
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.3.7.0\lib\netstandard2.0\Microsoft.CodeAnalysis.dll</HintPath>
42+
<Reference Include="Microsoft.CodeAnalysis, Version=3.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
43+
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.3.8.0\lib\netstandard2.0\Microsoft.CodeAnalysis.dll</HintPath>
4444
</Reference>
45-
<Reference Include="Microsoft.CodeAnalysis.CSharp, Version=3.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
46-
<HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.3.7.0\lib\netstandard2.0\Microsoft.CodeAnalysis.CSharp.dll</HintPath>
45+
<Reference Include="Microsoft.CodeAnalysis.CSharp, Version=3.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
46+
<HintPath>..\packages\Microsoft.CodeAnalysis.CSharp.3.8.0\lib\netstandard2.0\Microsoft.CodeAnalysis.CSharp.dll</HintPath>
4747
</Reference>
4848
<Reference Include="System" />
4949
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
5050
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
5151
</Reference>
52-
<Reference Include="System.Collections.Immutable, Version=1.2.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
53-
<HintPath>..\packages\System.Collections.Immutable.1.7.1\lib\net461\System.Collections.Immutable.dll</HintPath>
52+
<Reference Include="System.Collections.Immutable, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
53+
<HintPath>..\packages\System.Collections.Immutable.5.0.0\lib\net461\System.Collections.Immutable.dll</HintPath>
5454
</Reference>
5555
<Reference Include="System.Core" />
5656
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
@@ -60,8 +60,8 @@
6060
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
6161
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
6262
</Reference>
63-
<Reference Include="System.Reflection.Metadata, Version=1.4.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
64-
<HintPath>..\packages\System.Reflection.Metadata.1.8.1\lib\net461\System.Reflection.Metadata.dll</HintPath>
63+
<Reference Include="System.Reflection.Metadata, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
64+
<HintPath>..\packages\System.Reflection.Metadata.5.0.0\lib\net461\System.Reflection.Metadata.dll</HintPath>
6565
</Reference>
6666
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
6767
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.7.1\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
@@ -87,16 +87,16 @@
8787
<None Include="App.config" />
8888
<None Include="packages.config" />
8989
</ItemGroup>
90+
<ItemGroup>
91+
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.3.3.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" />
92+
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.3.3.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" />
93+
</ItemGroup>
9094
<ItemGroup>
9195
<ProjectReference Include="..\RazorEngineCore\RazorEngineCore.csproj">
9296
<Project>{1e21186e-49a5-433a-abad-18ced1aa2494}</Project>
9397
<Name>RazorEngineCore</Name>
9498
</ProjectReference>
9599
</ItemGroup>
96-
<ItemGroup>
97-
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.3.3.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" />
98-
<Analyzer Include="..\packages\Microsoft.CodeAnalysis.Analyzers.3.3.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" />
99-
</ItemGroup>
100100
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
101101
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
102102
<PropertyGroup>

ExampleAppNet472/packages.config

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Microsoft.AspNetCore.Razor.Language" version="3.1.8" targetFramework="net472" />
3+
<package id="Microsoft.AspNetCore.Razor.Language" version="5.0.3" targetFramework="net472" />
44
<package id="Microsoft.CodeAnalysis.Analyzers" version="3.3.0" targetFramework="net472" developmentDependency="true" />
5-
<package id="Microsoft.CodeAnalysis.Common" version="3.7.0" targetFramework="net472" />
6-
<package id="Microsoft.CodeAnalysis.CSharp" version="3.7.0" targetFramework="net472" />
5+
<package id="Microsoft.CodeAnalysis.Common" version="3.8.0" targetFramework="net472" />
6+
<package id="Microsoft.CodeAnalysis.CSharp" version="3.8.0" targetFramework="net472" />
77
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
8-
<package id="System.Collections.Immutable" version="1.7.1" targetFramework="net472" />
8+
<package id="System.Collections.Immutable" version="5.0.0" targetFramework="net472" />
99
<package id="System.Memory" version="4.5.4" targetFramework="net472" />
1010
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
11-
<package id="System.Reflection.Metadata" version="1.8.1" targetFramework="net472" />
11+
<package id="System.Reflection.Metadata" version="5.0.0" targetFramework="net472" />
1212
<package id="System.Runtime.CompilerServices.Unsafe" version="4.7.1" targetFramework="net472" />
1313
<package id="System.Text.Encoding.CodePages" version="4.7.1" targetFramework="net472" />
1414
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" />

RazorEngineCore.Tests/RazorEngineCore.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net472</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.1;net472;net5.0</TargetFrameworks>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

RazorEngineCore.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExampleAppCore", "ExampleAp
1616
EndProject
1717
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleAppNET472", "ExampleAppNet472\ExampleAppNET472.csproj", "{D27C1578-BFF9-4469-9099-DAF64450BBBE}"
1818
EndProject
19+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleAppNET5", "ExampleAppNET5\ExampleAppNET5.csproj", "{B19C090A-4EC1-441D-B702-ADD2ECA79198}"
20+
EndProject
1921
Global
2022
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2123
Debug|Any CPU = Debug|Any CPU
@@ -38,6 +40,10 @@ Global
3840
{D27C1578-BFF9-4469-9099-DAF64450BBBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
3941
{D27C1578-BFF9-4469-9099-DAF64450BBBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
4042
{D27C1578-BFF9-4469-9099-DAF64450BBBE}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{B19C090A-4EC1-441D-B702-ADD2ECA79198}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
44+
{B19C090A-4EC1-441D-B702-ADD2ECA79198}.Debug|Any CPU.Build.0 = Debug|Any CPU
45+
{B19C090A-4EC1-441D-B702-ADD2ECA79198}.Release|Any CPU.ActiveCfg = Release|Any CPU
46+
{B19C090A-4EC1-441D-B702-ADD2ECA79198}.Release|Any CPU.Build.0 = Release|Any CPU
4147
EndGlobalSection
4248
GlobalSection(SolutionProperties) = preSolution
4349
HideSolutionNode = FALSE

RazorEngineCore/RazorEngine.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.CodeAnalysis;
99
using Microsoft.CodeAnalysis.CSharp;
1010
using Microsoft.CodeAnalysis.Emit;
11+
using System.Reflection.Metadata;
1112

1213
namespace RazorEngineCore
1314
{
@@ -82,7 +83,21 @@ private MemoryStream CreateAndCompileToStream(string templateSource, RazorEngine
8283
syntaxTree
8384
},
8485
options.ReferencedAssemblies
85-
.Select(ass => MetadataReference.CreateFromFile(ass.Location))
86+
.Select(ass =>
87+
{
88+
#if NETSTANDARD2_0
89+
return MetadataReference.CreateFromFile(ass.Location);
90+
#else
91+
unsafe
92+
{
93+
ass.TryGetRawMetadata(out byte* blob, out int length);
94+
var moduleMetadata = ModuleMetadata.CreateFromMetadata((IntPtr)blob, length);
95+
var assemblyMetadata = AssemblyMetadata.Create(moduleMetadata);
96+
var metadataReference = assemblyMetadata.GetReference();
97+
return metadataReference;
98+
}
99+
#endif
100+
})
86101
.Concat(options.MetadataReferences)
87102
.ToList(),
88103
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>netstandard2.0</TargetFramework>
3+
<TargetFrameworks>net5.0;netstandard2.0</TargetFrameworks>
44
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
5-
<Version>2020.10.1</Version>
5+
<Version>2021.03.1</Version>
66
<Authors>Alexander Selishchev, Simon Mourier, William David Cossey, Benjamin Smith, Dag H. Baardsen, krmr</Authors>
77
<PackageProjectUrl>https://github.com/adoconnection/RazorEngineCore</PackageProjectUrl>
8-
<Description>ASP.NET Core 3.1 Razor Template Engine</Description>
9-
<AssemblyVersion>2020.10.1.0</AssemblyVersion>
10-
<FileVersion>2020.10.1.0</FileVersion>
8+
<Description>NET5 Razor Template Engine</Description>
9+
<AssemblyVersion>2021.03.1</AssemblyVersion>
10+
<FileVersion>2021.03.1</FileVersion>
1111
<PackageReleaseNotes>– Linux fix: The type 'Object' is defined in an assembly that is not referenced</PackageReleaseNotes>
1212
<Company>Alexander Selishchev, Simon Mourier, William David Cossey, Benjamin Smith, Dag H. Baardsen, krmr</Company>
1313
</PropertyGroup>
14+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
15+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
16+
</PropertyGroup>
17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
18+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
19+
</PropertyGroup>
1420
<ItemGroup>
15-
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="3.1.8" />
16-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.7.0" />
21+
<PackageReference Include="Microsoft.AspNetCore.Razor.Language" Version="5.0.3" />
22+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0" />
1723
</ItemGroup>
1824
</Project>

0 commit comments

Comments
 (0)