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,41 +16,42 @@ namespace RazorEngineCore
1516{
1617 public class RazorEngine : IRazorEngine
1718 {
18- public IRazorEngineCompiledTemplate < T > Compile < T > ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null ) where T : IRazorEngineTemplate
19+ public IRazorEngineCompiledTemplate < T > Compile < T > ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null , CancellationToken cancellationToken = default ) where T : IRazorEngineTemplate
1920 {
2021 IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder ( ) ;
2122 compilationOptionsBuilder . AddAssemblyReference ( typeof ( T ) . Assembly ) ;
2223 compilationOptionsBuilder . Inherits ( typeof ( T ) ) ;
2324
2425 builderAction ? . Invoke ( compilationOptionsBuilder ) ;
2526
26- RazorEngineCompiledTemplateMeta meta = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
27+ MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options , cancellationToken ) ;
2728
2829 return new RazorEngineCompiledTemplate < T > ( meta ) ;
2930 }
3031
31- public Task < IRazorEngineCompiledTemplate < T > > CompileAsync < T > ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null ) where T : IRazorEngineTemplate
32+ public Task < IRazorEngineCompiledTemplate < T > > CompileAsync < T > ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null , CancellationToken cancellationToken = default ) where T : IRazorEngineTemplate
3233 {
33- return Task . Factory . StartNew ( ( ) => this . Compile < T > ( content : content , builderAction : builderAction ) ) ;
34+ return Task . Factory . StartNew ( ( ) => this . Compile < T > ( content : content , builderAction : builderAction , cancellationToken : cancellationToken ) ) ;
3435 }
3536
36- public IRazorEngineCompiledTemplate Compile ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null )
37+ public IRazorEngineCompiledTemplate Compile ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null , CancellationToken cancellationToken = default )
3738 {
3839 IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder ( ) ;
3940 compilationOptionsBuilder . Inherits ( typeof ( RazorEngineTemplateBase ) ) ;
4041
4142 builderAction ? . Invoke ( compilationOptionsBuilder ) ;
42-
43- RazorEngineCompiledTemplateMeta meta = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
43+
44+ RazorEngineCompiledTemplateMeta meta = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options , cancellationToken ) ;
4445 return new RazorEngineCompiledTemplate ( meta ) ;
4546 }
4647
47- public Task < IRazorEngineCompiledTemplate > CompileAsync ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null )
48+ public Task < IRazorEngineCompiledTemplate > CompileAsync ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null , CancellationToken cancellationToken = default )
4849 {
49- return Task . Factory . StartNew ( ( ) => this . Compile ( content : content , builderAction : builderAction ) ) ;
50+ return Task . Factory . StartNew ( ( ) => this . Compile ( content : content , builderAction : builderAction , cancellationToken : cancellationToken ) ) ;
5051 }
5152
52- protected virtual RazorEngineCompiledTemplateMeta CreateAndCompileToStream ( string templateSource , RazorEngineCompilationOptions options )
53+ protected virtual RazorEngineCompiledTemplateMeta CreateAndCompileToStream ( string templateSource , RazorEngineCompilationOptions options , CancellationToken cancellationToken )
54+
5355 {
5456 templateSource = this . WriteDirectives ( templateSource , options ) ;
5557 string projectPath = @"." ;
@@ -75,9 +77,9 @@ protected virtual RazorEngineCompiledTemplateMeta CreateAndCompileToStream(strin
7577 new List < TagHelperDescriptor > ( ) ) ;
7678
7779
78-
7980 RazorCSharpDocument razorCSharpDocument = codeDocument . GetCSharpDocument ( ) ;
80- SyntaxTree syntaxTree = CSharpSyntaxTree . ParseText ( razorCSharpDocument . GeneratedCode ) ;
81+ SyntaxTree syntaxTree = CSharpSyntaxTree . ParseText ( razorCSharpDocument . GeneratedCode , cancellationToken : cancellationToken ) ;
82+
8183
8284 CSharpCompilation compilation = CSharpCompilation . Create (
8385 fileName ,
@@ -110,7 +112,8 @@ protected virtual RazorEngineCompiledTemplateMeta CreateAndCompileToStream(strin
110112 MemoryStream assemblyStream = new MemoryStream ( ) ;
111113 MemoryStream pdbStream = options . IncludeDebuggingInfo ? new MemoryStream ( ) : null ;
112114
113- EmitResult emitResult = compilation . Emit ( assemblyStream , pdbStream ) ;
115+ EmitResult emitResult = compilation . Emit ( assemblyStream , pdbStream , cancellationToken : cancellationToken ) ;
116+
114117
115118 if ( ! emitResult . Success )
116119 {
0 commit comments