33using System . IO ;
44using System . Linq ;
55using System . Text ;
6+ using System . Threading ;
67using System . Threading . Tasks ;
78using Microsoft . AspNetCore . Razor . Language ;
89using Microsoft . CodeAnalysis ;
@@ -15,6 +16,11 @@ namespace RazorEngineCore
1516 public class RazorEngine : IRazorEngine
1617 {
1718 public IRazorEngineCompiledTemplate < T > Compile < T > ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null ) where T : IRazorEngineTemplate
19+ {
20+ return Compile < T > ( content , builderAction , cancellationToken : default ) ;
21+ }
22+
23+ public IRazorEngineCompiledTemplate < T > Compile < T > ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction , CancellationToken cancellationToken ) where T : IRazorEngineTemplate
1824 {
1925 IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder ( ) ;
2026
@@ -23,34 +29,54 @@ public IRazorEngineCompiledTemplate<T> Compile<T>(string content, Action<IRazorE
2329
2430 builderAction ? . Invoke ( compilationOptionsBuilder ) ;
2531
26- MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
32+ MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options , cancellationToken ) ;
2733
2834 return new RazorEngineCompiledTemplate < T > ( memoryStream , compilationOptionsBuilder . Options . TemplateNamespace ) ;
2935 }
3036
3137 public Task < IRazorEngineCompiledTemplate < T > > CompileAsync < T > ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null ) where T : IRazorEngineTemplate
3238 {
33- return Task . Factory . StartNew ( ( ) => this . Compile < T > ( content : content , builderAction : builderAction ) ) ;
39+ return CompileAsync < T > ( content , builderAction , cancellationToken : default ) ;
40+ }
41+
42+ public Task < IRazorEngineCompiledTemplate < T > > CompileAsync < T > ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction , CancellationToken cancellationToken ) where T : IRazorEngineTemplate
43+ {
44+ return Task . Factory . StartNew ( ( ) => this . Compile < T > ( content : content , builderAction : builderAction , cancellationToken : cancellationToken ) ) ;
3445 }
3546
3647 public IRazorEngineCompiledTemplate Compile ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null )
48+ {
49+ return Compile ( content , builderAction , cancellationToken : default ) ;
50+ }
51+
52+ public IRazorEngineCompiledTemplate Compile ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction , CancellationToken cancellationToken )
3753 {
3854 IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder ( ) ;
3955 compilationOptionsBuilder . Inherits ( typeof ( RazorEngineTemplateBase ) ) ;
4056
4157 builderAction ? . Invoke ( compilationOptionsBuilder ) ;
4258
43- MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
59+ MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options , cancellationToken ) ;
4460
4561 return new RazorEngineCompiledTemplate ( memoryStream , compilationOptionsBuilder . Options . TemplateNamespace ) ;
4662 }
4763
4864 public Task < IRazorEngineCompiledTemplate > CompileAsync ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null )
4965 {
50- return Task . Factory . StartNew ( ( ) => this . Compile ( content : content , builderAction : builderAction ) ) ;
66+ return CompileAsync ( content , builderAction , cancellationToken : default ) ;
67+ }
68+
69+ public Task < IRazorEngineCompiledTemplate > CompileAsync ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction , CancellationToken cancellationToken )
70+ {
71+ return Task . Factory . StartNew ( ( ) => this . Compile ( content : content , builderAction : builderAction , cancellationToken : cancellationToken ) ) ;
5172 }
5273
5374 protected virtual MemoryStream CreateAndCompileToStream ( string templateSource , RazorEngineCompilationOptions options )
75+ {
76+ return CreateAndCompileToStream ( templateSource , options , cancellationToken : default ) ;
77+ }
78+
79+ protected virtual MemoryStream CreateAndCompileToStream ( string templateSource , RazorEngineCompilationOptions options , CancellationToken cancellationToken )
5480 {
5581 templateSource = this . WriteDirectives ( templateSource , options ) ;
5682
@@ -74,7 +100,7 @@ protected virtual MemoryStream CreateAndCompileToStream(string templateSource, R
74100
75101 RazorCSharpDocument razorCSharpDocument = codeDocument . GetCSharpDocument ( ) ;
76102
77- SyntaxTree syntaxTree = CSharpSyntaxTree . ParseText ( razorCSharpDocument . GeneratedCode ) ;
103+ SyntaxTree syntaxTree = CSharpSyntaxTree . ParseText ( razorCSharpDocument . GeneratedCode , cancellationToken : cancellationToken ) ;
78104
79105 CSharpCompilation compilation = CSharpCompilation . Create (
80106 fileName ,
@@ -105,7 +131,7 @@ protected virtual MemoryStream CreateAndCompileToStream(string templateSource, R
105131
106132 MemoryStream memoryStream = new MemoryStream ( ) ;
107133
108- EmitResult emitResult = compilation . Emit ( memoryStream ) ;
134+ EmitResult emitResult = compilation . Emit ( memoryStream , cancellationToken : cancellationToken ) ;
109135
110136 if ( ! emitResult . Success )
111137 {
0 commit comments