Skip to content

Commit 50db5fa

Browse files
committed
2 parents 2b08451 + e483619 commit 50db5fa

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

README.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# RazorEngineCore
2-
NETCore 3.1.4 Razor Template Engine. No legacy code.
2+
NETCore 3.1.5 Razor Template Engine. No legacy code.
33
* .NET Standard 2.0
44

55
[![NuGet](https://img.shields.io/nuget/dt/RazorEngineCore.svg?style=flat-square)](https://www.nuget.org/packages/RazorEngineCore)
@@ -24,6 +24,7 @@ Install-Package RazorEngineCore
2424
* [@Raw](https://github.com/adoconnection/RazorEngineCore/wiki/@Raw)
2525
* [@Inject and referencing other assemblies](https://github.com/adoconnection/RazorEngineCore/wiki/@Inject-and-referencing-other-assemblies)
2626
* [Switch from RazorEngine cshtml templates](https://github.com/adoconnection/RazorEngineCore/wiki/Switch-from-RazorEngine-cshtml-templates)
27+
* [Azure Functions FileNotFoundException workaround](https://github.com/adoconnection/RazorEngineCore/wiki/Azure-Functions-FileNotFoundException-workaround)
2728

2829
## Extensions
2930
* [wdcossey/RazorEngineCore.Extensions](https://github.com/wdcossey/RazorEngineCore.Extensions)
@@ -32,8 +33,8 @@ Install-Package RazorEngineCore
3233

3334
#### Basic usage
3435
```cs
35-
RazorEngine razorEngine = new RazorEngine();
36-
RazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name");
36+
IRazorEngine razorEngine = new RazorEngine();
37+
IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name");
3738

3839
string result = template.Run(new
3940
{
@@ -45,10 +46,11 @@ Console.WriteLine(result);
4546

4647
#### Strongly typed model
4748
```cs
49+
IRazorEngine razorEngine = new RazorEngine();
4850
string templateText = "Hello @Model.Name";
4951

5052
// yeah, heavy definition
51-
RazorEngineCompiledTemplate<RazorEngineTemplateBase<TestModel>> template = razorEngine.Compile<RazorEngineTemplateBase<TestModel>>(templateText);
53+
IRazorEngineCompiledTemplate<RazorEngineTemplateBase<TestModel>> template = razorEngine.Compile<RazorEngineTemplateBase<TestModel>>(templateText);
5254

5355
string result = template.Run(instance =>
5456
{
@@ -65,8 +67,8 @@ Console.WriteLine(result);
6567
#### Save / Load compiled templates
6668
Most expensive task is to compile template, you should not compile template every time you need to run it
6769
```cs
68-
RazorEngine razorEngine = new RazorEngine();
69-
RazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name");
70+
IRazorEngine razorEngine = new RazorEngine();
71+
IRazorEngineCompiledTemplate template = razorEngine.Compile("Hello @Model.Name");
7072

7173
// save to file
7274
template.SaveToFile("myTemplate.dll");
@@ -77,13 +79,13 @@ template.SaveToStream(memoryStream);
7779
```
7880

7981
```cs
80-
RazorEngineCompiledTemplate template1 = RazorEngineCompiledTemplate.LoadFromFile("myTemplate.dll");
81-
RazorEngineCompiledTemplate template2 = RazorEngineCompiledTemplate.LoadFromStream(myStream);
82+
IRazorEngineCompiledTemplate template1 = RazorEngineCompiledTemplate.LoadFromFile("myTemplate.dll");
83+
IRazorEngineCompiledTemplate template2 = RazorEngineCompiledTemplate.LoadFromStream(myStream);
8284
```
8385

8486
```cs
85-
RazorEngineCompiledTemplate<MyBase> template1 = RazorEngineCompiledTemplate<MyBase>.LoadFromFile<MyBase>("myTemplate.dll");
86-
RazorEngineCompiledTemplate<MyBase> template2 = RazorEngineCompiledTemplate<MyBase>.LoadFromStream<MyBase>(myStream);
87+
IRazorEngineCompiledTemplate<MyBase> template1 = RazorEngineCompiledTemplate<MyBase>.LoadFromFile<MyBase>("myTemplate.dll");
88+
IRazorEngineCompiledTemplate<MyBase> template2 = RazorEngineCompiledTemplate<MyBase>.LoadFromStream<MyBase>(myStream);
8789
```
8890

8991
#### Caching
@@ -92,15 +94,15 @@ RazorEngineCore is not responsible for caching. Each team and project has their
9294
If you dont have one, use following static ConcurrentDictionary example as a simplest thread safe solution.
9395

9496
```cs
95-
private static ConcurrentDictionary<string, RazorEngineCompiledTemplate> TemplateCache = new ConcurrentDictionary<string, RazorEngineCompiledTemplate>();
97+
private static ConcurrentDictionary<int, IRazorEngineCompiledTemplate> TemplateCache = new ConcurrentDictionary<int, IRazorEngineCompiledTemplate>();
9698
```
9799

98100
```cs
99101
private string RenderTemplate(string template, object model)
100102
{
101103
int hashCode = template.GetHashCode();
102104

103-
RazorEngineCompiledTemplate compiledTemplate = TemplateCache.GetOrAdd(hashCode, i =>
105+
IRazorEngineCompiledTemplate compiledTemplate = TemplateCache.GetOrAdd(hashCode, i =>
104106
{
105107
RazorEngine razorEngine = new RazorEngine();
106108
return razorEngine.Compile(Content);
@@ -141,8 +143,8 @@ output:
141143
```cs
142144
string content = @"Hello @A, @B, @Decorator(123)";
143145

144-
RazorEngine razorEngine = new RazorEngine();
145-
RazorEngineCompiledTemplate<CustomModel> template = razorEngine.Compile<CustomModel>(content);
146+
IRazorEngine razorEngine = new RazorEngine();
147+
IRazorEngineCompiledTemplate<CustomModel> template = razorEngine.Compile<CustomModel>(content);
146148

147149
string result = template.Run(instance =>
148150
{
@@ -170,8 +172,8 @@ Keep your templates as simple as possible, if you need to inject "unusual" assem
170172
Writing `@using System.IO` in template will not reference System.IO assembly, use builder to manually reference it.
171173

172174
```cs
173-
RazorEngine razorEngine = new RazorEngine();
174-
RazorEngineCompiledTemplate compiledTemplate = razorEngine.Compile(templateText, builder =>
175+
IRazorEngine razorEngine = new RazorEngine();
176+
IRazorEngineCompiledTemplate compiledTemplate = razorEngine.Compile(templateText, builder =>
175177
{
176178
builder.AddAssemblyReferenceByName("System.Security"); // by name
177179
builder.AddAssemblyReference(typeof(System.IO.File)); // by type

RazorEngineCore/RazorEngineCompilationOptions.cs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,41 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Reflection;
3+
using System.Runtime.InteropServices;
34
using Microsoft.CodeAnalysis;
45

56
namespace RazorEngineCore
67
{
78
public class RazorEngineCompilationOptions
89
{
9-
public HashSet<Assembly> ReferencedAssemblies { get; set; } = new HashSet<Assembly>()
10-
{
11-
typeof(object).Assembly,
12-
Assembly.Load(new AssemblyName("Microsoft.CSharp")),
13-
typeof(RazorEngineTemplateBase).Assembly,
14-
Assembly.Load(new AssemblyName("netstandard")),
15-
Assembly.Load(new AssemblyName("System.Runtime")),
16-
Assembly.Load(new AssemblyName("System.Linq")),
17-
Assembly.Load(new AssemblyName("System.Linq.Expressions"))
18-
};
10+
private HashSet<Assembly> _referencedAssemblies;
11+
public HashSet<Assembly> ReferencedAssemblies {
12+
get
13+
{
14+
if (this._referencedAssemblies != null)
15+
return this._referencedAssemblies;
16+
17+
this._referencedAssemblies = new HashSet<Assembly>()
18+
{
19+
typeof(object).Assembly,
20+
Assembly.Load(new AssemblyName("Microsoft.CSharp")),
21+
typeof(RazorEngineTemplateBase).Assembly,
22+
Assembly.Load(new AssemblyName("System.Runtime")),
23+
Assembly.Load(new AssemblyName("System.Linq")),
24+
Assembly.Load(new AssemblyName("System.Linq.Expressions"))
25+
};
26+
27+
// Loading netstandard explicitly causes runtime error on Linux/OSX
28+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
29+
{
30+
this._referencedAssemblies.Add(Assembly.Load(new AssemblyName("netstandard")));
31+
}
32+
33+
return this._referencedAssemblies;
34+
}
35+
}
1936

2037
public HashSet<MetadataReference> MetadataReferences { get; set; } = new HashSet<MetadataReference>();
21-
38+
2239
public string TemplateNamespace { get; set; } = "TemplateNamespace";
2340
public string Inherits { get; set; } = "RazorEngineCore.RazorEngineTemplateBase";
2441

0 commit comments

Comments
 (0)