@@ -17,7 +17,6 @@ public class RazorEngine : IRazorEngine
1717 public IRazorEngineCompiledTemplate < T > Compile < T > ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null ) where T : IRazorEngineTemplate
1818 {
1919 IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder ( ) ;
20-
2120 compilationOptionsBuilder . AddAssemblyReference ( typeof ( T ) . Assembly ) ;
2221 compilationOptionsBuilder . Inherits ( typeof ( T ) ) ;
2322
@@ -32,31 +31,50 @@ public Task<IRazorEngineCompiledTemplate<T>> CompileAsync<T>(string content, Act
3231 {
3332 return Task . Factory . StartNew ( ( ) => this . Compile < T > ( content : content , builderAction : builderAction ) ) ;
3433 }
35-
3634 public IRazorEngineCompiledTemplate Compile ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null )
35+ {
36+ return Compile ( content , builderAction , false ) ;
37+ }
38+ public IRazorEngineCompiledTemplate Compile ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null , bool addPdb = false )
3739 {
3840 IRazorEngineCompilationOptionsBuilder compilationOptionsBuilder = new RazorEngineCompilationOptionsBuilder ( ) ;
3941 compilationOptionsBuilder . Inherits ( typeof ( RazorEngineTemplateBase ) ) ;
4042
4143 builderAction ? . Invoke ( compilationOptionsBuilder ) ;
44+ if ( addPdb )
45+ {
46+ MemoryStream pdbStream = new MemoryStream ( ) ;
47+ MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options , pdbStream ) ;
48+ return new RazorEngineCompiledTemplate ( memoryStream , compilationOptionsBuilder . Options . TemplateNamespace , pdbStream ) ;
49+ }
50+ else
51+ {
52+ MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
53+ return new RazorEngineCompiledTemplate ( memoryStream , compilationOptionsBuilder . Options . TemplateNamespace ) ;
54+ }
4255
43- MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
44-
45- return new RazorEngineCompiledTemplate ( memoryStream , compilationOptionsBuilder . Options . TemplateNamespace ) ;
4656 }
4757
4858 public Task < IRazorEngineCompiledTemplate > CompileAsync ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null )
4959 {
5060 return Task . Factory . StartNew ( ( ) => this . Compile ( content : content , builderAction : builderAction ) ) ;
5161 }
5262
53- protected virtual MemoryStream CreateAndCompileToStream ( string templateSource , RazorEngineCompilationOptions options )
63+ protected virtual MemoryStream CreateAndCompileToStream ( string templateSource , RazorEngineCompilationOptions options , MemoryStream pdbStream = null )
5464 {
5565 templateSource = this . WriteDirectives ( templateSource , options ) ;
66+ string projectPath = @"." ;
67+ string fileName = Path . GetRandomFileName ( ) + ".cshtml" ;
68+ if ( pdbStream != null )
69+ {
70+ projectPath = Path . GetTempPath ( ) ;
71+ Directory . CreateDirectory ( projectPath ) ;
72+ File . WriteAllText ( Path . Combine ( projectPath , fileName ) , templateSource ) ;
73+ }
5674
5775 RazorProjectEngine engine = RazorProjectEngine . Create (
5876 RazorConfiguration . Default ,
59- RazorProjectFileSystem . Create ( @"." ) ,
77+ RazorProjectFileSystem . Create ( projectPath ) ,
6078 ( builder ) =>
6179 {
6280 builder . SetNamespace ( options . TemplateNamespace ) ;
@@ -73,7 +91,6 @@ protected virtual MemoryStream CreateAndCompileToStream(string templateSource, R
7391 new List < TagHelperDescriptor > ( ) ) ;
7492
7593 RazorCSharpDocument razorCSharpDocument = codeDocument . GetCSharpDocument ( ) ;
76-
7794 SyntaxTree syntaxTree = CSharpSyntaxTree . ParseText ( razorCSharpDocument . GeneratedCode ) ;
7895
7996 CSharpCompilation compilation = CSharpCompilation . Create (
@@ -104,8 +121,11 @@ protected virtual MemoryStream CreateAndCompileToStream(string templateSource, R
104121 new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) ) ;
105122
106123 MemoryStream memoryStream = new MemoryStream ( ) ;
107-
108- EmitResult emitResult = compilation . Emit ( memoryStream ) ;
124+ EmitResult emitResult ;
125+ if ( pdbStream != null )
126+ emitResult = compilation . Emit ( memoryStream , pdbStream ) ;
127+ else
128+ emitResult = compilation . Emit ( memoryStream ) ;
109129
110130 if ( ! emitResult . Success )
111131 {
@@ -119,7 +139,8 @@ protected virtual MemoryStream CreateAndCompileToStream(string templateSource, R
119139 }
120140
121141 memoryStream . Position = 0 ;
122-
142+ if ( pdbStream != null )
143+ pdbStream . Position = 0 ;
123144 return memoryStream ;
124145 }
125146
@@ -138,4 +159,4 @@ protected virtual string WriteDirectives(string content, RazorEngineCompilationO
138159 return stringBuilder . ToString ( ) ;
139160 }
140161 }
141- }
162+ }
0 commit comments