@@ -22,9 +22,8 @@ public IRazorEngineCompiledTemplate<T> Compile<T>(string content, Action<IRazorE
2222
2323 builderAction ? . Invoke ( compilationOptionsBuilder ) ;
2424
25- MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
26-
27- return new RazorEngineCompiledTemplate < T > ( memoryStream , compilationOptionsBuilder . Options . TemplateNamespace ) ;
25+ CompiledStreams streams = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
26+ return new RazorEngineCompiledTemplate < T > ( streams . assembly , compilationOptionsBuilder . Options . TemplateNamespace ) ;
2827 }
2928
3029 public Task < IRazorEngineCompiledTemplate < T > > CompileAsync < T > ( string content , Action < IRazorEngineCompilationOptionsBuilder > builderAction = null ) where T : IRazorEngineTemplate
@@ -43,14 +42,13 @@ public IRazorEngineCompiledTemplate Compile(string content, Action<IRazorEngineC
4342 builderAction ? . Invoke ( compilationOptionsBuilder ) ;
4443 if ( compilationOptionsBuilder . Options . GeneratePdbSteram )
4544 {
46- MemoryStream pdbStream = new MemoryStream ( ) ;
47- MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options , pdbStream ) ;
48- return new RazorEngineCompiledTemplate ( memoryStream , compilationOptionsBuilder . Options . TemplateNamespace , pdbStream ) ;
45+ CompiledStreams streams = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
46+ return new RazorEngineCompiledTemplate ( streams . assembly , compilationOptionsBuilder . Options . TemplateNamespace , streams . pdb ) ;
4947 }
5048 else
5149 {
52- MemoryStream memoryStream = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
53- return new RazorEngineCompiledTemplate ( memoryStream , compilationOptionsBuilder . Options . TemplateNamespace ) ;
50+ CompiledStreams streams = this . CreateAndCompileToStream ( content , compilationOptionsBuilder . Options ) ;
51+ return new RazorEngineCompiledTemplate ( streams . assembly , compilationOptionsBuilder . Options . TemplateNamespace ) ;
5452 }
5553
5654 }
@@ -60,12 +58,12 @@ public Task<IRazorEngineCompiledTemplate> CompileAsync(string content, Action<IR
6058 return Task . Factory . StartNew ( ( ) => this . Compile ( content : content , builderAction : builderAction ) ) ;
6159 }
6260
63- protected virtual MemoryStream CreateAndCompileToStream ( string templateSource , RazorEngineCompilationOptions options , MemoryStream pdbStream = null )
61+ protected virtual CompiledStreams CreateAndCompileToStream ( string templateSource , RazorEngineCompilationOptions options , MemoryStream pdbStream = null )
6462 {
6563 templateSource = this . WriteDirectives ( templateSource , options ) ;
6664 string projectPath = @"." ;
6765 string fileName = Path . GetRandomFileName ( ) + ".cshtml" ;
68- if ( pdbStream != null )
66+ if ( options . GeneratePdbSteram )
6967 {
7068 projectPath = Path . GetTempPath ( ) ;
7169 Directory . CreateDirectory ( projectPath ) ;
@@ -119,13 +117,12 @@ protected virtual MemoryStream CreateAndCompileToStream(string templateSource, R
119117 . Concat ( options . MetadataReferences )
120118 . ToList ( ) ,
121119 new CSharpCompilationOptions ( OutputKind . DynamicallyLinkedLibrary ) ) ;
122-
123- MemoryStream memoryStream = new MemoryStream ( ) ;
120+ CompiledStreams streams = new CompiledStreams ( ) ;
124121 EmitResult emitResult ;
125- if ( pdbStream != null )
126- emitResult = compilation . Emit ( memoryStream , pdbStream ) ;
122+ if ( options . GeneratePdbSteram )
123+ emitResult = compilation . Emit ( streams . assembly , streams . pdb ) ;
127124 else
128- emitResult = compilation . Emit ( memoryStream ) ;
125+ emitResult = compilation . Emit ( streams . assembly ) ;
129126
130127 if ( ! emitResult . Success )
131128 {
@@ -138,10 +135,10 @@ protected virtual MemoryStream CreateAndCompileToStream(string templateSource, R
138135 throw exception ;
139136 }
140137
141- memoryStream . Position = 0 ;
142- if ( pdbStream != null )
143- pdbStream . Position = 0 ;
144- return memoryStream ;
138+ streams . assembly . Position = 0 ;
139+ if ( options . GeneratePdbSteram )
140+ streams . pdb . Position = 0 ;
141+ return streams ;
145142 }
146143
147144 protected virtual string WriteDirectives ( string content , RazorEngineCompilationOptions options )
@@ -158,5 +155,16 @@ protected virtual string WriteDirectives(string content, RazorEngineCompilationO
158155
159156 return stringBuilder . ToString ( ) ;
160157 }
158+
159+ private class CompiledStreams
160+ {
161+ public CompiledStreams ( )
162+ {
163+ assembly = new MemoryStream ( ) ;
164+ pdb = new MemoryStream ( ) ;
165+ }
166+ public MemoryStream assembly { get ; set ; }
167+ public MemoryStream pdb { get ; set ; }
168+ }
161169 }
162170}
0 commit comments