Skip to content

Commit 9a5ebf1

Browse files
committed
Use NonDisposableStream instead of using MemoryStream.ToArray()
1 parent 39ab9ba commit 9a5ebf1

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/Microsoft.AspNet.Mvc.Razor/Precompilation/RazorPreCompiler.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Reflection;
99
using System.Threading.Tasks;
1010
using Microsoft.AspNet.FileProviders;
11+
using Microsoft.AspNet.Mvc.Internal;
1112
using Microsoft.AspNet.Mvc.Razor.Compilation;
1213
using Microsoft.AspNet.Mvc.Razor.Directives;
1314
using Microsoft.AspNet.Mvc.Razor.Internal;
@@ -92,7 +93,7 @@ protected virtual RazorFileInfoCollection CreateFileInfoCollection()
9293
var file = filesToProcess[index];
9394

9495
PrecompilationCacheEntry cacheEntry;
95-
if(!PreCompilationCache.TryGetValue(file.RelativePath, out cacheEntry))
96+
if (!PreCompilationCache.TryGetValue(file.RelativePath, out cacheEntry))
9697
{
9798
cacheEntry = GetCacheEntry(file);
9899
PreCompilationCache.Set(
@@ -163,22 +164,17 @@ protected virtual RazorFileInfoCollection GeneratePrecompiledAssembly(
163164
}
164165
else
165166
{
166-
assemblyStream.Position = 0;
167-
var assemblyBytes = assemblyStream.ToArray();
168167
var assemblyResource = new ResourceDescription(assemblyResourceName,
169-
() => new MemoryStream(assemblyBytes),
168+
() => GetNonDisposableStream(assemblyStream),
170169
isPublic: true);
171170
CompileContext.Resources.Add(assemblyResource);
172171

173172
string symbolsResourceName = null;
174173
if (pdbStream != null)
175174
{
176175
symbolsResourceName = resourcePrefix + ".pdb";
177-
pdbStream.Position = 0;
178-
var pdbBytes = pdbStream.ToArray();
179-
180176
var pdbResource = new ResourceDescription(symbolsResourceName,
181-
() => new MemoryStream(pdbBytes),
177+
() => GetNonDisposableStream(pdbStream),
182178
isPublic: true);
183179

184180
CompileContext.Resources.Add(pdbResource);
@@ -279,6 +275,12 @@ private static void AddRange<TVal>(IList<TVal> target, IEnumerable<TVal> source)
279275
}
280276
}
281277

278+
private static Stream GetNonDisposableStream(Stream stream)
279+
{
280+
stream.Position = 0;
281+
return new NonDisposableStream(stream);
282+
}
283+
282284
private class PrecompileRazorFileInfoCollection : RazorFileInfoCollection
283285
{
284286
public PrecompileRazorFileInfoCollection(string assemblyResourceName,

0 commit comments

Comments
 (0)